diff --git a/apps/scandic-web/components/BookingWidget/Client.tsx b/apps/scandic-web/components/BookingWidget/Client.tsx index 90abd9a31..265dafc3f 100644 --- a/apps/scandic-web/components/BookingWidget/Client.tsx +++ b/apps/scandic-web/components/BookingWidget/Client.tsx @@ -97,7 +97,7 @@ export default function BookingWidgetClient({ const defaultRoomsData: BookingWidgetSchema["rooms"] = params.rooms?.map( (room) => ({ adults: room.adults, - childrenInRoom: room.childrenInRoom ?? [], + childrenInRoom: room.childrenInRoom, }) ) ?? [ { @@ -149,21 +149,18 @@ export default function BookingWidgetClient({ } useEffect(() => { - const debouncedResizeHandler = debounce(function ([ - entry, - ]: ResizeObserverEntry[]) { - if (entry.contentRect.width > 1366) { - closeMobileSearch() - } - }) - const observer = new ResizeObserver(debouncedResizeHandler) + const observer = new ResizeObserver( + debounce(([entry]) => { + if (entry.contentRect.width > 1366) { + closeMobileSearch() + } + }) + ) observer.observe(document.body) return () => { - if (observer) { - observer.unobserve(document.body) - } + observer.unobserve(document.body) } }, []) @@ -176,13 +173,15 @@ export default function BookingWidgetClient({ ? JSON.parse(sessionStorageSearchData) : undefined - initialSelectedLocation?.name && + if (initialSelectedLocation?.name) { methods.setValue("search", initialSelectedLocation.name) - sessionStorageSearchData && + } + if (sessionStorageSearchData) { methods.setValue( "location", encodeURIComponent(sessionStorageSearchData) ) + } } }, [methods, selectedLocation]) diff --git a/apps/scandic-web/components/BookingWidget/MobileToggleButton/index.tsx b/apps/scandic-web/components/BookingWidget/MobileToggleButton/index.tsx index 57950b27b..33619c475 100644 --- a/apps/scandic-web/components/BookingWidget/MobileToggleButton/index.tsx +++ b/apps/scandic-web/components/BookingWidget/MobileToggleButton/index.tsx @@ -27,21 +27,23 @@ export default function MobileToggleButton({ }: BookingWidgetToggleButtonProps) { const intl = useIntl() const lang = useLang() - const d = useWatch({ name: "date" }) - const location = useWatch({ name: "location" }) - const rooms: BookingWidgetSchema["rooms"] = useWatch({ name: "rooms" }) + const date = useWatch({ name: "date" }) + const location = useWatch({ + name: "location", + }) + const rooms = useWatch({ name: "rooms" }) const parsedLocation: Location | null = location && isValidJson(location) ? JSON.parse(decodeURIComponent(location)) : null - const selectedFromDate = dt(d.fromDate).locale(lang).format("D MMM") - const selectedToDate = dt(d.toDate).locale(lang).format("D MMM") + const selectedFromDate = dt(date.fromDate).locale(lang).format("D MMM") + const selectedToDate = dt(date.toDate).locale(lang).format("D MMM") - const locationAndDateIsSet = parsedLocation && d + const locationAndDateIsSet = parsedLocation && date - const totalNights = dt(d.toDate).diff(dt(d.fromDate), "days") + const totalNights = dt(date.toDate).diff(dt(date.fromDate), "days") const totalRooms = rooms.length const totalAdults = rooms.reduce((acc, room) => { if (room.adults) { diff --git a/apps/scandic-web/components/Forms/BookingWidget/FormContent/Search/index.tsx b/apps/scandic-web/components/Forms/BookingWidget/FormContent/Search/index.tsx index b0f9b5882..d29c6cd3a 100644 --- a/apps/scandic-web/components/Forms/BookingWidget/FormContent/Search/index.tsx +++ b/apps/scandic-web/components/Forms/BookingWidget/FormContent/Search/index.tsx @@ -27,15 +27,13 @@ import { type SetStorageData, } from "@/types/components/form/bookingwidget" import type { SearchHistoryItem, SearchProps } from "@/types/components/search" -import type { Location, Locations } from "@/types/trpc/routers/hotel/locations" - -const name = "search" +import type { Location } from "@/types/trpc/routers/hotel/locations" export default function Search({ locations, handlePressEnter }: SearchProps) { const { register, setValue, unregister, getValues } = useFormContext() const intl = useIntl() - const value = useWatch({ name }) + const value = useWatch({ name: "search" }) const locationString = getValues("location") const location = locationString && isValidJson(decodeURIComponent(locationString)) @@ -75,7 +73,7 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { evt: FormEvent | ChangeEvent ) { const newValue = evt.currentTarget.value - setValue(name, newValue) + setValue("search", newValue) dispatchInputValue(value) } @@ -97,7 +95,7 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { const stringified = JSON.stringify(selectedItem) setValue("location", encodeURIComponent(stringified)) sessionStorage.setItem(sessionStorageKey, stringified) - setValue(name, selectedItem.name) + setValue("search", selectedItem.name) const newHistoryItem: SearchHistoryItem = { type: selectedItem.type, id: selectedItem.id, @@ -114,7 +112,7 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { const searchHistory = [newHistoryItem, ...oldHistoryItems] localStorage.setItem(localStorageKey, JSON.stringify(searchHistory)) - const enhancedSearchHistory: Locations = [ + const enhancedSearchHistory: Location[] = [ ...getEnhancedSearchHistory([newHistoryItem], locations), ...oldSearchHistoryWithoutTheNew, ] @@ -213,7 +211,10 @@ export default function Search({ locations, handlePressEnter }: SearchProps) { // Adding hidden input to define hotel or city based on destination selection for basic form submit. ) : null} -