"use client" import { useSearchParams } from "next/navigation" import { useEffect, useRef } from "react" import { useIntl } from "react-intl" import { CheckIcon, InfoCircleIcon } from "@/components/Icons" import Modal from "@/components/Modal" import Button from "@/components/TempDesignSystem/Button" import Label from "@/components/TempDesignSystem/Form/Label" import Body from "@/components/TempDesignSystem/Text/Body" import Caption from "@/components/TempDesignSystem/Text/Caption" import PriceTable from "./PriceList" import styles from "./flexibilityOption.module.css" import type { FlexibilityOptionProps } from "@/types/components/hotelReservation/selectRate/flexibilityOption" export default function FlexibilityOption({ product, name, paymentTerm, priceInformation, roomTypeCode, petRoomPackage, handleSelectRate, roomListIndex, }: FlexibilityOptionProps) { const intl = useIntl() const inputElementRef = useRef(null) const handleSelectRateRef = useRef(handleSelectRate) useEffect(() => { handleSelectRateRef.current = handleSelectRate }, [handleSelectRate]) const searchParams = useSearchParams() // When entering the page with a room and rate selection already in the URL we // want to preselect the selection. This happens e.g. when you do a selection, // go to the enter details page and then want to change the room. useEffect(() => { const ratecodeSearchParam = searchParams.get( `room[${roomListIndex}].ratecode` ) const roomtypeSearchParam = searchParams.get( `room[${roomListIndex}].roomtype` ) // If this is not the room and rate we want to preselect, abort if ( !product || ratecodeSearchParam !== product.productType.public.rateCode || roomtypeSearchParam !== roomTypeCode ) { return } handleSelectRateRef.current((prev) => { // If the user already has made a new selection we respect that and don't do anything else if (prev) { return prev } if (inputElementRef.current) { inputElementRef.current.checked = true } return { publicRateCode: product.productType.public.rateCode, roomTypeCode: roomTypeCode, name: name, paymentTerm: paymentTerm, } }) }, [searchParams, roomListIndex, product, roomTypeCode, name, paymentTerm]) if (!product) { return (
{name} ({paymentTerm})
) } const { public: publicPrice, member: memberPrice } = product.productType const onClick: React.MouseEventHandler = (e) => { handleSelectRateRef.current((prev) => { if ( prev && prev.publicRateCode === publicPrice.rateCode && prev.roomTypeCode === roomTypeCode ) { if (e.currentTarget?.checked) e.currentTarget.checked = false return undefined } else return { publicRateCode: publicPrice.rateCode, roomTypeCode: roomTypeCode, name: name, paymentTerm: paymentTerm, } }) } return ( ) }