Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember input in LocalStorage #1382

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/views/Submit.vue
Original file line number Diff line number Diff line change
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,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
Expand Down Expand Up @@ -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 })
Expand Down