feat: SW-449 Implemented form submission

This commit is contained in:
Hrishikesh Vaipurkar
2024-10-15 20:17:53 +02:00
parent da898496fe
commit 446b4269d7

View File

@@ -2,6 +2,10 @@
import { useRouter } from "next/navigation"
import { useFormContext } from "react-hook-form"
import { selectHotel, selectRate } from "@/constants/routes/hotelReservation"
import useLang from "@/hooks/useLang"
import FormContent from "./FormContent"
import { bookingWidgetVariants } from "./variants"
@@ -9,11 +13,13 @@ import styles from "./form.module.css"
import type { BookingWidgetSchema } from "@/types/components/bookingWidget"
import type { BookingWidgetFormProps } from "@/types/components/form/bookingwidget"
import { Location } from "@/types/trpc/routers/hotel/locations"
const formId = "booking-widget"
export default function Form({ locations, type }: BookingWidgetFormProps) {
const router = useRouter()
const lang = useLang()
const classNames = bookingWidgetVariants({
type,
@@ -23,11 +29,37 @@ export default function Form({ locations, type }: BookingWidgetFormProps) {
useFormContext<BookingWidgetSchema>()
function onSubmit(data: BookingWidgetSchema) {
data.location = JSON.parse(decodeURIComponent(data.location))
console.log(data)
// TODO: Parse data and route accordignly to Select hotel or select room-rate page
console.log("to be routing")
router.push("/en/hotelreservation/select-hotel")
const locationData: Location = JSON.parse(decodeURIComponent(data.location))
const bookingFlowPage =
locationData.type == "cities" ? selectHotel[lang] : selectRate[lang]
const locationParam =
locationData.type == "cities"
? "city=" + locationData.name
: "hotel=" + locationData.operaId
const dateParam = new URLSearchParams(data.date).toString()
const roomsParam = data.rooms.reduce((result, room, index) => {
result = result + `&rooms[${index}].adults=` + room.adults
result =
result +
room.children.reduce((childParams, child, childIndex) => {
childParams =
childParams +
`&rooms[${index}].child[${childIndex}].age=` +
child.age
childParams =
childParams +
`&rooms[${index}].child[${childIndex}].bed=` +
child.bed
return childParams
}, "")
return result
}, "")
router.push(
bookingFlowPage + "?" + locationParam + "&" + dateParam + roomsParam
)
}
return (