fix: SW-693 Moved sessionstorage usage in useEffect

This commit is contained in:
Hrishikesh Vaipurkar
2024-10-25 13:27:16 +02:00
parent 11e94f42ba
commit 88cfde15a6

View File

@@ -36,13 +36,6 @@ export default function BookingWidgetClient({
name: StickyElementNameEnum.BOOKING_WIDGET,
})
const sessionStorageSearchData =
typeof window !== "undefined"
? sessionStorage.getItem("searchData")
: undefined
const initialSelectedLocation: Location | undefined = sessionStorageSearchData
? JSON.parse(sessionStorageSearchData)
: undefined
const bookingWidgetSearchData: BookingWidgetSearchParams | undefined =
searchParams
@@ -85,12 +78,10 @@ export default function BookingWidgetClient({
const methods = useForm<BookingWidgetSchema>({
defaultValues: {
search: selectedLocation?.name ?? initialSelectedLocation?.name ?? "",
search: selectedLocation?.name ?? "",
location: selectedLocation
? JSON.stringify(selectedLocation)
: sessionStorageSearchData
? encodeURIComponent(sessionStorageSearchData)
: undefined,
: undefined,
date: {
// UTC is required to handle requests from far away timezones https://scandichotels.atlassian.net/browse/SWAP-6375 & PET-507
// This is specifically to handle timezones falling in different dates.
@@ -146,6 +137,22 @@ export default function BookingWidgetClient({
}
}, [])
useEffect(() => {
const sessionStorageSearchData =
typeof window !== "undefined"
? sessionStorage.getItem("searchData")
: undefined
const initialSelectedLocation: Location | undefined =
sessionStorageSearchData
? JSON.parse(sessionStorageSearchData)
: undefined
!(selectedLocation?.name) && initialSelectedLocation?.name &&
methods.setValue("search", initialSelectedLocation.name)
!selectedLocation && sessionStorageSearchData &&
methods.setValue("location", encodeURIComponent(sessionStorageSearchData))
}, [methods, selectedLocation])
return (
<FormProvider {...methods}>
<section ref={bookingWidgetRef} className={styles.containerDesktop}>