Files
web/apps/scandic-web/components/Forms/BookingWidget/index.tsx
Joakim Jäderberg 213bd2c561 Merged in fix/SW-2427-floating-booking-widget-fullwidth (pull request #1826)
fix: when floating booking widget is sticky let it have white background all the way out. SW-2427

* fix: when floating booking widget is sticky let it have white background all the way out.
Resolves SW-2427

* pr: move styling variants to it's own file


Approved-by: Christian Andolf
2025-04-17 08:56:24 +00:00

100 lines
2.7 KiB
TypeScript

"use client"
import { useRouter } from "next/navigation"
import { useTransition } from "react"
import { Form as FormRAC } from "react-aria-components"
import { useFormContext } from "react-hook-form"
import { REDEMPTION } from "@/constants/booking"
import { selectHotel, selectRate } from "@/constants/routes/hotelReservation"
import useLang from "@/hooks/useLang"
import { convertObjToSearchParams } 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 lang = useLang()
const [isPending, startTransition] = useTransition()
const classNames = bookingWidgetVariants({
type,
})
const { handleSubmit, setValue } = useFormContext<BookingWidgetSchema>()
function onSubmit(data: BookingWidgetSchema) {
const type = data.city?.length ? "city" : "hotel"
const bookingFlowPage =
type === "city" ? selectHotel(lang) : selectRate(lang)
const bookingWidgetParams = convertObjToSearchParams({
rooms: data.rooms,
...data.date,
...(type === "city" ? { city: data.city } : { 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)
localStorage.removeItem("bookingCode")
} else if (data.bookingCode?.remember) {
localStorage.setItem("bookingCode", JSON.stringify(data.bookingCode))
}
}
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>
)
}