From 7f51e3b65f084252b9031bb1ad43b94655626e0a Mon Sep 17 00:00:00 2001 From: hamza221 Date: Thu, 18 May 2023 22:09:12 +0200 Subject: [PATCH] Remember input in LocalStorage Signed-off-by: hamza221 --- src/views/Submit.vue | 50 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/views/Submit.vue b/src/views/Submit.vue index f6eb67af1..302cc780e 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -98,7 +98,8 @@ v-bind="question" :values.sync="answers[question.id]" @keydown.enter="onKeydownEnter" - @keydown.ctrl.enter="onKeydownCtrlEnter" /> + @keydown.ctrl.enter="onKeydownCtrlEnter" + @update:values="addFormFieldToLocalStorage(question)" /> { // All questions must have a valid title @@ -271,6 +279,7 @@ export default { this.resetData() // Fetch full form on change this.fetchFullForm(this.form.id) + this.initFromLocalHost() SetWindowTitle(this.formTitle) }, }, @@ -290,9 +299,31 @@ export default { this.fetchFullForm(this.form.id) } SetWindowTitle(this.formTitle) + if (this.isLoggedIn) { + this.initFromLocalHost() + } }, methods: { + initFromLocalHost() { + if (localStorage.getItem(`nextcloud_forms_${this.publicView ? this.shareHash : this.hash}`)) { + for (const key in this.formValuesForLocalStorage) { + const answer = this.formValuesForLocalStorage[key] + const answers = [] + switch (answer?.type) { + case 'QuestionMultiple': + answer.value.forEach(num => { + answers.push(num.toString()) + }) + this.answers[key] = answers + break + default: + this.answers[key] = answer.value + break + } + } + } + }, /** * On Enter, focus next form-element * Last form element is the submit button, the form submits on enter then @@ -315,6 +346,22 @@ export default { // Using button-click event to not bypass validity-checks and use our specified behaviour this.$refs.submitButton.click() }, + addFormFieldToLocalStorage(question) { + if (!this.isLoggedIn) { + return + } + this.formValuesForLocalStorage[`${question.id}`] = { value: this.answers[question.id], type: answerTypes[question.type].component.name } + const parsed = JSON.stringify(this.formValuesForLocalStorage) + localStorage.setItem(`nextcloud_forms_${this.publicView ? this.shareHash : this.hash}`, parsed) + }, + deleteFormFieldFromLocalStorage() { + if (!this.isLoggedIn) { + return + } + this.formValuesForLocalStorage = {} + const parsed = JSON.stringify(this.formValuesForLocalStorage) + localStorage.setItem(`nextcloud_forms_${this.publicView ? this.shareHash : this.hash}`, parsed) + }, /* * Methods for catching unwanted unload events @@ -342,6 +389,7 @@ export default { * Submit the form after the browser validated it 🚀 */ async onSubmit() { + this.deleteFormFieldFromLocalStorage() this.loading = true this.submitForm = true