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 May 18, 2023
1 parent c98e97d commit 7758c4a
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/views/Submit.vue
Expand Up @@ -89,7 +89,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 @@ -158,6 +159,7 @@ export default {
maxStringLengths: loadState('forms', 'maxStringLengths'),
answerTypes,
answers: {},
formValuesForLocalStorage: [],
loading: false,
success: false,
}
Expand Down Expand Up @@ -236,6 +238,26 @@ export default {
this.fetchFullForm(this.form.id)
}
SetWindowTitle(this.formTitle)
if (localStorage.getItem(`${this.shareHash}`)) {
this.formValuesForLocalStorage = JSON.parse(localStorage.getItem(`${this.shareHash}`))
this.formValuesForLocalStorage.forEach(answer => {
if (answer) {
switch (answer?.type) {
case 'QuestionMultiple':
const answers = []

Check failure on line 248 in src/views/Submit.vue

View workflow job for this annotation

GitHub Actions / eslint node

Unexpected lexical declaration in case block
answer.value.forEach(num => {
answers.push(num.toString())
})
this.answers[answer.id] = answers
break
default:
this.answers[answer.id] = answer.value
break
}
}
})
}
},
methods: {
Expand All @@ -261,11 +283,23 @@ export default {
// Using button-click event to not bypass validity-checks and use our specified behaviour
this.$refs.submitButton.click()
},
addFormFieldToLocalStorage(question) {
this.formValuesForLocalStorage[question.id] = { id: question.id, value: this.answers[question.id], type:answerTypes[question.type].component.name }

Check failure on line 288 in src/views/Submit.vue

View workflow job for this annotation

GitHub Actions / eslint node

Missing space before value for key 'type'
const parsed = JSON.stringify(this.formValuesForLocalStorage)
localStorage.setItem(`${this.shareHash}`, parsed)
},
deleteFormFieldFromLocalStorage() {
this.formValuesForLocalStorage = []
const parsed = JSON.stringify(this.formValuesForLocalStorage)
localStorage.setItem(`${this.shareHash}`, parsed)
},
/**
* Submit the form after the browser validated it 🚀
*/
async onSubmit() {
this.deleteFormFieldFromLocalStorage()
this.loading = true
try {
Expand Down

0 comments on commit 7758c4a

Please sign in to comment.