From a3902ea92a147d46491b9cad1a40df11b12f69e6 Mon Sep 17 00:00:00 2001 From: GeorgiySergeev <126503204+GeorgiySergeev@users.noreply.github.com> Date: Mon, 24 Jul 2023 10:41:00 +0300 Subject: [PATCH] corrections --- src/js/02-video.js | 8 ++++---- src/js/03-feedback.js | 37 ++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/js/02-video.js b/src/js/02-video.js index b1fdcfa..dba5e79 100644 --- a/src/js/02-video.js +++ b/src/js/02-video.js @@ -3,16 +3,16 @@ import Player from '@vimeo/player'; import throttle from 'lodash.throttle'; const iframe = document.getElementById('vimeo-player'); -const player = new Player(iframe);// создаем функцию прототипом которой есть ф-я Плеер - +const player = new Player(iframe); +const LOCAL_STORAGE_KEY = "videoplayer-current-time" player.on( 'timeupdate', throttle(a => { - localStorage.setItem('videoplayer-current-time', a.seconds); + localStorage.setItem(LOCAL_STORAGE_KEY, a.seconds); }, 1000) ); -let currentTime = localStorage.getItem('videoplayer-current-time'); +let currentTime = localStorage.getItem(LOCAL_STORAGE_KEY); player .setCurrentTime(currentTime) diff --git a/src/js/03-feedback.js b/src/js/03-feedback.js index 7ed46e7..848b0a8 100644 --- a/src/js/03-feedback.js +++ b/src/js/03-feedback.js @@ -1,37 +1,36 @@ +import { keys } from '@vimeo/player'; import throttle from 'lodash.throttle'; -//1.refs const formEl = document.querySelector('.feedback-form'); -const inputEl = document.querySelector('input[name="email"]'); -const messageEl = document.querySelector('textarea[name="message"]'); +let feedbackFormState = {}; +const STORAGE_KEY = 'feedback-form-state'; formEl.addEventListener('input', throttle(onInput, 500)); -function onInput() { - const storageObject = { email: inputEl.value, message: messageEl.value }; - - localStorage.setItem('feedback-form-state', JSON.stringify(storageObject)); +function onInput(e) { + feedbackFormState[e.target.name] = e.target.value.trim(); + localStorage.setItem(STORAGE_KEY, JSON.stringify(feedbackFormState)); } formEl.addEventListener('submit', evt => { evt.preventDefault(); - console.log(JSON.parse(localStorage.getItem('feedback-form-state'))); - localStorage.clear(); + console.log(feedbackFormState); + feedbackFormState = {}; + localStorage.removeItem(STORAGE_KEY); evt.target.reset(); }); -const load = key => { +const onLoad = () => { try { - const serializedState = localStorage.getItem(key); - return serializedState === null ? undefined : JSON.parse(serializedState); + const data = localStorage.getItem(STORAGE_KEY); + if (!data) return; + feedbackFormState = JSON.parse(data); + Object.entries(feedbackFormState).forEach(([key, val]) => { + formEl.elements[key].value = val; + }); } catch (error) { - console.error('Get state error: ', error.message); + console.log(error.message); } }; - -const storageData = load('feedback-form-state'); -if (storageData) { - inputEl.value = storageData.email; - messageEl.value = storageData.message; -} +window.addEventListener('load', onLoad);