From 310df29e73b53c56538b55ec02eda3f5c7065334 Mon Sep 17 00:00:00 2001 From: Chirag <103994754+chiragnotionforms@users.noreply.github.com> Date: Wed, 16 Nov 2022 15:50:28 +0530 Subject: [PATCH] 05444 save content of form in browser storage if not submitted (#26) * Save content of form in browser storage if not submitted * Rename key form + fix tests --- app/Http/Resources/FormResource.php | 3 ++- app/Models/Forms/Form.php | 8 ++++++++ .../components/open/forms/OpenCompleteForm.vue | 3 +++ .../js/components/open/forms/OpenForm.vue | 18 +++++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/Http/Resources/FormResource.php b/app/Http/Resources/FormResource.php index 60dfcf6..c359c33 100644 --- a/app/Http/Resources/FormResource.php +++ b/app/Http/Resources/FormResource.php @@ -58,7 +58,8 @@ class FormResource extends JsonResource 'is_closed' => $this->is_closed, 'is_password_protected' => false, 'has_password' => $this->has_password, - 'max_number_of_submissions_reached' => $this->max_number_of_submissions_reached + 'max_number_of_submissions_reached' => $this->max_number_of_submissions_reached, + 'form_pending_submission_key' => $this->form_pending_submission_key ]); } diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.php index 11b61c9..2d8195b 100644 --- a/app/Models/Forms/Form.php +++ b/app/Models/Forms/Form.php @@ -166,6 +166,14 @@ class Form extends Model return ($this->closes_at && now()->gt($this->closes_at)); } + public function getFormPendingSubmissionKeyAttribute() + { + if ($this->updated_at?->timestamp) { + return "openform-" . $this->id . "-pending-submission-" . substr($this->updated_at?->timestamp, -6); + } + return null; + } + public function getMaxNumberOfSubmissionsReachedAttribute() { return ($this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count); diff --git a/resources/js/components/open/forms/OpenCompleteForm.vue b/resources/js/components/open/forms/OpenCompleteForm.vue index 33e9d38..878fdd2 100644 --- a/resources/js/components/open/forms/OpenCompleteForm.vue +++ b/resources/js/components/open/forms/OpenCompleteForm.vue @@ -191,6 +191,9 @@ export default { workspace_id: this.form.workspace_id, form_id: this.form.id }) + + window.localStorage.removeItem(this.form.form_pending_submission_Key) + if (response.data.redirect && response.data.redirect_url) { window.location.href = response.data.redirect_url } diff --git a/resources/js/components/open/forms/OpenForm.vue b/resources/js/components/open/forms/OpenForm.vue index 9481e92..212a54d 100644 --- a/resources/js/components/open/forms/OpenForm.vue +++ b/resources/js/components/open/forms/OpenForm.vue @@ -217,7 +217,15 @@ export default { handler () { this.formVersionId++ } - } + }, + dataForm: { + deep: true, + handler () { + if(this.isPublicFormPage && this.form && this.dataFormValue){ + window.localStorage.setItem(this.form.form_pending_submission_Key, JSON.stringify(this.dataFormValue)) + } + } + }, }, mounted () { @@ -266,6 +274,14 @@ export default { } }, initForm () { + if (this.isPublicFormPage) { + const pendingData = window.localStorage.getItem(this.form.form_pending_submission_Key) + if(pendingData !== null && pendingData){ + this.dataForm = new Form(JSON.parse(pendingData)) + return + } + } + const formData = clonedeep(this.dataForm ? this.dataForm.data() : {}) let urlPrefill = null if (this.isPublicFormPage && this.form.is_pro) {