feat: SW-449 Implemented form submission
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
import { useRouter } from "next/navigation"
|
import { useRouter } from "next/navigation"
|
||||||
import { useFormContext } from "react-hook-form"
|
import { useFormContext } from "react-hook-form"
|
||||||
|
|
||||||
|
import { selectHotel, selectRate } from "@/constants/routes/hotelReservation"
|
||||||
|
|
||||||
|
import useLang from "@/hooks/useLang"
|
||||||
|
|
||||||
import FormContent from "./FormContent"
|
import FormContent from "./FormContent"
|
||||||
import { bookingWidgetVariants } from "./variants"
|
import { bookingWidgetVariants } from "./variants"
|
||||||
|
|
||||||
@@ -9,11 +13,13 @@ import styles from "./form.module.css"
|
|||||||
|
|
||||||
import type { BookingWidgetSchema } from "@/types/components/bookingWidget"
|
import type { BookingWidgetSchema } from "@/types/components/bookingWidget"
|
||||||
import type { BookingWidgetFormProps } from "@/types/components/form/bookingwidget"
|
import type { BookingWidgetFormProps } from "@/types/components/form/bookingwidget"
|
||||||
|
import { Location } from "@/types/trpc/routers/hotel/locations"
|
||||||
|
|
||||||
const formId = "booking-widget"
|
const formId = "booking-widget"
|
||||||
|
|
||||||
export default function Form({ locations, type }: BookingWidgetFormProps) {
|
export default function Form({ locations, type }: BookingWidgetFormProps) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const lang = useLang()
|
||||||
|
|
||||||
const classNames = bookingWidgetVariants({
|
const classNames = bookingWidgetVariants({
|
||||||
type,
|
type,
|
||||||
@@ -23,11 +29,37 @@ export default function Form({ locations, type }: BookingWidgetFormProps) {
|
|||||||
useFormContext<BookingWidgetSchema>()
|
useFormContext<BookingWidgetSchema>()
|
||||||
|
|
||||||
function onSubmit(data: BookingWidgetSchema) {
|
function onSubmit(data: BookingWidgetSchema) {
|
||||||
data.location = JSON.parse(decodeURIComponent(data.location))
|
const locationData: Location = JSON.parse(decodeURIComponent(data.location))
|
||||||
console.log(data)
|
|
||||||
// TODO: Parse data and route accordignly to Select hotel or select room-rate page
|
const bookingFlowPage =
|
||||||
console.log("to be routing")
|
locationData.type == "cities" ? selectHotel[lang] : selectRate[lang]
|
||||||
router.push("/en/hotelreservation/select-hotel")
|
|
||||||
|
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 (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user