From 88cfde15a6f3cebad2ef2482e7f59638ce95a5f7 Mon Sep 17 00:00:00 2001 From: Hrishikesh Vaipurkar Date: Fri, 25 Oct 2024 13:27:16 +0200 Subject: [PATCH] fix: SW-693 Moved sessionstorage usage in useEffect --- components/BookingWidget/Client.tsx | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/components/BookingWidget/Client.tsx b/components/BookingWidget/Client.tsx index af4bf4c48..95f113242 100644 --- a/components/BookingWidget/Client.tsx +++ b/components/BookingWidget/Client.tsx @@ -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({ 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 (