diff --git a/src/app/(event-space)/explore-event-space/[spaceId]/page.tsx b/src/app/(event-space)/explore-event-space/[spaceId]/page.tsx index c3d06ab..65254d3 100644 --- a/src/app/(event-space)/explore-event-space/[spaceId]/page.tsx +++ b/src/app/(event-space)/explore-event-space/[spaceId]/page.tsx @@ -46,7 +46,7 @@ const EventPage = () => { router.push("/unauthorized"); toast.error("Unauthorized access. Please register."); } else { - router.push(`/register-event/${spaceId}`); + router.push(`/explore-event-space/request-booking?spaceId=${spaceId}`); toast.success("Redirecting to booking..."); } }); diff --git a/src/app/(event-space)/explore-event-space/request-booking/page.tsx b/src/app/(event-space)/explore-event-space/request-booking/page.tsx new file mode 100644 index 0000000..180dea4 --- /dev/null +++ b/src/app/(event-space)/explore-event-space/request-booking/page.tsx @@ -0,0 +1,95 @@ +"use client"; + +import React, { useState, useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { supabase } from "../../../../utils/supabase"; +import { userDetails } from "../../../../action/userDetails"; +import { useRouter, useSearchParams } from "next/navigation"; +import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs"; +import Loading from "@/components/loading"; +import { toast } from "sonner"; + +export default function RequestBooking() { + const router = useRouter(); + const searchParams = useSearchParams(); + const spaceId = searchParams.get("spaceId"); + const { isAuthenticated, isLoading } = useKindeBrowserClient(); + + const [user, setUser]: any = useState(null); + const { register, handleSubmit, formState: { errors } } = useForm(); + + useEffect(() => { + // Fetch user details and set organiser email + const fetchUserDetails = async () => { + try { + const fetchedUserDetails = await userDetails(); + setUser(fetchedUserDetails); + } catch (error) { + console.error("Error fetching user details:", error); + toast.error("Failed to fetch user details"); + setUser(null); + } + }; + + fetchUserDetails(); + }, []); + + const onSubmit = async (data: any) => { + const { request_date } = data; + + if (!spaceId) { + toast.error("Space ID is missing"); + return; + } + + const { error } = await supabase + .from("event_space_request") + .insert([{ + organiser_id: user?.email, + space_id: spaceId, + request_date: request_date, + status: 'pending', + }]); + + if (error) { + console.error(error); + toast.error("Request submission failed. Please try again."); + return; + } + + toast.success("Booking request submitted successfully!"); + router.push("/dashboard"); + }; + + if (isLoading) { + return ; + } + + return isAuthenticated ? ( +
+
+

Request Event Space

+ +
+ + + {errors.request_date && Request date is required} +
+ + +
+
+ ) : ( + router.push("/unauthorized") + ); +}