diff --git a/components/Forms/BookingWidget/FormContent/Search/index.tsx b/components/Forms/BookingWidget/FormContent/Search/index.tsx index 51b38bd9e..ebeda5dbc 100644 --- a/components/Forms/BookingWidget/FormContent/Search/index.tsx +++ b/components/Forms/BookingWidget/FormContent/Search/index.tsx @@ -115,14 +115,21 @@ export default function Search({ locations }: SearchProps) { } useEffect(() => { - state.searchData = + const searchData = typeof window !== "undefined" ? JSON.parse(sessionStorage.getItem(sessionStorageKey) || "") : undefined - state.searchHistory = + const searchHistory = typeof window !== "undefined" ? JSON.parse(localStorage.getItem(localStorageKey) || "") : null + dispatch({ + payload: { + searchData, + searchHistory, + }, + type: ActionType.SET_STORAGE_DATA, + }) }, [state]) return ( diff --git a/components/Forms/BookingWidget/FormContent/Search/reducer.ts b/components/Forms/BookingWidget/FormContent/Search/reducer.ts index b4a849027..4c42a2507 100644 --- a/components/Forms/BookingWidget/FormContent/Search/reducer.ts +++ b/components/Forms/BookingWidget/FormContent/Search/reducer.ts @@ -68,6 +68,13 @@ export function reducer(state: State, action: Action) { searchHistory: action.payload.searchHistory, } } + case ActionType.SET_STORAGE_DATA: { + return { + ...state, + searchData: action.payload.searchData, + searchHistory: action.payload.searchHistory, + } + } default: const unhandledActionType: never = type console.info(`Unhandled type: ${unhandledActionType}`) diff --git a/types/components/form/bookingwidget.ts b/types/components/form/bookingwidget.ts index 997890ca3..14a548952 100644 --- a/types/components/form/bookingwidget.ts +++ b/types/components/form/bookingwidget.ts @@ -21,6 +21,7 @@ export enum ActionType { CLEAR_SEARCH_LOCATIONS = "CLEAR_SEARCH_LOCATIONS", SEARCH_LOCATIONS = "SEARCH_LOCATIONS", SELECT_ITEM = "SELECT_ITEM", + SET_STORAGE_DATA = "SET_STORAGE_DATA", } interface ClearHistoryLocationsAction { @@ -46,11 +47,20 @@ interface SetItemAction { type: ActionType.SELECT_ITEM } +interface SetStorageData { + payload: { + searchData: Location + searchHistory: Locations + } + type: ActionType.SET_STORAGE_DATA +} + export type Action = | ClearHistoryLocationsAction | ClearSearchLocationsAction | SearchLocationsAction | SetItemAction + | SetStorageData export interface State { defaultLocations: Locations