fix: SW-693 Fixed hydration error
This commit is contained in:
@@ -25,14 +25,15 @@ import type { Location } from "@/types/trpc/routers/hotel/locations"
|
|||||||
|
|
||||||
const name = "search"
|
const name = "search"
|
||||||
export default function Search({ locations }: SearchProps) {
|
export default function Search({ locations }: SearchProps) {
|
||||||
const [state, dispatch] = useReducer(
|
|
||||||
reducer,
|
|
||||||
{ defaultLocations: locations },
|
|
||||||
init
|
|
||||||
)
|
|
||||||
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(
|
||||||
|
reducer,
|
||||||
|
{ defaultLocations: locations, searchData },
|
||||||
|
init
|
||||||
|
)
|
||||||
|
|
||||||
const handleMatchLocations = useCallback(
|
const handleMatchLocations = useCallback(
|
||||||
function (searchValue: string) {
|
function (searchValue: string) {
|
||||||
|
|||||||
@@ -22,27 +22,14 @@ export function getSearchHistoryFromLocalStorage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const sessionStorageKey = "searchData"
|
export const sessionStorageKey = "searchData"
|
||||||
export function getSearchDataFromSessionStorage() {
|
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
const storageSearchData = window.sessionStorage.getItem(sessionStorageKey)
|
|
||||||
if (storageSearchData) {
|
|
||||||
const parsedStorageSearchData: Location = JSON.parse(storageSearchData)
|
|
||||||
if (parsedStorageSearchData) {
|
|
||||||
return parsedStorageSearchData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
export function init(initState: InitState): State {
|
export function init(initState: InitState): State {
|
||||||
const searchHistory = getSearchHistoryFromLocalStorage()
|
const searchHistory = getSearchHistoryFromLocalStorage()
|
||||||
const searchData = getSearchDataFromSessionStorage()
|
|
||||||
return {
|
return {
|
||||||
defaultLocations: initState.defaultLocations,
|
defaultLocations: initState.defaultLocations,
|
||||||
locations: [],
|
locations: [],
|
||||||
search: "",
|
search: "",
|
||||||
searchData,
|
searchData: initState.searchData,
|
||||||
searchHistory,
|
searchHistory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,4 +60,5 @@ export interface State {
|
|||||||
searchHistory: Locations | null
|
searchHistory: Locations | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InitState extends Pick<State, "defaultLocations"> {}
|
export interface InitState
|
||||||
|
extends Pick<State, "defaultLocations" | "searchData"> {}
|
||||||
|
|||||||
Reference in New Issue
Block a user