"use client" import { zodResolver } from "@hookform/resolvers/zod" import { useEffect, useState } from "react" import { FormProvider, useForm } from "react-hook-form" import { dt } from "@/lib/dt" import Form from "@/components/Forms/BookingWidget" import { bookingWidgetSchema } from "@/components/Forms/BookingWidget/schema" import { CloseLargeIcon } from "@/components/Icons" import { debounce } from "@/utils/debounce" import getHotelReservationQueryParams from "../HotelReservation/SelectRate/RoomSelection/utils" import MobileToggleButton from "./MobileToggleButton" import styles from "./bookingWidget.module.css" import type { BookingWidgetClientProps, BookingWidgetSchema, } from "@/types/components/bookingWidget" import type { Location } from "@/types/trpc/routers/hotel/locations" export default function BookingWidgetClient({ locations, type, searchParams, }: BookingWidgetClientProps) { const [isOpen, setIsOpen] = useState(false) const sessionStorageSearchData = typeof window !== "undefined" ? sessionStorage.getItem("searchData") : undefined const initialSelectedLocation: Location | undefined = sessionStorageSearchData ? JSON.parse(sessionStorageSearchData) : undefined const bookingWidgetSearchParams = searchParams ? new URLSearchParams(searchParams) : undefined const bookingWidgetSearchData = bookingWidgetSearchParams ? getHotelReservationQueryParams(bookingWidgetSearchParams) : undefined const getLocationObj = (destination: string): Location | undefined => { if (destination) { const location: Location | undefined = locations.find((location) => { return ( location.name.toLowerCase() === destination.toLowerCase() || //@ts-ignore (due to operaId not property error) (location.operaId && location.operaId == destination) ) }) return location } return undefined } const isDateParamValid = bookingWidgetSearchData?.fromDate && bookingWidgetSearchData?.toDate && dt(bookingWidgetSearchData?.toDate.toString()).isAfter( dt(bookingWidgetSearchData?.fromDate.toString()) ) const selectedLocation = bookingWidgetSearchData ? getLocationObj( (bookingWidgetSearchData.city ?? bookingWidgetSearchData.hotel) as string ) : undefined const methods = useForm({ defaultValues: { search: selectedLocation?.name ?? initialSelectedLocation?.name ?? "", location: selectedLocation ? JSON.stringify(selectedLocation) : sessionStorageSearchData ? encodeURIComponent(sessionStorageSearchData) : 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. fromDate: isDateParamValid ? bookingWidgetSearchData?.fromDate.toString() : dt().utc().format("YYYY-MM-DD"), toDate: isDateParamValid ? bookingWidgetSearchData?.toDate?.toString() : dt().utc().add(1, "day").format("YYYY-MM-DD"), }, bookingCode: "", redemption: false, voucher: false, rooms: [ { adults: 1, children: [], }, ], }, shouldFocusError: false, mode: "all", resolver: zodResolver(bookingWidgetSchema), reValidateMode: "onChange", }) function closeMobileSearch() { setIsOpen(false) document.body.style.overflowY = "visible" } function openMobileSearch() { setIsOpen(true) document.body.style.overflowY = "hidden" } useEffect(() => { const debouncedResizeHandler = debounce(function ([ entry, ]: ResizeObserverEntry[]) { if (entry.contentRect.width > 1366) { closeMobileSearch() } }) const observer = new ResizeObserver(debouncedResizeHandler) observer.observe(document.body) return () => { if (observer) { observer.unobserve(document.body) } } }, []) return (
) }