Skip to content

Commit

Permalink
Remember input in LocalStorage
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Nov 8, 2023
1 parent c7a42ab commit 7f51e3b
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/views/Submit.vue
Expand Up @@ -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)" />
</ul>
<input ref="submitButton"
class="primary"
Expand Down Expand Up @@ -201,6 +202,13 @@ export default {
},
computed: {
formValuesForLocalStorage() {
const fromLocalStorage = localStorage.getItem(`nextcloud_forms_${this.publicView ? this.shareHash : this.hash}`)
if (fromLocalStorage) {
return JSON.parse(fromLocalStorage)
}
return {}
},
validQuestions() {
return this.form.questions.filter(question => {
// All questions must have a valid title
Expand Down Expand Up @@ -271,6 +279,7 @@ export default {
this.resetData()
// Fetch full form on change
this.fetchFullForm(this.form.id)
this.initFromLocalHost()
SetWindowTitle(this.formTitle)
},
},
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -342,6 +389,7 @@ export default {
* Submit the form after the browser validated it 🚀
*/
async onSubmit() {
this.deleteFormFieldFromLocalStorage()
this.loading = true
this.submitForm = true
Expand Down

0 comments on commit 7f51e3b

Please sign in to comment.