feat: add validation to enter details flow
This commit is contained in:
@@ -2,6 +2,7 @@ import { redirect } from "next/navigation"
|
|||||||
|
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
|
import EnterDetailsProvider from "@/components/HotelReservation/EnterDetails/Provider"
|
||||||
import SelectedRoom from "@/components/HotelReservation/EnterDetails/SelectedRoom"
|
import SelectedRoom from "@/components/HotelReservation/EnterDetails/SelectedRoom"
|
||||||
import Summary from "@/components/HotelReservation/EnterDetails/Summary"
|
import Summary from "@/components/HotelReservation/EnterDetails/Summary"
|
||||||
import HotelSelectionHeader from "@/components/HotelReservation/HotelSelectionHeader"
|
import HotelSelectionHeader from "@/components/HotelReservation/HotelSelectionHeader"
|
||||||
@@ -9,12 +10,13 @@ import { setLang } from "@/i18n/serverContext"
|
|||||||
|
|
||||||
import styles from "./layout.module.css"
|
import styles from "./layout.module.css"
|
||||||
|
|
||||||
|
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||||
import type { LangParams, LayoutArgs } from "@/types/params"
|
import type { LangParams, LayoutArgs } from "@/types/params"
|
||||||
|
|
||||||
export default async function StepLayout({
|
export default async function StepLayout({
|
||||||
children,
|
children,
|
||||||
params,
|
params,
|
||||||
}: React.PropsWithChildren<LayoutArgs<LangParams>>) {
|
}: React.PropsWithChildren<LayoutArgs<LangParams & { step: StepEnum }>>) {
|
||||||
setLang(params.lang)
|
setLang(params.lang)
|
||||||
const hotel = await serverClient().hotel.hotelData.get({
|
const hotel = await serverClient().hotel.hotelData.get({
|
||||||
hotelId: "811",
|
hotelId: "811",
|
||||||
@@ -26,15 +28,17 @@ export default async function StepLayout({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className={styles.layout}>
|
<EnterDetailsProvider step={params.step}>
|
||||||
<HotelSelectionHeader hotel={hotel.data.attributes} />
|
<main className={styles.layout}>
|
||||||
<div className={styles.content}>
|
<HotelSelectionHeader hotel={hotel.data.attributes} />
|
||||||
<SelectedRoom />
|
<div className={styles.content}>
|
||||||
{children}
|
<SelectedRoom />
|
||||||
<aside className={styles.summary}>
|
{children}
|
||||||
<Summary />
|
<aside className={styles.summary}>
|
||||||
</aside>
|
<Summary />
|
||||||
</div>
|
</aside>
|
||||||
</main>
|
</div>
|
||||||
|
</main>
|
||||||
|
</EnterDetailsProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,117 +1,67 @@
|
|||||||
"use client"
|
|
||||||
|
|
||||||
import { notFound } from "next/navigation"
|
import { notFound } from "next/navigation"
|
||||||
import { useState } from "react"
|
|
||||||
import { useIntl } from "react-intl"
|
|
||||||
|
|
||||||
import { trpc } from "@/lib/trpc/client"
|
import { getProfileSafely } from "@/lib/trpc/memoizedRequests"
|
||||||
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import BedType from "@/components/HotelReservation/EnterDetails/BedType"
|
import BedType from "@/components/HotelReservation/EnterDetails/BedType"
|
||||||
import Breakfast from "@/components/HotelReservation/EnterDetails/Breakfast"
|
import Breakfast from "@/components/HotelReservation/EnterDetails/Breakfast"
|
||||||
import Details from "@/components/HotelReservation/EnterDetails/Details"
|
import Details from "@/components/HotelReservation/EnterDetails/Details"
|
||||||
|
import SectionAccordion from "@/components/HotelReservation/EnterDetails/SectionAccordion"
|
||||||
import Payment from "@/components/HotelReservation/SelectRate/Payment"
|
import Payment from "@/components/HotelReservation/SelectRate/Payment"
|
||||||
import SectionAccordion from "@/components/HotelReservation/SelectRate/SectionAccordion"
|
import { getIntl } from "@/i18n"
|
||||||
import LoadingSpinner from "@/components/LoadingSpinner"
|
|
||||||
|
|
||||||
|
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||||
import type { LangParams, PageArgs } from "@/types/params"
|
import type { LangParams, PageArgs } from "@/types/params"
|
||||||
|
|
||||||
enum StepEnum {
|
|
||||||
selectBed = "select-bed",
|
|
||||||
breakfast = "breakfast",
|
|
||||||
details = "details",
|
|
||||||
payment = "payment",
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidStep(step: string): step is StepEnum {
|
function isValidStep(step: string): step is StepEnum {
|
||||||
return Object.values(StepEnum).includes(step as StepEnum)
|
return Object.values(StepEnum).includes(step as StepEnum)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function StepPage({
|
export default async function StepPage({
|
||||||
params,
|
params,
|
||||||
}: PageArgs<LangParams & { step: StepEnum }>) {
|
}: PageArgs<LangParams & { step: StepEnum }>) {
|
||||||
const { step } = params
|
const { step, lang } = params
|
||||||
const [activeStep, setActiveStep] = useState<StepEnum>(step)
|
|
||||||
const intl = useIntl()
|
|
||||||
|
|
||||||
if (!isValidStep(activeStep)) {
|
const intl = await getIntl()
|
||||||
|
|
||||||
|
const hotel = await serverClient().hotel.hotelData.get({
|
||||||
|
hotelId: "811",
|
||||||
|
language: lang,
|
||||||
|
})
|
||||||
|
|
||||||
|
const user = await getProfileSafely()
|
||||||
|
|
||||||
|
if (!isValidStep(step) || !hotel) {
|
||||||
return notFound()
|
return notFound()
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: hotel, isLoading: loadingHotel } =
|
|
||||||
trpc.hotel.hotelData.get.useQuery({
|
|
||||||
hotelId: "811",
|
|
||||||
language: params.lang,
|
|
||||||
})
|
|
||||||
|
|
||||||
const { data: userData } = trpc.user.getSafely.useQuery()
|
|
||||||
|
|
||||||
if (loadingHotel) {
|
|
||||||
return <LoadingSpinner />
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hotel) {
|
|
||||||
// TODO: handle case with hotel missing
|
|
||||||
return notFound()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (activeStep) {
|
|
||||||
case StepEnum.breakfast:
|
|
||||||
//return <div>Select BREAKFAST</div>
|
|
||||||
case StepEnum.details:
|
|
||||||
//return <div>Select DETAILS</div>
|
|
||||||
case StepEnum.payment:
|
|
||||||
//return <div>Select PAYMENT</div>
|
|
||||||
case StepEnum.selectBed:
|
|
||||||
// return <div>Select BED</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
function onNav(step: StepEnum) {
|
|
||||||
setActiveStep(step)
|
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
window.history.pushState({}, "", step)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let user = null
|
|
||||||
if (userData && !("error" in userData)) {
|
|
||||||
user = userData
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section>
|
<section>
|
||||||
<SectionAccordion
|
<SectionAccordion
|
||||||
header="Select bed"
|
header="Select bed"
|
||||||
isCompleted={true}
|
step={StepEnum.selectBed}
|
||||||
isOpen={activeStep === StepEnum.selectBed}
|
|
||||||
label={intl.formatMessage({ id: "Request bedtype" })}
|
label={intl.formatMessage({ id: "Request bedtype" })}
|
||||||
path="/select-bed"
|
|
||||||
>
|
>
|
||||||
<BedType />
|
<BedType />
|
||||||
</SectionAccordion>
|
</SectionAccordion>
|
||||||
<SectionAccordion
|
<SectionAccordion
|
||||||
header="Food options"
|
header="Food options"
|
||||||
isCompleted={true}
|
step={StepEnum.breakfast}
|
||||||
isOpen={activeStep === StepEnum.breakfast}
|
|
||||||
label={intl.formatMessage({ id: "Select breakfast options" })}
|
label={intl.formatMessage({ id: "Select breakfast options" })}
|
||||||
path="/breakfast"
|
|
||||||
>
|
>
|
||||||
<Breakfast />
|
<Breakfast />
|
||||||
</SectionAccordion>
|
</SectionAccordion>
|
||||||
<SectionAccordion
|
<SectionAccordion
|
||||||
header="Details"
|
header="Details"
|
||||||
isCompleted={false}
|
step={StepEnum.details}
|
||||||
isOpen={activeStep === StepEnum.details}
|
|
||||||
label={intl.formatMessage({ id: "Enter your details" })}
|
label={intl.formatMessage({ id: "Enter your details" })}
|
||||||
path="/details"
|
|
||||||
>
|
>
|
||||||
<Details user={user} />
|
<Details user={user} />
|
||||||
</SectionAccordion>
|
</SectionAccordion>
|
||||||
<SectionAccordion
|
<SectionAccordion
|
||||||
header="Payment"
|
header="Payment"
|
||||||
isCompleted={false}
|
step={StepEnum.payment}
|
||||||
isOpen={activeStep === StepEnum.payment}
|
|
||||||
label={intl.formatMessage({ id: "Select payment method" })}
|
label={intl.formatMessage({ id: "Select payment method" })}
|
||||||
path="/payment"
|
|
||||||
>
|
>
|
||||||
<Payment hotel={hotel.data.attributes} />
|
<Payment hotel={hotel.data.attributes} />
|
||||||
</SectionAccordion>
|
</SectionAccordion>
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useCallback, useEffect } from "react"
|
||||||
import { FormProvider, useForm } from "react-hook-form"
|
import { FormProvider, useForm } from "react-hook-form"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
|
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||||
|
|
||||||
import { KingBedIcon } from "@/components/Icons"
|
import { KingBedIcon } from "@/components/Icons"
|
||||||
import RadioCard from "@/components/TempDesignSystem/Form/ChoiceCard/Radio"
|
import RadioCard from "@/components/TempDesignSystem/Form/ChoiceCard/Radio"
|
||||||
|
|
||||||
@@ -16,8 +19,14 @@ import { bedTypeEnum } from "@/types/enums/bedType"
|
|||||||
|
|
||||||
export default function BedType() {
|
export default function BedType() {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
|
const bedType = useEnterDetailsStore((state) => state.data.bedType)
|
||||||
|
|
||||||
const methods = useForm<BedTypeSchema>({
|
const methods = useForm<BedTypeSchema>({
|
||||||
|
defaultValues: bedType
|
||||||
|
? {
|
||||||
|
bedType,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
criteriaMode: "all",
|
criteriaMode: "all",
|
||||||
mode: "all",
|
mode: "all",
|
||||||
resolver: zodResolver(bedTypeSchema),
|
resolver: zodResolver(bedTypeSchema),
|
||||||
@@ -28,15 +37,32 @@ export default function BedType() {
|
|||||||
{ id: "<b>Included</b> (based on availability)" },
|
{ id: "<b>Included</b> (based on availability)" },
|
||||||
{ b: (str) => <b>{str}</b> }
|
{ b: (str) => <b>{str}</b> }
|
||||||
)
|
)
|
||||||
|
const completeStep = useEnterDetailsStore((state) => state.completeStep)
|
||||||
|
|
||||||
|
const onSubmit = useCallback(
|
||||||
|
(values: BedTypeSchema) => {
|
||||||
|
completeStep(values)
|
||||||
|
},
|
||||||
|
[completeStep]
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (methods.formState.isSubmitting) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const subscription = methods.watch(() => methods.handleSubmit(onSubmit)())
|
||||||
|
return () => subscription.unsubscribe()
|
||||||
|
}, [methods, onSubmit])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider {...methods}>
|
<FormProvider {...methods}>
|
||||||
<form className={styles.form}>
|
<form className={styles.form} onSubmit={methods.handleSubmit(onSubmit)}>
|
||||||
<RadioCard
|
<RadioCard
|
||||||
Icon={KingBedIcon}
|
Icon={KingBedIcon}
|
||||||
iconWidth={46}
|
iconWidth={46}
|
||||||
id={bedTypeEnum.KING}
|
id={bedTypeEnum.KING}
|
||||||
name="bed"
|
name="bedType"
|
||||||
subtitle={intl.formatMessage(
|
subtitle={intl.formatMessage(
|
||||||
{ id: "{width} cm × {length} cm" },
|
{ id: "{width} cm × {length} cm" },
|
||||||
{
|
{
|
||||||
@@ -52,7 +78,7 @@ export default function BedType() {
|
|||||||
Icon={KingBedIcon}
|
Icon={KingBedIcon}
|
||||||
iconWidth={46}
|
iconWidth={46}
|
||||||
id={bedTypeEnum.QUEEN}
|
id={bedTypeEnum.QUEEN}
|
||||||
name="bed"
|
name="bedType"
|
||||||
subtitle={intl.formatMessage(
|
subtitle={intl.formatMessage(
|
||||||
{ id: "{width} cm × {length} cm" },
|
{ id: "{width} cm × {length} cm" },
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ import { z } from "zod"
|
|||||||
import { bedTypeEnum } from "@/types/enums/bedType"
|
import { bedTypeEnum } from "@/types/enums/bedType"
|
||||||
|
|
||||||
export const bedTypeSchema = z.object({
|
export const bedTypeSchema = z.object({
|
||||||
bed: z.nativeEnum(bedTypeEnum),
|
bedType: z.nativeEnum(bedTypeEnum),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useCallback, useEffect } from "react"
|
||||||
import { FormProvider, useForm } from "react-hook-form"
|
import { FormProvider, useForm } from "react-hook-form"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
|
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||||
|
|
||||||
import { BreakfastIcon, NoBreakfastIcon } from "@/components/Icons"
|
import { BreakfastIcon, NoBreakfastIcon } from "@/components/Icons"
|
||||||
import RadioCard from "@/components/TempDesignSystem/Form/ChoiceCard/Radio"
|
import RadioCard from "@/components/TempDesignSystem/Form/ChoiceCard/Radio"
|
||||||
|
|
||||||
@@ -17,16 +20,36 @@ import { breakfastEnum } from "@/types/enums/breakfast"
|
|||||||
export default function Breakfast() {
|
export default function Breakfast() {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
|
|
||||||
|
const breakfast = useEnterDetailsStore((state) => state.data.breakfast)
|
||||||
|
|
||||||
const methods = useForm<BreakfastSchema>({
|
const methods = useForm<BreakfastSchema>({
|
||||||
|
defaultValues: breakfast ? { breakfast } : undefined,
|
||||||
criteriaMode: "all",
|
criteriaMode: "all",
|
||||||
mode: "all",
|
mode: "all",
|
||||||
resolver: zodResolver(breakfastSchema),
|
resolver: zodResolver(breakfastSchema),
|
||||||
reValidateMode: "onChange",
|
reValidateMode: "onChange",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const completeStep = useEnterDetailsStore((state) => state.completeStep)
|
||||||
|
|
||||||
|
const onSubmit = useCallback(
|
||||||
|
(values: BreakfastSchema) => {
|
||||||
|
completeStep(values)
|
||||||
|
},
|
||||||
|
[completeStep]
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (methods.formState.isSubmitting) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const subscription = methods.watch(() => methods.handleSubmit(onSubmit)())
|
||||||
|
return () => subscription.unsubscribe()
|
||||||
|
}, [methods, onSubmit])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider {...methods}>
|
<FormProvider {...methods}>
|
||||||
<form className={styles.form}>
|
<form className={styles.form} onSubmit={methods.handleSubmit(onSubmit)}>
|
||||||
<RadioCard
|
<RadioCard
|
||||||
Icon={BreakfastIcon}
|
Icon={BreakfastIcon}
|
||||||
id={breakfastEnum.BREAKFAST}
|
id={breakfastEnum.BREAKFAST}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useCallback } from "react"
|
||||||
import { FormProvider, useForm } from "react-hook-form"
|
import { FormProvider, useForm } from "react-hook-form"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
|
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||||
|
|
||||||
import Button from "@/components/TempDesignSystem/Button"
|
import Button from "@/components/TempDesignSystem/Button"
|
||||||
import CheckboxCard from "@/components/TempDesignSystem/Form/ChoiceCard/Checkbox"
|
import CheckboxCard from "@/components/TempDesignSystem/Form/ChoiceCard/Checkbox"
|
||||||
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
|
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
|
||||||
@@ -19,6 +22,7 @@ import type {
|
|||||||
DetailsSchema,
|
DetailsSchema,
|
||||||
} from "@/types/components/enterDetails/details"
|
} from "@/types/components/enterDetails/details"
|
||||||
|
|
||||||
|
const formID = "enter-details"
|
||||||
export default function Details({ user }: DetailsProps) {
|
export default function Details({ user }: DetailsProps) {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
|
|
||||||
@@ -28,13 +32,21 @@ export default function Details({ user }: DetailsProps) {
|
|||||||
{ title: intl.formatMessage({ id: "Join at no cost" }) },
|
{ title: intl.formatMessage({ id: "Join at no cost" }) },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const initialData = useEnterDetailsStore((state) => ({
|
||||||
|
countryCode: state.data.countryCode,
|
||||||
|
email: state.data.email,
|
||||||
|
firstname: state.data.firstname,
|
||||||
|
lastname: state.data.lastname,
|
||||||
|
phoneNumber: state.data.phoneNumber,
|
||||||
|
}))
|
||||||
|
|
||||||
const methods = useForm<DetailsSchema>({
|
const methods = useForm<DetailsSchema>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
countryCode: user?.address?.countryCode ?? "",
|
countryCode: user?.address?.countryCode ?? initialData.countryCode,
|
||||||
email: user?.email ?? "",
|
email: user?.email ?? initialData.email,
|
||||||
firstname: user?.firstName ?? "",
|
firstname: user?.firstName ?? initialData.firstname,
|
||||||
lastname: user?.lastName ?? "",
|
lastname: user?.lastName ?? initialData.lastname,
|
||||||
phoneNumber: user?.phoneNumber ?? "",
|
phoneNumber: user?.phoneNumber ?? initialData.phoneNumber,
|
||||||
},
|
},
|
||||||
criteriaMode: "all",
|
criteriaMode: "all",
|
||||||
mode: "all",
|
mode: "all",
|
||||||
@@ -42,6 +54,15 @@ export default function Details({ user }: DetailsProps) {
|
|||||||
reValidateMode: "onChange",
|
reValidateMode: "onChange",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const completeStep = useEnterDetailsStore((state) => state.completeStep)
|
||||||
|
|
||||||
|
const onSubmit = useCallback(
|
||||||
|
(values: DetailsSchema) => {
|
||||||
|
completeStep(values)
|
||||||
|
},
|
||||||
|
[completeStep]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider {...methods}>
|
<FormProvider {...methods}>
|
||||||
<section className={styles.container}>
|
<section className={styles.container}>
|
||||||
@@ -50,7 +71,11 @@ export default function Details({ user }: DetailsProps) {
|
|||||||
{intl.formatMessage({ id: "Guest information" })}
|
{intl.formatMessage({ id: "Guest information" })}
|
||||||
</Body>
|
</Body>
|
||||||
</header>
|
</header>
|
||||||
<form className={styles.form}>
|
<form
|
||||||
|
className={styles.form}
|
||||||
|
id={formID}
|
||||||
|
onSubmit={methods.handleSubmit(onSubmit)}
|
||||||
|
>
|
||||||
<Input
|
<Input
|
||||||
label={intl.formatMessage({ id: "Firstname" })}
|
label={intl.formatMessage({ id: "Firstname" })}
|
||||||
name="firstname"
|
name="firstname"
|
||||||
@@ -106,9 +131,11 @@ export default function Details({ user }: DetailsProps) {
|
|||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
disabled={!methods.formState.isValid}
|
disabled={!methods.formState.isValid}
|
||||||
|
form={formID}
|
||||||
intent="secondary"
|
intent="secondary"
|
||||||
size="small"
|
size="small"
|
||||||
theme="base"
|
theme="base"
|
||||||
|
type="submit"
|
||||||
>
|
>
|
||||||
{intl.formatMessage({ id: "Proceed to payment method" })}
|
{intl.formatMessage({ id: "Proceed to payment method" })}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
26
components/HotelReservation/EnterDetails/Provider/index.tsx
Normal file
26
components/HotelReservation/EnterDetails/Provider/index.tsx
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
"use client"
|
||||||
|
import { PropsWithChildren, useRef } from "react"
|
||||||
|
|
||||||
|
import {
|
||||||
|
EnterDetailsContext,
|
||||||
|
type EnterDetailsStore,
|
||||||
|
initEditDetailsState,
|
||||||
|
} from "@/stores/enter-details"
|
||||||
|
|
||||||
|
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||||
|
|
||||||
|
export default function EnterDetailsProvider({
|
||||||
|
step,
|
||||||
|
children,
|
||||||
|
}: PropsWithChildren<{ step: StepEnum }>) {
|
||||||
|
const initialStore = useRef<EnterDetailsStore>()
|
||||||
|
if (!initialStore.current) {
|
||||||
|
initialStore.current = initEditDetailsState(step)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<EnterDetailsContext.Provider value={initialStore.current}>
|
||||||
|
{children}
|
||||||
|
</EnterDetailsContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { useEffect, useRef } from "react"
|
import { useEffect, useRef, useState } from "react"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
|
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||||
|
|
||||||
import { CheckIcon, ChevronDownIcon } from "@/components/Icons"
|
import { CheckIcon, ChevronDownIcon } from "@/components/Icons"
|
||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Button from "@/components/TempDesignSystem/Button"
|
||||||
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
|
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
|
||||||
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
|
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
|
||||||
|
|
||||||
@@ -13,17 +15,22 @@ import { SectionAccordionProps } from "@/types/components/hotelReservation/selec
|
|||||||
|
|
||||||
export default function SectionAccordion({
|
export default function SectionAccordion({
|
||||||
header,
|
header,
|
||||||
isOpen,
|
|
||||||
isCompleted,
|
|
||||||
label,
|
label,
|
||||||
path,
|
step,
|
||||||
children,
|
children,
|
||||||
}: React.PropsWithChildren<SectionAccordionProps>) {
|
}: React.PropsWithChildren<SectionAccordionProps>) {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
|
const [isComplete, setIsComplete] = useState(false)
|
||||||
|
const currentStep = useEnterDetailsStore((state) => state.currentStep)
|
||||||
|
const isValid = useEnterDetailsStore((state) => state.isValid[step])
|
||||||
|
|
||||||
|
const navigate = useEnterDetailsStore((state) => state.navigate)
|
||||||
|
|
||||||
const contentRef = useRef<HTMLDivElement>(null)
|
const contentRef = useRef<HTMLDivElement>(null)
|
||||||
const circleRef = useRef<HTMLDivElement>(null)
|
const circleRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
const isOpen = currentStep === step
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const content = contentRef.current
|
const content = contentRef.current
|
||||||
const circle = circleRef.current
|
const circle = circleRef.current
|
||||||
@@ -44,15 +51,24 @@ export default function SectionAccordion({
|
|||||||
}
|
}
|
||||||
}, [isOpen])
|
}, [isOpen])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// We need to set the state on mount because of hydration errors
|
||||||
|
setIsComplete(isValid)
|
||||||
|
}, [isValid])
|
||||||
|
|
||||||
|
function onModify() {
|
||||||
|
navigate(step)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.wrapper} data-open={isOpen}>
|
<section className={styles.wrapper} data-open={isOpen}>
|
||||||
<div className={styles.iconWrapper}>
|
<div className={styles.iconWrapper}>
|
||||||
<div
|
<div
|
||||||
className={styles.circle}
|
className={styles.circle}
|
||||||
data-checked={isCompleted}
|
data-checked={isComplete}
|
||||||
ref={circleRef}
|
ref={circleRef}
|
||||||
>
|
>
|
||||||
{isCompleted ? (
|
{isComplete ? (
|
||||||
<CheckIcon color="white" height="16" width="16" />
|
<CheckIcon color="white" height="16" width="16" />
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
@@ -75,11 +91,18 @@ export default function SectionAccordion({
|
|||||||
{label}
|
{label}
|
||||||
</Subtitle>
|
</Subtitle>
|
||||||
</div>
|
</div>
|
||||||
{isCompleted && !isOpen && (
|
{isComplete && !isOpen && (
|
||||||
<Link href={path} color="burgundy" variant="icon">
|
<Button
|
||||||
|
onClick={onModify}
|
||||||
|
theme="base"
|
||||||
|
size="small"
|
||||||
|
variant="icon"
|
||||||
|
intent="text"
|
||||||
|
wrapping
|
||||||
|
>
|
||||||
{intl.formatMessage({ id: "Modify" })}{" "}
|
{intl.formatMessage({ id: "Modify" })}{" "}
|
||||||
<ChevronDownIcon color="burgundy" />
|
<ChevronDownIcon color="burgundy" />
|
||||||
</Link>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</header>
|
</header>
|
||||||
<div className={styles.content} ref={contentRef}>
|
<div className={styles.content} ref={contentRef}>
|
||||||
@@ -75,3 +75,7 @@
|
|||||||
transition: max-height 0.4s ease-out;
|
transition: max-height 0.4s ease-out;
|
||||||
max-height: 0;
|
max-height: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.wrapper[data-open="true"] .content {
|
||||||
|
max-height: 1000px;
|
||||||
|
}
|
||||||
@@ -53,6 +53,7 @@ a.inverted {
|
|||||||
a.text {
|
a.text {
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
|
outline: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* VARIANTS */
|
/* VARIANTS */
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
|
import { useFormContext } from "react-hook-form"
|
||||||
|
|
||||||
import { CheckIcon, CloseIcon, HeartIcon } from "@/components/Icons"
|
import { CheckIcon, CloseIcon, HeartIcon } from "@/components/Icons"
|
||||||
import Caption from "@/components/TempDesignSystem/Text/Caption"
|
import Caption from "@/components/TempDesignSystem/Text/Caption"
|
||||||
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
|
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
|
||||||
@@ -23,6 +25,8 @@ export default function Card({
|
|||||||
type,
|
type,
|
||||||
value,
|
value,
|
||||||
}: CardProps) {
|
}: CardProps) {
|
||||||
|
const { register } = useFormContext()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<label className={styles.label} data-declined={declined}>
|
<label className={styles.label} data-declined={declined}>
|
||||||
<Caption className={styles.title} textTransform="bold" uppercase>
|
<Caption className={styles.title} textTransform="bold" uppercase>
|
||||||
@@ -68,9 +72,9 @@ export default function Card({
|
|||||||
aria-hidden
|
aria-hidden
|
||||||
id={id || name}
|
id={id || name}
|
||||||
hidden
|
hidden
|
||||||
name={name}
|
|
||||||
type={type}
|
type={type}
|
||||||
value={value}
|
value={value}
|
||||||
|
{...register(name)}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -238,14 +238,10 @@ export const userQueryRouter = router({
|
|||||||
|
|
||||||
const data = await getVerifiedUser({ session: ctx.session })
|
const data = await getVerifiedUser({ session: ctx.session })
|
||||||
|
|
||||||
if (!data) {
|
if (!data || "error" in data) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("error" in data) {
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsedUser(data.data, true)
|
return parsedUser(data.data, true)
|
||||||
}),
|
}),
|
||||||
name: safeProtectedProcedure.query(async function ({ ctx }) {
|
name: safeProtectedProcedure.query(async function ({ ctx }) {
|
||||||
|
|||||||
142
stores/enter-details.ts
Normal file
142
stores/enter-details.ts
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
import { produce } from "immer"
|
||||||
|
import { createContext, useContext } from "react"
|
||||||
|
import { create, useStore } from "zustand"
|
||||||
|
|
||||||
|
import { bedTypeSchema } from "@/components/HotelReservation/EnterDetails/BedType/schema"
|
||||||
|
import { breakfastSchema } from "@/components/HotelReservation/EnterDetails/Breakfast/schema"
|
||||||
|
import { detailsSchema } from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||||
|
|
||||||
|
import { DetailsSchema } from "@/types/components/enterDetails/details"
|
||||||
|
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||||
|
import { bedTypeEnum } from "@/types/enums/bedType"
|
||||||
|
import { breakfastEnum } from "@/types/enums/breakfast"
|
||||||
|
|
||||||
|
interface EnterDetailsState {
|
||||||
|
data: {
|
||||||
|
bedType: bedTypeEnum | undefined
|
||||||
|
breakfast: breakfastEnum | undefined
|
||||||
|
} & DetailsSchema
|
||||||
|
steps: StepEnum[]
|
||||||
|
currentStep: StepEnum
|
||||||
|
isValid: Record<StepEnum, boolean>
|
||||||
|
completeStep: (updatedData: Partial<EnterDetailsState["data"]>) => void
|
||||||
|
navigate: (step: StepEnum, searchParams?: Record<string, string>) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export function initEditDetailsState(currentStep: StepEnum) {
|
||||||
|
const isBrowser = typeof window !== "undefined"
|
||||||
|
const sessionData = isBrowser ? sessionStorage.getItem("editDetails") : null
|
||||||
|
const search = isBrowser ? new URLSearchParams(window.location.search) : null
|
||||||
|
|
||||||
|
const defaultData: EnterDetailsState["data"] = {
|
||||||
|
bedType: undefined,
|
||||||
|
breakfast: undefined,
|
||||||
|
countryCode: "",
|
||||||
|
email: "",
|
||||||
|
firstname: "",
|
||||||
|
lastname: "",
|
||||||
|
phoneNumber: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
let inputData = {}
|
||||||
|
if (search?.size) {
|
||||||
|
const searchParams: Record<string, string> = {}
|
||||||
|
search.forEach((value, key) => {
|
||||||
|
searchParams[key] = value
|
||||||
|
})
|
||||||
|
|
||||||
|
inputData = searchParams
|
||||||
|
} else if (sessionData) {
|
||||||
|
inputData = JSON.parse(sessionData)
|
||||||
|
}
|
||||||
|
|
||||||
|
const validPaths = [StepEnum.selectBed]
|
||||||
|
|
||||||
|
let initialData = defaultData
|
||||||
|
|
||||||
|
const isValid = {
|
||||||
|
[StepEnum.selectBed]: false,
|
||||||
|
[StepEnum.breakfast]: false,
|
||||||
|
[StepEnum.details]: false,
|
||||||
|
[StepEnum.payment]: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
const validatedBedType = bedTypeSchema.safeParse(inputData)
|
||||||
|
if (validatedBedType.success) {
|
||||||
|
validPaths.push(StepEnum.breakfast)
|
||||||
|
initialData = { ...initialData, ...validatedBedType.data }
|
||||||
|
isValid[StepEnum.selectBed] = true
|
||||||
|
}
|
||||||
|
const validatedBreakfast = breakfastSchema.safeParse(inputData)
|
||||||
|
if (validatedBreakfast.success) {
|
||||||
|
validPaths.push(StepEnum.details)
|
||||||
|
initialData = { ...initialData, ...validatedBreakfast.data }
|
||||||
|
isValid[StepEnum.breakfast] = true
|
||||||
|
}
|
||||||
|
const validatedDetails = detailsSchema.safeParse(inputData)
|
||||||
|
if (validatedDetails.success) {
|
||||||
|
validPaths.push(StepEnum.payment)
|
||||||
|
initialData = { ...initialData, ...validatedDetails.data }
|
||||||
|
isValid[StepEnum.details] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validPaths.includes(currentStep)) {
|
||||||
|
currentStep = validPaths.pop()! // We will always have at least one valid path
|
||||||
|
if (isBrowser) {
|
||||||
|
window.history.pushState({}, "", currentStep + window.location.search)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return create<EnterDetailsState>()((set, get) => ({
|
||||||
|
data: initialData,
|
||||||
|
steps: Object.values(StepEnum),
|
||||||
|
navigate: (step, searchParams) =>
|
||||||
|
set(
|
||||||
|
produce((state) => {
|
||||||
|
const query = new URLSearchParams(window.location.search)
|
||||||
|
if (searchParams) {
|
||||||
|
Object.entries(searchParams).forEach(([key, value]) => {
|
||||||
|
query.set(key, value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
state.currentStep = step
|
||||||
|
window.history.pushState({}, "", step + "?" + query.toString())
|
||||||
|
})
|
||||||
|
),
|
||||||
|
currentStep,
|
||||||
|
isValid,
|
||||||
|
completeStep: (updatedData) =>
|
||||||
|
set(
|
||||||
|
produce((state) => {
|
||||||
|
state.isValid[state.currentStep] = true
|
||||||
|
|
||||||
|
const nextStep =
|
||||||
|
state.steps[state.steps.indexOf(state.currentStep) + 1]
|
||||||
|
|
||||||
|
state.data = { ...state.data, ...updatedData }
|
||||||
|
|
||||||
|
state.currentStep = nextStep
|
||||||
|
get().navigate(nextStep, updatedData)
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
export type EnterDetailsStore = ReturnType<typeof initEditDetailsState>
|
||||||
|
|
||||||
|
export const EnterDetailsContext = createContext<EnterDetailsStore | null>(null)
|
||||||
|
|
||||||
|
export const useEnterDetailsStore = <T>(
|
||||||
|
selector: (store: EnterDetailsState) => T
|
||||||
|
): T => {
|
||||||
|
const enterDetailsContextStore = useContext(EnterDetailsContext)
|
||||||
|
|
||||||
|
if (!enterDetailsContextStore) {
|
||||||
|
throw new Error(
|
||||||
|
`useEnterDetailsStore must be used within EnterDetailsContextProvider`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return useStore(enterDetailsContextStore, selector)
|
||||||
|
}
|
||||||
@@ -2,10 +2,10 @@ import { z } from "zod"
|
|||||||
|
|
||||||
import { detailsSchema } from "@/components/HotelReservation/EnterDetails/Details/schema"
|
import { detailsSchema } from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||||
|
|
||||||
import { User } from "@/types/user"
|
import type { SafeUser } from "@/types/user"
|
||||||
|
|
||||||
export interface DetailsSchema extends z.output<typeof detailsSchema> {}
|
export interface DetailsSchema extends z.output<typeof detailsSchema> {}
|
||||||
|
|
||||||
export interface DetailsProps {
|
export interface DetailsProps {
|
||||||
user: User | null
|
user: SafeUser
|
||||||
}
|
}
|
||||||
|
|||||||
6
types/components/enterDetails/step.ts
Normal file
6
types/components/enterDetails/step.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export enum StepEnum {
|
||||||
|
selectBed = "select-bed",
|
||||||
|
breakfast = "breakfast",
|
||||||
|
details = "details",
|
||||||
|
payment = "payment",
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
|
import { StepEnum } from "../../enterDetails/step"
|
||||||
|
|
||||||
export interface SectionAccordionProps {
|
export interface SectionAccordionProps {
|
||||||
header: string
|
header: string
|
||||||
isOpen: boolean
|
|
||||||
isCompleted: boolean
|
|
||||||
label: string
|
label: string
|
||||||
path: string
|
step: StepEnum
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
import { creditCardSchema, getUserSchema, membershipSchema } from "@/server/routers/user/output"
|
import {
|
||||||
|
creditCardSchema,
|
||||||
|
getUserSchema,
|
||||||
|
membershipSchema,
|
||||||
|
} from "@/server/routers/user/output"
|
||||||
|
|
||||||
|
import type { RouterOutput } from "@/lib/trpc/client"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All extended field needs to be added by API team to response or
|
* All extended field needs to be added by API team to response or
|
||||||
* we have to get the values from elsewhere
|
* we have to get the values from elsewhere
|
||||||
*/
|
*/
|
||||||
export interface User extends z.output<typeof getUserSchema> { }
|
export interface User extends z.output<typeof getUserSchema> {}
|
||||||
|
|
||||||
|
export type SafeUser = RouterOutput["user"]["getSafely"]
|
||||||
|
|
||||||
export type CreditCard = z.output<typeof creditCardSchema>
|
export type CreditCard = z.output<typeof creditCardSchema>
|
||||||
|
|
||||||
export interface Membership extends z.output<typeof membershipSchema> { }
|
export interface Membership extends z.output<typeof membershipSchema> {}
|
||||||
|
|
||||||
export type Memberships = Membership[]
|
export type Memberships = Membership[]
|
||||||
|
|||||||
Reference in New Issue
Block a user