Beautiful Open-Source Form Builder
New feature visibility - public/draft (#10)
Chirag36e038a23bed
diff --git a/app/Http/Controllers/Forms/PublicFormController.php b/app/Http/Controllers/Forms/PublicFormController.phpindex 1cbc0929e..5b4fab184 100644--- a/app/Http/Controllers/Forms/PublicFormController.php+++ b/app/Http/Controllers/Forms/PublicFormController.php@@ -21,7 +21,7 @@ class PublicFormController extends Controllerpublic function show(Request $request, string $slug){- $form = Form::whereSlug($slug)->firstOrFail();+ $form = Form::whereSlug($slug)->whereVisibility('public')->firstOrFail();if ($form->workspace == null) {// Workspace deletedreturn $this->error([diff --git a/app/Http/Requests/AnswerFormRequest.php b/app/Http/Requests/AnswerFormRequest.phpindex 379d28afa..7cd1f3b68 100644--- a/app/Http/Requests/AnswerFormRequest.php+++ b/app/Http/Requests/AnswerFormRequest.php@@ -40,7 +40,7 @@ class AnswerFormRequest extends FormRequest*/public function authorize(){- return !$this->form->is_closed && !$this->form->max_number_of_submissions_reached;+ return !$this->form->is_closed && !$this->form->max_number_of_submissions_reached && $this->form->visibility === 'public';}/**diff --git a/app/Http/Requests/UserFormRequest.php b/app/Http/Requests/UserFormRequest.phpindex ecfb0739a..8b3da4491 100644--- a/app/Http/Requests/UserFormRequest.php+++ b/app/Http/Requests/UserFormRequest.php@@ -29,6 +29,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest'title' => 'required|string|max:60','description' => 'nullable|string|max:2000','tags' => 'nullable|array',+ 'visibility' => ['required',Rule::in(Form::VISIBILITY)],// Notifications'notifies' => 'boolean',diff --git a/app/Http/Resources/FormResource.php b/app/Http/Resources/FormResource.phpindex 71dee8cc5..dbb850e07 100644--- a/app/Http/Resources/FormResource.php+++ b/app/Http/Resources/FormResource.php@@ -42,6 +42,7 @@ class FormResource extends JsonResource'can_be_indexed' => $this->can_be_indexed,'password' => $this->password,'tags' => $this->tags,+ 'visibility' => $this->visibility,'notification_emails' => $this->notification_emails,'slack_webhook_url' => $this->slack_webhook_url,] : [];diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.phpindex b12cdbe7d..c2f24054c 100644--- a/app/Models/Forms/Form.php+++ b/app/Models/Forms/Form.php@@ -20,6 +20,7 @@ class Form extends Modelconst DARK_MODE_VALUES = ['auto', 'light', 'dark'];const THEMES = ['default', 'simple', 'notion'];const WIDTHS = ['centered', 'full'];+ const VISIBILITY = ['public', 'draft'];use HasFactory, HasSlug, SoftDeletes;@@ -45,6 +46,7 @@ class Form extends Model'title','description','tags',+ 'visibility',// Customization'theme',diff --git a/database/factories/FormFactory.php b/database/factories/FormFactory.phpindex 40d485012..b5e580987 100644--- a/database/factories/FormFactory.php+++ b/database/factories/FormFactory.php@@ -58,6 +58,7 @@ class FormFactory extends Factoryreturn ['title' => $this->faker->text(30),'description' => $this->faker->randomHtml(1),+ 'visibility' => 'public','notifies' => false,'send_submission_confirmation' => false,'webhook_url' => null,diff --git a/database/migrations/2022_10_07_122038_add_visibility_to_forms.php b/database/migrations/2022_10_07_122038_add_visibility_to_forms.phpnew file mode 100644index 000000000..acea5b782--- /dev/null+++ b/database/migrations/2022_10_07_122038_add_visibility_to_forms.php@@ -0,0 +1,32 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ *+ * @return void+ */+ public function up()+ {+ Schema::table('forms', function (Blueprint $table) {+ $table->string('visibility')->default('public');+ });+ }++ /**+ * Reverse the migrations.+ *+ * @return void+ */+ public function down()+ {+ Schema::table('forms', function (Blueprint $table) {+ $table->dropColumn('visibility');+ });+ }+};diff --git a/resources/js/components/open/forms/components/form-components/FormInformation.vue b/resources/js/components/open/forms/components/form-components/FormInformation.vueindex 62631d4eb..f94bd12a7 100644--- a/resources/js/components/open/forms/components/form-components/FormInformation.vue+++ b/resources/js/components/open/forms/components/form-components/FormInformation.vue@@ -27,6 +27,11 @@placeholder="Select Tag(s)" :multiple="true" :allowCreation="true":options="allTagsOptions"/>+ <select-input name="visibility" label="Visibility" :form="form" class="mt-3 mb-6"+ help="Only public form will be accessible"+ placeholder="Select Visibility" :required="true"+ :options="visibilityOptions"+ /><buttonv-if="copyFormOptions.length > 0"class="group mt-3 cursor-pointer relative w-full rounded-lg border-transparent flex-1 appearance-none border border-gray-300 dark:border-gray-600 w-full py-2 px-4 bg-white text-gray-700 dark:bg-notion-dark-light dark:text-gray-300 dark:placeholder-gray-500 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:border-transparent focus:ring-opacity-100"@@ -86,7 +91,17 @@ export default {data () {return {showCopyFormSettingsModal: false,- copyFormId: null+ copyFormId: null,+ visibilityOptions: [+ {+ name: "Public",+ value: "public"+ },+ {+ name: "Draft (form won't be accessible)",+ value: "draft"+ }+ ]}},diff --git a/resources/js/pages/forms/create.vue b/resources/js/pages/forms/create.vueindex 7ce727c09..21cdd8850 100644--- a/resources/js/pages/forms/create.vue+++ b/resources/js/pages/forms/create.vue@@ -171,6 +171,7 @@ export default {this.form = new Form({title: 'My Form',description: null,+ visibility: 'public',workspace_id: this.workspace?.id,properties: [],diff --git a/resources/js/pages/forms/show.vue b/resources/js/pages/forms/show.vueindex 94c1f96f0..e9c5dd3cc 100644--- a/resources/js/pages/forms/show.vue+++ b/resources/js/pages/forms/show.vue@@ -5,6 +5,7 @@<div v-if="form" class="sm:px-6"><h2 class="text-nt-blue text-3xl font-bold z-10 mt-6 mb-3">{{ form.title }}+ <span v-if="form.visibility=='draft'" class="float-right text-white p-2 text-xs inline rounded-lg font-semibold mr-2 bg-gray-400 dark:bg-gray-700">Draft (not public)</span></h2><p class="mb-3">diff --git a/resources/js/pages/home.vue b/resources/js/pages/home.vueindex 05a8e55fc..570d22335 100644--- a/resources/js/pages/home.vue+++ b/resources/js/pages/home.vue@@ -31,7 +31,8 @@</div><div v-if="enrichedForms && enrichedForms.length" class="border border border-gray-300 dark:bg-notion-dark-light rounded-md w-full"><div v-for="(form, index) in enrichedForms" :key="form.id"- class="p-4 w-full mx-auto border-gray-300 hover:bg-blue-100 dark:hover:bg-blue-900 transition-colors cursor-pointer relative" :class="{'border-t':index!==0}"+ class="p-4 w-full mx-auto border-gray-300 hover:bg-blue-100 dark:hover:bg-blue-900 transition-colors cursor-pointer relative"+ :class="{'border-t':index!==0, 'bg-gray-50 dark:bg-gray-400':form.visibility=='draft'}"><div class="items-center space-x-4 truncate"><p class="truncate float-left">diff --git a/tests/Feature/Forms/AnswerFormTest.php b/tests/Feature/Forms/AnswerFormTest.phpindex e76de0fa1..b89c82085 100644--- a/tests/Feature/Forms/AnswerFormTest.php+++ b/tests/Feature/Forms/AnswerFormTest.php@@ -58,6 +58,29 @@ it('can submit form till max submissions count is not reached at limit', functio}// Now, can not submit form, Because it's reached at submission limit+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(403);+});++it('can not open draft form', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'visibility' => 'draft'+ ]);++ $this->getJson(route('forms.show', $form->slug))+ ->assertStatus(404);+});++it('can not submit draft form', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'visibility' => 'draft'+ ]);+ $formData = FormSubmissionDataFactory::generateSubmissionData($form);+$this->postJson(route('forms.answer', $form->slug), $formData)->assertStatus(403);});\ No newline at end of file