From b506c4a5084b1cee3e0d7ac69c09c9e9e2cd5d6c 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 | 48 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/views/Submit.vue b/src/views/Submit.vue index f6eb67af1..504b4f033 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,20 @@ 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 + } + localStorage.removeItem(`nextcloud_forms_${this.publicView ? this.shareHash : this.hash}`) + }, /* * Methods for catching unwanted unload events @@ -352,6 +397,7 @@ export default { shareHash: this.shareHash, }) this.success = true + this.deleteFormFieldFromLocalStorage() emit('forms:last-updated:set', this.form.id) } catch (error) { logger.error('Error while submitting the form', { error })