From fb5ba552e93f983514937c87852d498478b339a9 Mon Sep 17 00:00:00 2001 From: Hrishikesh Vaipurkar Date: Thu, 1 Aug 2024 13:40:18 +0200 Subject: [PATCH] SW-65 Removed action used onSubmit and dt lib --- actions/updateBookingWidget.ts | 56 ------------------------- components/BookingWidget/index.tsx | 27 ++++++------ components/BookingWidget/schema.ts | 9 ++-- lib/dt.ts | 2 + types/components/bookingWidget/index.ts | 5 --- 5 files changed, 19 insertions(+), 80 deletions(-) delete mode 100644 actions/updateBookingWidget.ts delete mode 100644 types/components/bookingWidget/index.ts diff --git a/actions/updateBookingWidget.ts b/actions/updateBookingWidget.ts deleted file mode 100644 index 5370678d3..000000000 --- a/actions/updateBookingWidget.ts +++ /dev/null @@ -1,56 +0,0 @@ -"use server" -import { ZodError } from "zod" - -import { bookingWidgetSchema } from "@/components/BookingWidget/schema" - -import { type State, Status } from "@/types/components/myPages/myProfile/edit" - -export async function updateBookingWidget(_prevState: State, values: FormData) { - try { - const data: Record = Object.fromEntries(values.entries()) - - /** - * ToDo: Update the data parsing and redirect User to respective booking flow page - */ - console.info(`Raw Data BW`) - console.log(data) - const parsedData = bookingWidgetSchema.safeParse(data) - if (parsedData.success) { - console.info(`Success`) - console.log(parsedData.data) - return { - message: "All good!", - status: Status.success, - } - } else { - console.error("Error parsing BW data") - console.error(parsedData.error) - return { - message: "Invalid data, parse failed!", - status: Status.error, - } - } - } catch (error) { - if (error instanceof ZodError) { - console.error(`ZodError handling profile edit`) - console.error(error) - - return { - errors: error.issues.map((issue) => ({ - message: `Server validation: ${issue.message}`, - path: issue.path.join("."), - })), - message: "Invalid form data", - status: Status.error, - } - } - - console.error(`EditProfile Server Action Error`) - console.error(error) - - return { - message: "Something went wrong. Please try again.", - status: Status.error, - } - } -} diff --git a/components/BookingWidget/index.tsx b/components/BookingWidget/index.tsx index fddb7b5c6..f4eebe601 100644 --- a/components/BookingWidget/index.tsx +++ b/components/BookingWidget/index.tsx @@ -1,22 +1,15 @@ "use client" import { zodResolver } from "@hookform/resolvers/zod" -import { useFormState as useReactFormState } from "react-dom" import { FormProvider, useForm } from "react-hook-form" -import { updateBookingWidget } from "@/actions/updateBookingWidget" +import { dt } from "@/lib/dt" import Button from "../TempDesignSystem/Button" import { type BookingWidgetSchema, bookingWidgetSchema } from "./schema" import styles from "./bookingWidget.module.css" -import { State } from "@/types/components/myPages/myProfile/edit" - export function BookingWidget() { - const [state, formAction] = useReactFormState( - updateBookingWidget, - null - ) const methods = useForm({ defaultValues: { search: { @@ -24,12 +17,12 @@ export function BookingWidget() { stayValue: "", }, nights: { - fromDate: new Date(), - toDate: new Date(new Date().setDate(+1)), - }, - bookingCode: { - value: "", + // UTC is required to handle requests from far away timezones https://scandichotels.atlassian.net/browse/SWAP-6375 & PET-507 + // This is specifically to handle timezones falling in different dates. + fromDate: dt().utc().format("DD/MM/YYYY"), + toDate: dt().utc().add(1, "day").format("DD/MM/YYYY"), }, + bookingCode: "", redemption: false, voucher: false, rooms: [ @@ -44,9 +37,15 @@ export function BookingWidget() { reValidateMode: "onChange", }) + function onSubmit(data: BookingWidgetSchema) { + console.log(data) + // Parse data and route accordignly to Select hotel or select room-rate page + console.log("to be routing") + } + return (
-
+
Search
Nights
diff --git a/components/BookingWidget/schema.ts b/components/BookingWidget/schema.ts index 5ce7f2d2a..39a06c78e 100644 --- a/components/BookingWidget/schema.ts +++ b/components/BookingWidget/schema.ts @@ -6,12 +6,11 @@ export const bookingWidgetSchema = z.object({ stayValue: z.string(), }), nights: z.object({ - fromDate: z.date().default(new Date()), - toDate: z.date().default(new Date(new Date().setDate(+1))), - }), - bookingCode: z.object({ - value: z.string(), + // Update this as required once started working with Date picker in Nights component + fromDate: z.string(), + toDate: z.string(), }), + bookingCode: z.string(), // Update this as required when working with booking codes component redemption: z.boolean().default(false), voucher: z.boolean().default(false), rooms: z.array( diff --git a/lib/dt.ts b/lib/dt.ts index f6e8faf0b..083032505 100644 --- a/lib/dt.ts +++ b/lib/dt.ts @@ -6,6 +6,7 @@ import "dayjs/locale/sv" import d from "dayjs" import isToday from "dayjs/plugin/isToday" import relativeTime from "dayjs/plugin/relativeTime" +import utc from "dayjs/plugin/utc" /** * dayjs export Norwegian as nb [Norwegian Bokmål] so here we create the same @@ -56,5 +57,6 @@ d.locale("no", { */ d.extend(isToday) d.extend(relativeTime) +d.extend(utc) export const dt = d diff --git a/types/components/bookingWidget/index.ts b/types/components/bookingWidget/index.ts deleted file mode 100644 index 457a9a1af..000000000 --- a/types/components/bookingWidget/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { BookingWidgetSchema } from "@/components/BookingWidget/schema" - -export type bwFormProps = { - data: BookingWidgetSchema -}