From b884ae8fb7b3d208912f75decfe941435c59d571 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 18 Oct 2024 11:25:02 +0700 Subject: save to local storage --- .../pengajuan-tempo/component/PengajuanTempo.jsx | 72 +++++++++++++++++----- .../component/informasiPerusahaan.jsx | 8 +++ 2 files changed, 65 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx index b2e9832e..bdd6e6ef 100644 --- a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx +++ b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx @@ -8,7 +8,8 @@ import { usePengajuanTempoStore, usePengajuanTempoStoreKontakPerson, } from '../../../../src-migrate/modules/register/stores/usePengajuanTempoStore'; -import { ChevronRightIcon } from '@heroicons/react/24/outline'; +import { ChevronRightIcon, ChevronLeftIcon } from '@heroicons/react/24/outline'; +import { chakra } from '@chakra-ui/react'; const PengajuanTempo = () => { const [currentStep, setCurrentStep] = React.useState(0); const NUMBER_OF_STEPS = 6; @@ -22,11 +23,11 @@ const PengajuanTempo = () => { const [notValid, setNotValid] = useState(false); const [buttonSubmitClick, setButtonSubmitClick] = useState(false); const stepDivs = [ - , - , @@ -37,14 +38,31 @@ const PengajuanTempo = () => {
Konfirmasi
, ]; const stepDivsError = [ - errorsKontakPerson, errors, + errorsKontakPerson, +
Kontak Person
, +
Pengiriman
, +
Referensi
, +
Dokumen
, +
Konfirmasi
, + ]; + const stepDivsForm = [ + form, + formKontakPerson,
Kontak Person
,
Pengiriman
,
Referensi
,
Dokumen
,
Konfirmasi
, ]; + const stepLabels = [ + 'informasi_perusahaan', + 'kontak_person', + 'Pengiriman', + 'Referensi', + 'Dokumen', + 'Konfirmasi', + ]; const isFormValid = useMemo( () => Object.keys(stepDivsError[currentStep]).length === 0, @@ -54,16 +72,18 @@ const PengajuanTempo = () => { validate(); validateKontakPerson(); }, []); - console.log('isFormValid', isFormValid); const goToNextStep = () => { if (!isFormValid) { setNotValid(true); setButtonSubmitClick(!buttonSubmitClick); - console.log('form', form); + console.log('form', stepDivsForm[currentStep]); console.log('error', stepDivsError[currentStep]); return; } else { - console.log('form', form); + saveToLocalStorage(stepLabels[currentStep], stepDivsForm[currentStep]); + const cachedData = getFromLocalStorage(stepLabels[currentStep]); + console.log('cachedData', cachedData); + console.log('form', stepDivsForm[currentStep]); console.log('error', stepDivsError[currentStep]); setButtonSubmitClick(!buttonSubmitClick); setNotValid(false); @@ -71,8 +91,29 @@ const PengajuanTempo = () => { setCurrentStep((prev) => (prev === NUMBER_OF_STEPS - 1 ? prev : prev + 1)); }; - const goToPreviousStep = () => + const goToPreviousStep = () => { + const cachedData = getFromLocalStorage(stepLabels[currentStep - 1]); + console.log('cachedData prev button', cachedData); + if (cachedData) { + // const formData = JSON.parse(cachedData); + Object.keys(cachedData).forEach((key) => { + updateForm(key, cachedData[key]); + }); + } setCurrentStep((prev) => (prev <= 0 ? prev : prev - 1)); + }; + + const saveToLocalStorage = (key, form) => { + localStorage.setItem(key, JSON.stringify(form)); + }; + + const getFromLocalStorage = (key) => { + const itemStr = localStorage.getItem(key); + if (!itemStr) return null; + + const item = JSON.parse(itemStr); + return item; + }; return ( <> @@ -94,13 +135,6 @@ const PengajuanTempo = () => {
{stepDivs[currentStep]}
- {/* */} {/*