feat: SW-683 SW-684 Prefill location & date in BW via query params
This commit is contained in:
@@ -10,6 +10,7 @@ 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"
|
||||
@@ -23,6 +24,7 @@ import type { Location } from "@/types/trpc/routers/hotel/locations"
|
||||
export default function BookingWidgetClient({
|
||||
locations,
|
||||
type,
|
||||
searchParams,
|
||||
}: BookingWidgetClientProps) {
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
|
||||
@@ -33,17 +35,59 @@ export default function BookingWidgetClient({
|
||||
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<BookingWidgetSchema>({
|
||||
defaultValues: {
|
||||
search: initialSelectedLocation?.name ?? "",
|
||||
location: sessionStorageSearchData
|
||||
? encodeURIComponent(sessionStorageSearchData)
|
||||
: undefined,
|
||||
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: dt().utc().format("YYYY-MM-DD"),
|
||||
toDate: dt().utc().add(1, "day").format("YYYY-MM-DD"),
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user