feat (SW-2864): Move booking router to trpc package * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Merge branch 'master' into feat/sw-2864-move-hotels-router-to-trpc-package Approved-by: Linus Flood
115 lines
3.2 KiB
TypeScript
115 lines
3.2 KiB
TypeScript
"use client"
|
|
|
|
import { usePathname, useRouter } from "next/navigation"
|
|
import { useTransition } from "react"
|
|
import { Form as FormRAC } from "react-aria-components"
|
|
import { useFormContext } from "react-hook-form"
|
|
|
|
import { selectRate } from "@scandic-hotels/common/constants/routes/hotelReservation"
|
|
import { REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
|
|
|
|
import {
|
|
selectHotel,
|
|
selectHotelMap,
|
|
} from "@/constants/routes/hotelReservation"
|
|
|
|
import useLang from "@/hooks/useLang"
|
|
import { trackBookingSearchClick } from "@/utils/tracking/booking"
|
|
import { serializeBookingSearchParams } from "@/utils/url"
|
|
|
|
import FormContent, { BookingWidgetFormContentSkeleton } from "./FormContent"
|
|
import { bookingWidgetVariants } from "./variants"
|
|
|
|
import styles from "./form.module.css"
|
|
|
|
import type {
|
|
BookingWidgetSchema,
|
|
BookingWidgetType,
|
|
} from "@/types/components/bookingWidget"
|
|
import type { BookingWidgetFormProps } from "@/types/components/form/bookingwidget"
|
|
|
|
const formId = "booking-widget"
|
|
|
|
export default function Form({ type, onClose }: BookingWidgetFormProps) {
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
const lang = useLang()
|
|
const [isPending, startTransition] = useTransition()
|
|
|
|
const classNames = bookingWidgetVariants({
|
|
type,
|
|
})
|
|
|
|
const { handleSubmit, setValue, reset } =
|
|
useFormContext<BookingWidgetSchema>()
|
|
|
|
function onSubmit(data: BookingWidgetSchema) {
|
|
trackBookingSearchClick(data.search, data.hotel ? "hotel" : "destination")
|
|
const isMapView = pathname.endsWith("/map")
|
|
const bookingFlowPage = data.hotel
|
|
? selectRate(lang)
|
|
: isMapView
|
|
? selectHotelMap(lang)
|
|
: selectHotel(lang)
|
|
const bookingWidgetParams = serializeBookingSearchParams({
|
|
rooms: data.rooms,
|
|
...data.date,
|
|
...(data.city ? { city: data.city } : {}),
|
|
...(data.hotel ? { hotel: data.hotel } : {}),
|
|
...(data.bookingCode?.value
|
|
? { bookingCode: data.bookingCode.value }
|
|
: {}),
|
|
// Followed current url structure to keep searchtype=redemption param incase of reward night
|
|
...(data.redemption ? { searchType: REDEMPTION } : {}),
|
|
})
|
|
|
|
onClose()
|
|
startTransition(() => {
|
|
router.push(`${bookingFlowPage}?${bookingWidgetParams.toString()}`)
|
|
})
|
|
if (!data.bookingCode?.value) {
|
|
setValue("bookingCode.remember", false, {
|
|
shouldDirty: true,
|
|
})
|
|
localStorage.removeItem("bookingCode")
|
|
} else if (data.bookingCode?.remember) {
|
|
localStorage.setItem("bookingCode", JSON.stringify(data.bookingCode))
|
|
}
|
|
reset(data)
|
|
}
|
|
|
|
return (
|
|
<section className={classNames}>
|
|
<FormRAC
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
className={styles.form}
|
|
id={formId}
|
|
>
|
|
<FormContent
|
|
formId={formId}
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
isSearching={isPending}
|
|
/>
|
|
</FormRAC>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
export function BookingWidgetFormSkeleton({
|
|
type,
|
|
}: {
|
|
type: BookingWidgetType
|
|
}) {
|
|
const classNames = bookingWidgetVariants({
|
|
type,
|
|
})
|
|
|
|
return (
|
|
<section className={classNames}>
|
|
<form className={styles.form}>
|
|
<BookingWidgetFormContentSkeleton />
|
|
</form>
|
|
</section>
|
|
)
|
|
}
|