feat: SW-693 Moved borwser storage code in useEffect of component

This commit is contained in:
Hrishikesh Vaipurkar
2024-10-30 13:33:54 +01:00
parent 88cfde15a6
commit 726a57c515
3 changed files with 17 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ import {
FocusEvent, FocusEvent,
FormEvent, FormEvent,
useCallback, useCallback,
useEffect,
useReducer, useReducer,
} from "react" } from "react"
import { useFormContext, useWatch } from "react-hook-form" import { useFormContext, useWatch } from "react-hook-form"
@@ -28,10 +29,9 @@ export default function Search({ locations }: SearchProps) {
const { register, setValue, trigger } = useFormContext<BookingWidgetSchema>() const { register, setValue, trigger } = useFormContext<BookingWidgetSchema>()
const intl = useIntl() const intl = useIntl()
const value = useWatch({ name }) const value = useWatch({ name })
const searchData = useWatch({ name: "location" })
const [state, dispatch] = useReducer( const [state, dispatch] = useReducer(
reducer, reducer,
{ defaultLocations: locations, searchData }, { defaultLocations: locations },
init 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 ( return (
<Downshift <Downshift
initialSelectedItem={state.searchData} initialSelectedItem={state.searchData}

View File

@@ -4,33 +4,18 @@ import {
type InitState, type InitState,
type State, type State,
} from "@/types/components/form/bookingwidget" } from "@/types/components/form/bookingwidget"
import type { Location, Locations } from "@/types/trpc/routers/hotel/locations" import type { Locations } from "@/types/trpc/routers/hotel/locations"
export const localStorageKey = "searchHistory" export const localStorageKey = "searchHistory"
export function getSearchHistoryFromLocalStorage() {
if (typeof window !== "undefined") {
const storageSearchHistory = window.localStorage.getItem(localStorageKey)
if (storageSearchHistory) {
const parsedStorageSearchHistory: Locations =
JSON.parse(storageSearchHistory)
if (parsedStorageSearchHistory?.length) {
return parsedStorageSearchHistory
}
}
}
return null
}
export const sessionStorageKey = "searchData" export const sessionStorageKey = "searchData"
export function init(initState: InitState): State { export function init(initState: InitState): State {
const searchHistory = getSearchHistoryFromLocalStorage()
return { return {
defaultLocations: initState.defaultLocations, defaultLocations: initState.defaultLocations,
locations: [], locations: [],
search: "", search: "",
searchData: initState.searchData, searchData: undefined,
searchHistory, searchHistory: null,
} }
} }

View File

@@ -60,5 +60,4 @@ export interface State {
searchHistory: Locations | null searchHistory: Locations | null
} }
export interface InitState export interface InitState extends Pick<State, "defaultLocations"> {}
extends Pick<State, "defaultLocations" | "searchData"> {}