Skip to content

Commit

Permalink
fix 03-feedback.js
Browse files Browse the repository at this point in the history
  • Loading branch information
anetta999 committed Jul 2, 2023
1 parent 6bee5a3 commit a4c1f6d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/js/03-feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ form.addEventListener('input', throttle(handlerFormInput, 500));
form.addEventListener('submit', handlerFormSubmit);

const LS_KEY = 'feedback-form-state';
const data = {};
let data = JSON.parse(localStorage.getItem(LS_KEY)) || {};
populateForm();
function handlerFormInput(evt) {
data[evt.target.name] = evt.target.value;
// console.log(data);

localStorage.setItem(LS_KEY, JSON.stringify(data));

const savedData = JSON.parse(localStorage.getItem(LS_KEY));
console.log(savedData);

if (savedData) {
email.value = savedData.email;
textarea.value = savedData.message;
}
}

function handlerFormSubmit(evt) {
Expand All @@ -30,4 +23,15 @@ function handlerFormSubmit(evt) {
localStorage.removeItem(LS_KEY);

console.log(data);
data = {};
}

function populateForm() {
const savedData = JSON.parse(localStorage.getItem(LS_KEY));
console.log(savedData);

if (savedData) {
email.value = savedData.email || '';
textarea.value = savedData.message || '';
}
}

0 comments on commit a4c1f6d

Please sign in to comment.