Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
226 lines
6.6 KiB
TypeScript
226 lines
6.6 KiB
TypeScript
"use client"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { useEffect, useRef, useState } from "react"
|
|
import { FormProvider, useForm } from "react-hook-form"
|
|
|
|
import { dt } from "@/lib/dt"
|
|
import { StickyElementNameEnum } from "@/stores/sticky-position"
|
|
|
|
import Form, {
|
|
BookingWidgetFormSkeleton,
|
|
} from "@/components/Forms/BookingWidget"
|
|
import { bookingWidgetSchema } from "@/components/Forms/BookingWidget/schema"
|
|
import { CloseLargeIcon } from "@/components/Icons"
|
|
import useStickyPosition from "@/hooks/useStickyPosition"
|
|
import { debounce } from "@/utils/debounce"
|
|
import isValidJson from "@/utils/isValidJson"
|
|
import { convertSearchParamsToObj } from "@/utils/url"
|
|
|
|
import MobileToggleButton, {
|
|
MobileToggleButtonSkeleton,
|
|
} from "./MobileToggleButton"
|
|
|
|
import styles from "./bookingWidget.module.css"
|
|
|
|
import type {
|
|
BookingCodeSchema,
|
|
BookingWidgetClientProps,
|
|
BookingWidgetSchema,
|
|
BookingWidgetSearchData,
|
|
} from "@/types/components/bookingWidget"
|
|
import type { Location } from "@/types/trpc/routers/hotel/locations"
|
|
|
|
export default function BookingWidgetClient({
|
|
locations,
|
|
type,
|
|
bookingWidgetSearchParams,
|
|
}: BookingWidgetClientProps) {
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const bookingWidgetRef = useRef(null)
|
|
useStickyPosition({
|
|
ref: bookingWidgetRef,
|
|
name: StickyElementNameEnum.BOOKING_WIDGET,
|
|
})
|
|
|
|
const bookingWidgetSearchData = bookingWidgetSearchParams
|
|
? convertSearchParamsToObj<BookingWidgetSearchData>(
|
|
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 reqFromDate = bookingWidgetSearchData?.fromDate?.toString()
|
|
const reqToDate = bookingWidgetSearchData?.toDate?.toString()
|
|
|
|
const parsedFromDate = reqFromDate ? dt(reqFromDate) : undefined
|
|
const parsedToDate = reqToDate ? dt(reqToDate) : undefined
|
|
|
|
const now = dt()
|
|
|
|
const isDateParamValid =
|
|
parsedFromDate &&
|
|
parsedToDate &&
|
|
parsedFromDate.isSameOrAfter(now, "day") &&
|
|
parsedToDate.isAfter(parsedFromDate)
|
|
|
|
const selectedLocation = bookingWidgetSearchData
|
|
? getLocationObj(
|
|
(bookingWidgetSearchData.hotelId ??
|
|
bookingWidgetSearchData.city) as string
|
|
)
|
|
: undefined
|
|
|
|
const selectedBookingCode = bookingWidgetSearchData
|
|
? bookingWidgetSearchData.bookingCode
|
|
: ""
|
|
|
|
const defaultRoomsData: BookingWidgetSchema["rooms"] =
|
|
bookingWidgetSearchData?.rooms?.map((room) => ({
|
|
adults: room.adults,
|
|
childrenInRoom: room.childrenInRoom ?? [],
|
|
})) ?? [
|
|
{
|
|
adults: 1,
|
|
childrenInRoom: [],
|
|
},
|
|
]
|
|
|
|
const methods = useForm<BookingWidgetSchema>({
|
|
defaultValues: {
|
|
search: selectedLocation?.name ?? "",
|
|
location: selectedLocation ? JSON.stringify(selectedLocation) : 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
|
|
? parsedFromDate.format("YYYY-MM-DD")
|
|
: now.utc().format("YYYY-MM-DD"),
|
|
toDate: isDateParamValid
|
|
? parsedToDate.format("YYYY-MM-DD")
|
|
: now.utc().add(1, "day").format("YYYY-MM-DD"),
|
|
},
|
|
bookingCode: {
|
|
value: selectedBookingCode,
|
|
remember: false,
|
|
},
|
|
redemption: false,
|
|
voucher: false,
|
|
rooms: defaultRoomsData,
|
|
},
|
|
shouldFocusError: false,
|
|
mode: "onSubmit",
|
|
resolver: zodResolver(bookingWidgetSchema),
|
|
reValidateMode: "onSubmit",
|
|
})
|
|
|
|
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)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (typeof window !== "undefined" && !selectedLocation) {
|
|
const sessionStorageSearchData = sessionStorage.getItem("searchData")
|
|
|
|
const initialSelectedLocation: Location | undefined =
|
|
sessionStorageSearchData && isValidJson(sessionStorageSearchData)
|
|
? JSON.parse(sessionStorageSearchData)
|
|
: undefined
|
|
|
|
initialSelectedLocation?.name &&
|
|
methods.setValue("search", initialSelectedLocation.name)
|
|
sessionStorageSearchData &&
|
|
methods.setValue(
|
|
"location",
|
|
encodeURIComponent(sessionStorageSearchData)
|
|
)
|
|
}
|
|
}, [methods, selectedLocation])
|
|
|
|
useEffect(() => {
|
|
if (typeof window !== "undefined" && !selectedBookingCode) {
|
|
const storedBookingCode = localStorage.getItem("bookingCode")
|
|
const initialBookingCode: BookingCodeSchema | undefined =
|
|
storedBookingCode && isValidJson(storedBookingCode)
|
|
? JSON.parse(storedBookingCode)
|
|
: undefined
|
|
|
|
initialBookingCode?.remember &&
|
|
methods.setValue("bookingCode", initialBookingCode)
|
|
}
|
|
}, [methods, selectedBookingCode])
|
|
|
|
return (
|
|
<FormProvider {...methods}>
|
|
<section
|
|
ref={bookingWidgetRef}
|
|
className={styles.wrapper}
|
|
data-open={isOpen}
|
|
>
|
|
<MobileToggleButton openMobileSearch={openMobileSearch} />
|
|
<div className={styles.formContainer}>
|
|
<button
|
|
className={styles.close}
|
|
onClick={closeMobileSearch}
|
|
type="button"
|
|
>
|
|
<CloseLargeIcon />
|
|
</button>
|
|
<Form locations={locations} type={type} onClose={closeMobileSearch} />
|
|
</div>
|
|
</section>
|
|
<div className={styles.backdrop} onClick={closeMobileSearch} />
|
|
</FormProvider>
|
|
)
|
|
}
|
|
|
|
export function BookingWidgetSkeleton() {
|
|
return (
|
|
<>
|
|
<section className={styles.wrapper} style={{ top: 0 }}>
|
|
<MobileToggleButtonSkeleton />
|
|
<div className={styles.formContainer}>
|
|
<BookingWidgetFormSkeleton />
|
|
</div>
|
|
</section>
|
|
</>
|
|
)
|
|
}
|