145 lines
4.6 KiB
TypeScript
145 lines
4.6 KiB
TypeScript
"use client"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { useCallback } 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 Button from "@/components/TempDesignSystem/Button"
|
|
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
|
|
import Input from "@/components/TempDesignSystem/Form/Input"
|
|
import Phone from "@/components/TempDesignSystem/Form/Phone"
|
|
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
|
|
|
|
import { guestDetailsSchema, signedInDetailsSchema } from "./schema"
|
|
import Signup from "./Signup"
|
|
|
|
import styles from "./details.module.css"
|
|
|
|
import type {
|
|
DetailsProps,
|
|
DetailsSchema,
|
|
} from "@/types/components/hotelReservation/enterDetails/details"
|
|
|
|
const formID = "enter-details"
|
|
export default function Details({ user }: DetailsProps) {
|
|
const intl = useIntl()
|
|
const initialData = useDetailsStore((state) => ({
|
|
countryCode: state.data.countryCode,
|
|
email: state.data.email,
|
|
firstName: state.data.firstName,
|
|
lastName: state.data.lastName,
|
|
phoneNumber: state.data.phoneNumber,
|
|
join: state.data.join,
|
|
dateOfBirth: state.data.dateOfBirth,
|
|
zipCode: state.data.zipCode,
|
|
termsAccepted: state.data.termsAccepted,
|
|
membershipNo: state.data.membershipNo,
|
|
}))
|
|
|
|
const updateDetails = useDetailsStore((state) => state.actions.updateDetails)
|
|
const completeStep = useStepsStore((state) => state.completeStep)
|
|
|
|
const methods = useForm<DetailsSchema>({
|
|
defaultValues: {
|
|
countryCode: user?.address?.countryCode ?? initialData.countryCode,
|
|
email: user?.email ?? initialData.email,
|
|
firstName: user?.firstName ?? initialData.firstName,
|
|
lastName: user?.lastName ?? initialData.lastName,
|
|
phoneNumber: user?.phoneNumber ?? initialData.phoneNumber,
|
|
join: initialData.join,
|
|
dateOfBirth: initialData.dateOfBirth,
|
|
zipCode: initialData.zipCode,
|
|
termsAccepted: initialData.termsAccepted,
|
|
membershipNo: initialData.membershipNo,
|
|
},
|
|
criteriaMode: "all",
|
|
mode: "all",
|
|
resolver: zodResolver(user ? signedInDetailsSchema : guestDetailsSchema),
|
|
reValidateMode: "onChange",
|
|
})
|
|
|
|
const onSubmit = useCallback(
|
|
(values: DetailsSchema) => {
|
|
updateDetails(values)
|
|
completeStep()
|
|
},
|
|
[completeStep, updateDetails]
|
|
)
|
|
|
|
return (
|
|
<FormProvider {...methods}>
|
|
<form
|
|
className={styles.form}
|
|
id={formID}
|
|
onSubmit={methods.handleSubmit(onSubmit)}
|
|
>
|
|
{user ? null : <Signup name="join" />}
|
|
<Footnote
|
|
color="uiTextHighContrast"
|
|
textTransform="uppercase"
|
|
type="label"
|
|
>
|
|
{intl.formatMessage({ id: "Guest information" })}
|
|
</Footnote>
|
|
<div className={styles.container}>
|
|
<Input
|
|
label={intl.formatMessage({ id: "First name" })}
|
|
name="firstName"
|
|
readOnly={!!user}
|
|
registerOptions={{ required: true }}
|
|
/>
|
|
<Input
|
|
label={intl.formatMessage({ id: "Last name" })}
|
|
name="lastName"
|
|
readOnly={!!user}
|
|
registerOptions={{ required: true }}
|
|
/>
|
|
<CountrySelect
|
|
className={styles.country}
|
|
label={intl.formatMessage({ id: "Country" })}
|
|
name="countryCode"
|
|
readOnly={!!user}
|
|
registerOptions={{ required: true }}
|
|
/>
|
|
<Input
|
|
className={styles.email}
|
|
label={intl.formatMessage({ id: "Email address" })}
|
|
name="email"
|
|
readOnly={!!user}
|
|
registerOptions={{ required: true }}
|
|
/>
|
|
<Phone
|
|
className={styles.phone}
|
|
label={intl.formatMessage({ id: "Phone number" })}
|
|
name="phoneNumber"
|
|
readOnly={!!user}
|
|
registerOptions={{ required: true }}
|
|
/>
|
|
{user || methods.watch("join") ? null : (
|
|
<Input
|
|
className={styles.membershipNo}
|
|
label={intl.formatMessage({ id: "Membership no" })}
|
|
name="membershipNo"
|
|
type="tel"
|
|
/>
|
|
)}
|
|
</div>
|
|
<footer className={styles.footer}>
|
|
<Button
|
|
disabled={!methods.formState.isValid}
|
|
intent="secondary"
|
|
size="small"
|
|
theme="base"
|
|
type="submit"
|
|
>
|
|
{intl.formatMessage({ id: "Proceed to payment method" })}
|
|
</Button>
|
|
</footer>
|
|
</form>
|
|
</FormProvider>
|
|
)
|
|
}
|