From 726a57c5155aab85f1a536187d074de115cd2f1f Mon Sep 17 00:00:00 2001 From: Hrishikesh Vaipurkar Date: Wed, 30 Oct 2024 13:33:54 +0100 Subject: [PATCH] feat: SW-693 Moved borwser storage code in useEffect of component --- .../FormContent/Search/index.tsx | 15 +++++++++++-- .../FormContent/Search/reducer.ts | 21 +++---------------- types/components/form/bookingwidget.ts | 3 +-- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/components/Forms/BookingWidget/FormContent/Search/index.tsx b/components/Forms/BookingWidget/FormContent/Search/index.tsx index 83e56a222..51b38bd9e 100644 --- a/components/Forms/BookingWidget/FormContent/Search/index.tsx +++ b/components/Forms/BookingWidget/FormContent/Search/index.tsx @@ -5,6 +5,7 @@ import { FocusEvent, FormEvent, useCallback, + useEffect, useReducer, } from "react" import { useFormContext, useWatch } from "react-hook-form" @@ -28,10 +29,9 @@ export default function Search({ locations }: SearchProps) { const { register, setValue, trigger } = useFormContext() const intl = useIntl() const value = useWatch({ name }) - const searchData = useWatch({ name: "location" }) const [state, dispatch] = useReducer( reducer, - { defaultLocations: locations, searchData }, + { defaultLocations: locations }, init ) @@ -114,6 +114,17 @@ export default function Search({ locations }: SearchProps) { } } + useEffect(() => { + state.searchData = + typeof window !== "undefined" + ? JSON.parse(sessionStorage.getItem(sessionStorageKey) || "") + : undefined + state.searchHistory = + typeof window !== "undefined" + ? JSON.parse(localStorage.getItem(localStorageKey) || "") + : null + }, [state]) + return ( {} +export interface InitState extends Pick {}