mirror of
https://github.com/Sosokker/B2D-Ventures.git
synced 2025-12-20 14:34:05 +01:00
feat: only approve user can access dataroom
This commit is contained in:
parent
3bc6159618
commit
02a3d4092b
@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { FileIcon, defaultStyles } from "react-file-icon";
|
import { FileIcon, defaultStyles } from "react-file-icon";
|
||||||
import { getFilesByDataroomId } from "@/lib/data/dataroomQuery";
|
import { getFilesByDataroomId } from "@/lib/data/dataroomQuery";
|
||||||
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
||||||
@ -10,37 +10,83 @@ import Link from "next/link";
|
|||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
import { getAccessRequests } from "@/lib/data/dataroomQuery";
|
||||||
|
import useSession from "@/lib/supabase/useSession";
|
||||||
|
|
||||||
export default function ViewDataRoomFilesPage({ params }: { params: { dataroomId: number } }) {
|
export default function ViewDataRoomFilesPage({ params }: { params: { dataroomId: number } }) {
|
||||||
const dataroomId = params.dataroomId;
|
const dataroomId = params.dataroomId;
|
||||||
const supabase = createSupabaseClient();
|
const supabase = createSupabaseClient();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const [sortOption, setSortOption] = useState("name");
|
||||||
|
const { session, loading: sessionLoading } = useSession();
|
||||||
|
const userId = session?.user?.id;
|
||||||
|
|
||||||
const getProjectDataQuery = supabase
|
useEffect(() => {
|
||||||
.from("project")
|
if (!sessionLoading && !session) {
|
||||||
.select(`id, project_name, dataroom_id`)
|
toast.error("Please login to access this page");
|
||||||
.eq("dataroom_id", dataroomId);
|
router.push("/auth/login");
|
||||||
const { data: project, error: projectError, isLoading: isLoadingProject } = useQuery(getProjectDataQuery);
|
}
|
||||||
const { data: files, error, isLoading: isLoadingFiles } = useQuery(getFilesByDataroomId(supabase, dataroomId));
|
}, [session, sessionLoading, router]);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: project,
|
||||||
|
error: projectError,
|
||||||
|
isLoading: isLoadingProject,
|
||||||
|
} = useQuery(supabase.from("project").select(`id, project_name, dataroom_id`).eq("dataroom_id", dataroomId), {
|
||||||
|
enabled: !!userId,
|
||||||
|
});
|
||||||
|
const {
|
||||||
|
data: accessRequest,
|
||||||
|
error: accessRequestError,
|
||||||
|
isLoading: accessRequestLoading,
|
||||||
|
} = useQuery(getAccessRequests(supabase, { dataroomId: dataroomId, userId: userId }));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!accessRequestLoading && accessRequest) {
|
||||||
|
const hasAccess = accessRequest[0]?.status === "approve";
|
||||||
|
if (!hasAccess) {
|
||||||
|
toast.error("You don't have permission to access this dataroom");
|
||||||
|
router.push("/dataroom/overview");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [accessRequest, accessRequestLoading, router]);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: files,
|
||||||
|
error: filesError,
|
||||||
|
isLoading: isLoadingFiles,
|
||||||
|
} = useQuery(getFilesByDataroomId(supabase, dataroomId));
|
||||||
|
|
||||||
|
if (projectError || filesError || accessRequestError) {
|
||||||
|
const errorMessage = projectError
|
||||||
|
? "Unable to load project details"
|
||||||
|
: filesError
|
||||||
|
? "Unable to load files"
|
||||||
|
: "Unable to verify access permissions";
|
||||||
|
|
||||||
|
toast.error(errorMessage);
|
||||||
|
router.push("/dataroom/overview");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLoadingProject || isLoadingFiles || accessRequestLoading) {
|
||||||
|
return (
|
||||||
|
<div className="container max-w-screen-xl p-4">
|
||||||
|
<Skeleton className="h-8 w-64 mb-4" />
|
||||||
|
<div className="space-y-3">
|
||||||
|
{[...Array(3)].map((_, index) => (
|
||||||
|
<Skeleton key={index} className="h-16 w-full" />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function getFileNameFromUrl(fileUrl: string): string {
|
function getFileNameFromUrl(fileUrl: string): string {
|
||||||
const fullFileName = fileUrl.split("/").pop() || "";
|
const fullFileName = fileUrl.split("/").pop() || "";
|
||||||
return decodeURIComponent(fullFileName.split("?")[0]);
|
return decodeURIComponent(fullFileName.split("?")[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
|
||||||
toast.error("Unable to load files.");
|
|
||||||
router.push("/");
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (projectError) {
|
|
||||||
toast.error("Unable to load project that relate to dataroom.");
|
|
||||||
router.push("/");
|
|
||||||
throw projectError;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [sortOption, setSortOption] = useState("name");
|
|
||||||
const sortedFiles = [...(files || [])].sort((a, b) => {
|
const sortedFiles = [...(files || [])].sort((a, b) => {
|
||||||
if (sortOption === "name") {
|
if (sortOption === "name") {
|
||||||
const nameA = getFileNameFromUrl(a.file_url).toLowerCase();
|
const nameA = getFileNameFromUrl(a.file_url).toLowerCase();
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { SupabaseClient } from "@supabase/supabase-js";
|
import { SupabaseClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
export const requestAccessToDataRoom = (client: SupabaseClient, dataRoomId: number, userId: number) => {
|
export const requestAccessToDataRoom = (client: SupabaseClient, dataRoomId: number, userId: string) => {
|
||||||
return client.from("access_request").insert([
|
return client.from("access_request").insert([
|
||||||
{
|
{
|
||||||
dataroom_id: dataRoomId,
|
dataroom_id: dataRoomId,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user