From a7167dde6aec6e824d6bc352c6bac953cb428c96 Mon Sep 17 00:00:00 2001 From: Hrishikesh Vaipurkar Date: Wed, 18 Sep 2024 13:24:35 +0200 Subject: [PATCH] feat: SW-276 Implemented child age validation --- components/Forms/BookingWidget/schema.ts | 24 ++++----- .../ChildSelector/ChildInfoSelector.tsx | 49 ++++++++++++------- .../ChildSelector/child-selector.module.css | 5 +- .../GuestsRoomPicker/ChildSelector/index.tsx | 15 ++++++ .../guests-room-picker.module.css | 1 - .../GuestsRoomPicker/index.tsx | 2 + .../GuestsRoomsPicker/GuestsRoomsPicker.tsx | 14 ++++-- .../guests-rooms-picker.module.css | 10 +++- components/GuestsRoomsPicker/index.tsx | 41 ++++++++++------ .../bookingWidget/guestsRoomsPicker.ts | 3 ++ 10 files changed, 110 insertions(+), 54 deletions(-) diff --git a/components/Forms/BookingWidget/schema.ts b/components/Forms/BookingWidget/schema.ts index bc0be8d9c..9541abd42 100644 --- a/components/Forms/BookingWidget/schema.ts +++ b/components/Forms/BookingWidget/schema.ts @@ -2,6 +2,18 @@ import { z } from "zod" import type { Location } from "@/types/trpc/routers/hotel/locations" +export const guestRoomsSchema = z.array( + z.object({ + adults: z.number().default(1), + children: z.array( + z.object({ + age: z.number().nonnegative(), + bed: z.number(), + }) + ), + }) +) + export const bookingWidgetSchema = z.object({ bookingCode: z.string(), // Update this as required when working with booking codes component date: z.object({ @@ -25,17 +37,7 @@ export const bookingWidgetSchema = z.object({ { message: "Required" } ), redemption: z.boolean().default(false), - rooms: z.array( - z.object({ - adults: z.number().default(1), - children: z.array( - z.object({ - age: z.number(), - bed: z.number(), - }) - ), - }) - ), + rooms: guestRoomsSchema, search: z.string({ coerce: true }).min(1, "Required"), voucher: z.boolean().default(false), }) diff --git a/components/GuestsRoomsPicker/GuestsRoomPicker/ChildSelector/ChildInfoSelector.tsx b/components/GuestsRoomsPicker/GuestsRoomPicker/ChildSelector/ChildInfoSelector.tsx index 7dbc6d303..d0ff98aba 100644 --- a/components/GuestsRoomsPicker/GuestsRoomPicker/ChildSelector/ChildInfoSelector.tsx +++ b/components/GuestsRoomsPicker/GuestsRoomPicker/ChildSelector/ChildInfoSelector.tsx @@ -12,6 +12,7 @@ type ChildSelectorProps = { index: number availableBedTypes?: ChildBed[] updateChild: (child: Child, index: number) => void + childAgeError: boolean } export default function ChildInfoSelector({ @@ -23,6 +24,7 @@ export default function ChildInfoSelector({ { label: "In extra bed", value: 2 }, ], updateChild = (child: Child, index: number) => {}, + childAgeError, }: ChildSelectorProps) { const intl = useIntl() const ageLabel = intl.formatMessage({ id: "Age" }) @@ -55,29 +57,38 @@ export default function ChildInfoSelector({ return ( <> - { - handleOnSelect(key, "bed") + handleOnSelect(key, "age") }} - name="bed" + name="age" + placeholder={ageLabel} /> - ) : null} + {childAgeError && child.age < 0 ? ( + Child Age is required + ) : null} + +
+ {child.age !== -1 ? ( +