diff --git a/src/app/dataroom/overview/page.tsx b/src/app/dataroom/overview/page.tsx new file mode 100644 index 0000000..e5abbb5 --- /dev/null +++ b/src/app/dataroom/overview/page.tsx @@ -0,0 +1,216 @@ +"use client"; + +import { useMemo } from "react"; +import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; +import { requestAccessToDataRoom } from "@/lib/data/dataroomMutate"; +import { getInvestmentByUserId } from "@/lib/data/investmentQuery"; +import { + Table, + TableBody, + TableCaption, + TableCell, + TableHeader, + TableHead, + TableRow, + TableFooter, +} from "@/components/ui/table"; +import { Button } from "@/components/ui/button"; +import { getAccessRequests } from "@/lib/data/dataroomQuery"; +import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; +import { Separator } from "@/components/ui/separator"; +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; +import useSession from "@/lib/supabase/useSession"; +import toast from "react-hot-toast"; + +interface Investment { + id: any; + project_id: any; + project_name: any; + project_short_description: any; + dataroom_id: any; + deal_amount: any; + investor_id: any; + created_time: any; +} + +interface AccessRequest { + id: number; + dataroom_id: number; + user_id: string; + status: "approve" | "reject" | "pending"; + requested_at: string; +} + +export default function ListRequestAccessPage() { + const client = createSupabaseClient(); + const { session, loading: sessionLoading } = useSession(); + const userId = session?.user?.id; + + const { + data: investments = [], + error: investmentsError, + isLoading: isLoadingInvestments, + } = useQuery(getInvestmentByUserId(client, userId ?? ""), { + enabled: !!userId, + }); + + const { + data: accessRequests = [], + error: accessRequestsError, + isLoading: isLoadingAccessRequests, + refetch: refetchAccessRequests, + } = useQuery(getAccessRequests(client, { userId: userId ?? "" }), { + enabled: !!userId, + }); + + const projectInvestments = useMemo(() => { + if (!investments) return {}; + + return investments.reduce((acc: { [key: string]: Investment[] }, investment: Investment) => { + const projectId = investment.project_id; + if (!acc[projectId]) { + acc[projectId] = []; + } + acc[projectId].push(investment); + return acc; + }, {}); + }, [investments]); + + const handleRequestAccess = async (dataroomId: any) => { + if (!userId) { + toast.error("Please login first"); + return; + } + + try { + const { error } = await requestAccessToDataRoom(client, dataroomId, userId); + if (error) { + toast.error("Error sending request."); + return; + } + + toast.success("Request sent successfully!"); + await refetchAccessRequests(); + } catch (error) { + toast.error("An unexpected error occurred"); + } + }; + + if (sessionLoading) { + return