"use client" import { zodResolver } from "@hookform/resolvers/zod" import { useCallback, useEffect } from "react" import { FormProvider, useForm } from "react-hook-form" import { useIntl } from "react-intl" import { useDetailsStore } from "@/stores/details" import { useStepsStore } from "@/stores/steps" import { Highlight } from "@/components/TempDesignSystem/Form/ChoiceCard/_Card" import RadioCard from "@/components/TempDesignSystem/Form/ChoiceCard/Radio" import { breakfastFormSchema } from "./schema" import styles from "./breakfast.module.css" import type { BreakfastFormSchema, BreakfastProps, } from "@/types/components/hotelReservation/enterDetails/breakfast" import { BreakfastPackageEnum } from "@/types/enums/breakfast" export default function Breakfast({ packages }: BreakfastProps) { const intl = useIntl() const breakfast = useDetailsStore(({ data }) => data.breakfast ? data.breakfast.code : data.breakfast === false ? "false" : data.breakfast ) const updateBreakfast = useDetailsStore( (state) => state.actions.updateBreakfast ) const completeStep = useStepsStore((state) => state.completeStep) const methods = useForm({ defaultValues: breakfast ? { breakfast } : undefined, criteriaMode: "all", mode: "all", resolver: zodResolver(breakfastFormSchema), reValidateMode: "onChange", }) const onSubmit = useCallback( (values: BreakfastFormSchema) => { const pkg = packages?.find((p) => p.code === values.breakfast) if (pkg) { updateBreakfast(pkg) } else { updateBreakfast(false) } completeStep() }, [completeStep, packages, updateBreakfast] ) useEffect(() => { if (methods.formState.isSubmitting) { return } const subscription = methods.watch(() => methods.handleSubmit(onSubmit)()) return () => subscription.unsubscribe() }, [methods, onSubmit]) return (
{packages.map((pkg) => ( ( { id: "breakfast.price.free" }, { amount: pkg.localPrice.price, currency: pkg.localPrice.currency, free: (str) => {str}, strikethrough: (str) => {str}, } ) : intl.formatMessage( { id: "breakfast.price" }, { amount: pkg.localPrice.price, currency: pkg.localPrice.currency, } ) } text={intl.formatMessage({ id: "All our breakfast buffets offer gluten free, vegan, and allergy-friendly options.", })} title={intl.formatMessage({ id: "Breakfast buffet" })} value={pkg.code} handleSelectedOnClick={ breakfast === pkg.code ? completeStep : undefined } /> ))}
) }