From f39b093f0dc9d13e2ccfc601d90c53c85832d9e2 Mon Sep 17 00:00:00 2001 From: Sid-80 Date: Sun, 4 Aug 2024 21:19:59 +0530 Subject: [PATCH 1/2] feat: team settings page --- src/app/api/files/get/[id]/route.ts | 2 +- src/app/api/teams/getTeamById/[id]/route.ts | 3 +- .../_components/SideNavBottomSection.tsx | 6 +- src/app/teams/settings/[id]/page.tsx | 54 +++++++++--------- .../teams/settings/_components/FileList.tsx | 56 ++++--------------- src/components/shared/MemberCarousel.tsx | 21 +++---- 6 files changed, 51 insertions(+), 91 deletions(-) diff --git a/src/app/api/files/get/[id]/route.ts b/src/app/api/files/get/[id]/route.ts index 736da16..c0a49a3 100644 --- a/src/app/api/files/get/[id]/route.ts +++ b/src/app/api/files/get/[id]/route.ts @@ -20,7 +20,7 @@ export async function GET( await mongoDB(); - const files = await FileModel.find({teamId:id}).populate("createdBy") + const files = await FileModel.find({teamId:id}).populate("createdBy"); return NextResponse.json(files,{ status: 200 }); } catch (err) { diff --git a/src/app/api/teams/getTeamById/[id]/route.ts b/src/app/api/teams/getTeamById/[id]/route.ts index 977d7b2..99b25c2 100644 --- a/src/app/api/teams/getTeamById/[id]/route.ts +++ b/src/app/api/teams/getTeamById/[id]/route.ts @@ -14,8 +14,7 @@ export async function GET( await mongoDB(); - const teams = await TeamModel.findById({ _id: id }).populate("createdBy"); - + const teams = await TeamModel.findById({ _id: id }).populate("createdBy").populate("files"); return NextResponse.json(teams, { status: 200 }); } catch (err) { diff --git a/src/app/dashboard/_components/SideNavBottomSection.tsx b/src/app/dashboard/_components/SideNavBottomSection.tsx index a42bfe3..2eaabdc 100644 --- a/src/app/dashboard/_components/SideNavBottomSection.tsx +++ b/src/app/dashboard/_components/SideNavBottomSection.tsx @@ -83,8 +83,7 @@ function SideNavBottomSection({getFiles, totalFiles, activeTeam }: any) { const [fileList, setFileList] = useState([]); const [fileInput, setFileInput] = useState(""); const [error, setError] = useState(""); - - const email = ((state:RootState) => state.auth.user.email) + const user = useSelector((state:RootState)=>state.auth.user); const axiosInstance = createAxiosInstance(user.accessToken); const [filePrivate,setFileprivate] = useState(false); @@ -132,6 +131,7 @@ function SideNavBottomSection({getFiles, totalFiles, activeTeam }: any) { console.log(err) } } + console.log(activeTeam) return (
@@ -148,7 +148,7 @@ function SideNavBottomSection({getFiles, totalFiles, activeTeam }: any) { ))} - {email === activeTeam?.createdBy && + {activeTeam && user.id === activeTeam.createdBy &&

(null); + const [focusedUser, setFocusedUser] = useState(null); const [fileList, setFileList] = useState([]); const [updated,setIsUpdated] = useState(true); + const user = useSelector((state:RootState) => state.auth.user); + const axiosInstance = createAxiosInstance(user.accessToken); + useEffect(() => { - const getData = async () => { - const res = await convex.query(api.files.getFiles, { teamId: teamId }); - setFileList(res); - setIsUpdated(false); + + const getFiles = async () => { + const result = await axiosInstance.get(`${getFileUrl}/${teamId}`); + setFileList(result.data); }; if (teamId && updated) { - getData(); + getFiles(); } }, [teamId,updated]); - useEffect(() => { - const getData = async () => { - try { - const res = await axiosInstance.get(`${getTeamMembersData}/${teamId}`); - setData(res.data.memberData); - } catch (err) { - console.log(err); - } - }; - if (teamId) { - getData(); + console.log(fileList) + + useEffect(()=>{ + const getData = async() => { + const res = await axiosInstance.get(`${getTeamMembersData}/${teamId}`); + setData(res.data) } - }, [teamId]); + getData() + + },[]) return (
@@ -65,9 +65,9 @@ export default function Page({ params }: { params: { id: string } }) { )}
- {focusedUser !== null && ( + {focusedUser !== null && - )} + }
); diff --git a/src/app/teams/settings/_components/FileList.tsx b/src/app/teams/settings/_components/FileList.tsx index a1af404..3bb83ff 100644 --- a/src/app/teams/settings/_components/FileList.tsx +++ b/src/app/teams/settings/_components/FileList.tsx @@ -1,44 +1,13 @@ import { useState, useEffect, SetStateAction } from "react"; import { Loader2, - ChevronsUpDown, - ArchiveIcon, - CheckCircle2, - Edit3Icon, + ChevronsUpDown } from "lucide-react"; -import { usePathname, useRouter } from "next/navigation"; -import { useConvex } from "convex/react"; -import { Button } from "@/components/ui/button"; -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, - AlertDialogTrigger, -} from "@/components/ui/alert-dialog"; +import { useRouter } from "next/navigation"; import { Badge } from "@/components/ui/badge"; import ReadAccessModal from "@/components/shared/ReadAccessModal"; import WriteAccessModal from "@/components/shared/WriteAccessModal"; - -export interface FILE { - archive: boolean; - createdBy: string; - document: string; - fileName: string; - teamId: string; - whiteboard: string; - _id: string; - _creationTime: number; - private: boolean; - read: boolean; - write: boolean; - readBy: string[]; - writtenBy: string[]; -} +import { FILE } from "@/types/types"; const FileRow = ({ file, @@ -64,21 +33,21 @@ const FileRow = ({ className="whitespace-nowrap px-4 py-2 text-muted-foreground" onClick={() => router.push("/workspace/" + file._id)} > - {file.createdBy} + {file.createdBy.email} router.push("/workspace/" + file._id)} > {file.readBy - ? file.readBy.includes(user.email.toString()) && Read + ? file.readBy.includes(user._id) && Read : ""} {file.writtenBy ? file.writtenBy && - file.writtenBy.includes(user.email) && Write + file.writtenBy.includes(user._id) && Write : ""} - {!file.readBy.includes(user.email) && - !file.writtenBy.includes(user.email) && No Access} + {!file.readBy.includes(user._id) && + !file.writtenBy.includes(user._id) && No Access} >; }) { const router = useRouter(); - const convex = useConvex(); const [sortConfig, setSortConfig] = useState<{ key: keyof FILE; direction: string; @@ -245,14 +213,14 @@ function FileList({

- {file.readBy && file.readBy.includes(user.email) && ( + {file.readBy && file.readBy.includes(user._id) && ( Read )} - {file.writtenBy && file.writtenBy.includes(user.email) && ( + {file.writtenBy && file.writtenBy.includes(user._id) && ( Write )} - {!file.readBy.includes(user.email) && - !file.writtenBy.includes(user.email) && ( + {!file.readBy.includes(user._id) && + !file.writtenBy.includes(user._id) && ( No Access )}
diff --git a/src/components/shared/MemberCarousel.tsx b/src/components/shared/MemberCarousel.tsx index cf24afc..7fcf114 100644 --- a/src/components/shared/MemberCarousel.tsx +++ b/src/components/shared/MemberCarousel.tsx @@ -16,17 +16,10 @@ import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar"; import { SetStateAction } from "react"; import DeleteTeamMember from "./DeleteTeamMember"; - export type USER = { - name: string; - _id: string; - email: string; - image: string; -}; - type Props = { - teamMembersData: USER[]; - focusedUser:USER | null; - setFocusedUser:React.Dispatch>; + teamMembersData: any[]; + focusedUser:any; + setFocusedUser:React.Dispatch>; }; export default function MemberCarousel({ teamMembersData,focusedUser,setFocusedUser }: Props) { @@ -36,7 +29,7 @@ export default function MemberCarousel({ teamMembersData,focusedUser,setFocusedU {teamMembersData !== null && - teamMembersData.map((user: USER, index) => ( + teamMembersData.map((user: any, index) => ( setFocusedUser(user)} key={index} @@ -48,12 +41,12 @@ export default function MemberCarousel({ teamMembersData,focusedUser,setFocusedU > - + - {user.name.charAt(0)} + {user.firstName.charAt(0)} - {user.name} + {user.firstName}{" "}{user.lastName} From 87ae269a5a966a1f9c98dbcbc36b1f0fb6243d98 Mon Sep 17 00:00:00 2001 From: Sid-80 Date: Wed, 7 Aug 2024 16:27:49 +0530 Subject: [PATCH 2/2] feat: mongo read and write --- src/app/api/files/read/route.ts | 128 ++++++++++------- src/app/api/files/write/route.ts | 135 +++++++++++------- .../teams/settings/_components/FileList.tsx | 4 - src/components/shared/ReadAccessModal.tsx | 70 +++++---- src/components/shared/WriteAccessModal.tsx | 68 +++++---- 5 files changed, 246 insertions(+), 159 deletions(-) diff --git a/src/app/api/files/read/route.ts b/src/app/api/files/read/route.ts index a22f603..5f67009 100644 --- a/src/app/api/files/read/route.ts +++ b/src/app/api/files/read/route.ts @@ -1,68 +1,98 @@ import { api } from "../../../../../convex/_generated/api"; import { ConvexHttpClient } from "convex/browser"; import { Id } from "../../../../../convex/_generated/dataModel"; +import { AuthMiddleware } from "@/Middleware/AuthMiddleware"; +import { NextResponse } from "next/server"; +import FileModel from "@/models/file"; +import { ApiUser } from "@/types/types"; +import { mongoDB } from "@/lib/MongoDB"; -// Give user read access -export const POST = async (req: Request) => { - try { - const { teamId, email, memberEmail, readBy, fileId } = await req.json(); - if (!teamId || !memberEmail || !email || !fileId) - return new Response("Parameters missing!!", { status: 401 }); +// Remove read access from the user +export async function PUT( + request: Request +) { - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + const result = await AuthMiddleware(request); - const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId as Id<"teams">}); - - if (!teamInfo.teamMembers.includes(memberEmail)) { - return new Response("User is not member of the team", { status: 400 }); - } - - if (teamInfo.createdBy !== email) { - return new Response("Only owner can make changes!!", { status: 400 }); - } - - readBy.push(memberEmail); + if (result instanceof NextResponse) { - await client.mutation(api.files.updateRead, { _id: fileId as Id<"files">, readBy:readBy }); + try { + await mongoDB(); - return new Response("Changed to Public!!", { status: 200 }); - } catch (err) { + const { userId, fileId } = await request.json(); - return new Response(`Error: ${err}`, {status:500}) + if (!userId || !fileId) { + return NextResponse.json(`Access Denied!!`, { status: 404 }); + } - } -}; - -// Remove read access from the user -export const PUT = async (req: Request) => { - try { - const { teamId, email, memberEmail, readBy, fileId } = await req.json(); + const user: ApiUser = JSON.parse(request.headers.get("user") || "{}"); - if (!teamId || !memberEmail || !email || !fileId) - return new Response("Parameters missing!!", { status: 401 }); + const file = await FileModel.findById({ _id: fileId }); - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + if(file.createdBy == userId){ + return NextResponse.json(`Operation not possible!`, { status: 401 }); + } - const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId }); + if (file.createdBy != user._id) { + return NextResponse.json(`Access Denied!!`, { status: 401 }); + } - if (!teamInfo.teamMembers.includes(memberEmail)) { - return new Response("User is not member of the team", { status: 400 }); - } + await FileModel.updateOne( + { _id: fileId }, + { $pull: { readBy: userId } } + ); - if (teamInfo.createdBy !== email) { - return new Response("Only owner can make changes!!", { status: 400 }); + return NextResponse.json('Read access removed!', { status: 200 }); + } catch (err) { + return NextResponse.json(`Err : ${err}`, { status: 500 }); } + } else { + return result; + } +} - const updatedReadBy = Array.isArray(readBy) - ? readBy.filter(writer => writer !== memberEmail) - : []; - - await client.mutation(api.files.updateRead, { _id: fileId, readBy:updatedReadBy }); - - return new Response("Changed to Public!!", { status: 200 }); - } catch (err) { - return new Response(`Error: ${err}`, {status:500}) - } -}; \ No newline at end of file +// Give user read access +export async function POST( + request: Request +) { + + const result = await AuthMiddleware(request); + + if (result instanceof NextResponse) { + + try { + await mongoDB(); + + const {userId, fileId} = await request.json() + + if(!userId || !fileId){ + return NextResponse.json(`Access Denied!!`, {status:404}); + } + + const user: ApiUser = JSON.parse(request.headers.get("user") || "{}"); + + const file1 = await FileModel.findById({_id:fileId}); + + if(file1.createdBy == userId){ + return NextResponse.json(`Operation not possible!`, { status: 401 }); + } + + if(file1.createdBy != user._id){ + return NextResponse.json(`Owner can only change team settings!!`, {status:401}); + } + + await FileModel.updateOne( + { _id: fileId }, + { $push: { readBy: userId } } + ); + + return NextResponse.json('Read access granted!',{status:200}); + } catch (err) { + return NextResponse.json(`Err : ${err}`, {status:500}); + } + } else { + return result; + } +} \ No newline at end of file diff --git a/src/app/api/files/write/route.ts b/src/app/api/files/write/route.ts index 9471fe2..e6de735 100644 --- a/src/app/api/files/write/route.ts +++ b/src/app/api/files/write/route.ts @@ -1,73 +1,98 @@ import { api } from "../../../../../convex/_generated/api"; import { ConvexHttpClient } from "convex/browser"; import { Id } from "../../../../../convex/_generated/dataModel"; +import { AuthMiddleware } from "@/Middleware/AuthMiddleware"; +import { NextResponse } from "next/server"; +import FileModel from "@/models/file"; +import { ApiUser } from "@/types/types"; +import { mongoDB } from "@/lib/MongoDB"; -// Give write read access -export const POST = async (req: Request) => { - try { - const { teamId, email, memberEmail, writtenBy, readBy, fileId } = await req.json(); - if (!teamId || !memberEmail || !email || !fileId || !writtenBy || !readBy) - return new Response("Parameters missing!!", { status: 401 }); +// Remove read access from the user +export async function PUT( + request: Request +) { - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + const result = await AuthMiddleware(request); - const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId as Id<"teams">}); - - if (!teamInfo.teamMembers.includes(memberEmail)) { - return new Response("User is not member of the team", { status: 400 }); - } - - if (teamInfo.createdBy !== email) { - return new Response("Only owner can make changes!!", { status: 400 }); - } - - if(!readBy.includes(memberEmail)){ - readBy.push(memberEmail) - } - - writtenBy.push(memberEmail); - - await client.mutation(api.files.updateRead, { _id: fileId as Id<"files">, readBy:readBy }); - await client.mutation(api.files.updateWrite, { _id: fileId as Id<"files">, writtenBy:writtenBy }); + if (result instanceof NextResponse) { - return new Response("Read Access given!!", { status: 200 }); - } catch (err) { + try { + await mongoDB(); - return new Response(`Error: ${err}`, {status:500}) + const { userId, fileId } = await request.json(); - } -}; + if (!userId || !fileId) { + return NextResponse.json(`Access Denied!!`, { status: 404 }); + } + const user: ApiUser = JSON.parse(request.headers.get("user") || "{}"); -// Remove write access from the user -export const PUT = async (req: Request) => { - try { - const { teamId, email, memberEmail, writtenBy, fileId } = await req.json(); + const file = await FileModel.findById({ _id: fileId }); - if (!teamId || !memberEmail || !email || !fileId || !writtenBy) - return new Response("Parameters missing!!", { status: 401 }); + if(file.createdBy == userId){ + return NextResponse.json(`Operation not possible!`, { status: 401 }); + } - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + if (file.createdBy != user._id) { + return NextResponse.json(`Access Denied!!`, { status: 401 }); + } - const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId }); + await FileModel.updateOne( + { _id: fileId }, + { $pull: { writtenBy: userId } } + ); - if (!teamInfo.teamMembers.includes(memberEmail)) { - return new Response("User is not member of the team", { status: 400 }); + return NextResponse.json('Read access removed!', { status: 200 }); + } catch (err) { + return NextResponse.json(`Err : ${err}`, { status: 500 }); } - - if (teamInfo.createdBy !== email) { - return new Response("Only owner can make changes!!", { status: 400 }); - } - - const updatedWrittenBy = Array.isArray(writtenBy) - ? writtenBy.filter(writer => writer !== memberEmail) - : []; - - await client.mutation(api.files.updateWrite, { _id: fileId, writtenBy:updatedWrittenBy }); - - return new Response("Read access removed!!", { status: 200 }); - } catch (err) { - console.log(err); + } else { + return result; } -}; \ No newline at end of file +} + + +// Give user read access +export async function POST( + request: Request +) { + + const result = await AuthMiddleware(request); + + if (result instanceof NextResponse) { + + try { + await mongoDB(); + + const {userId, fileId} = await request.json() + + if(!userId || !fileId){ + return NextResponse.json(`Access Denied!!`, {status:404}); + } + + const user: ApiUser = JSON.parse(request.headers.get("user") || "{}"); + + const file1 = await FileModel.findById({_id:fileId}); + + if(file1.createdBy == userId){ + return NextResponse.json(`Operation not possible!`, { status: 401 }); + } + + if(file1.createdBy != user._id){ + return NextResponse.json(`Owner can only change team settings!!`, {status:401}); + } + + await FileModel.updateOne( + { _id: fileId }, + { $push: { writtenBy: userId } } + ); + + return NextResponse.json('Read access granted!',{status:200}); + } catch (err) { + return NextResponse.json(`Err : ${err}`, {status:500}); + } + } else { + return result; + } +} \ No newline at end of file diff --git a/src/app/teams/settings/_components/FileList.tsx b/src/app/teams/settings/_components/FileList.tsx index 3bb83ff..6f02077 100644 --- a/src/app/teams/settings/_components/FileList.tsx +++ b/src/app/teams/settings/_components/FileList.tsx @@ -52,13 +52,11 @@ const FileRow = ({ @@ -199,13 +197,11 @@ function FileList({
diff --git a/src/components/shared/ReadAccessModal.tsx b/src/components/shared/ReadAccessModal.tsx index 207bbec..08a55ad 100644 --- a/src/components/shared/ReadAccessModal.tsx +++ b/src/components/shared/ReadAccessModal.tsx @@ -2,8 +2,6 @@ import { EyeIcon } from "lucide-react"; import { Button } from "../ui/button"; import { SetStateAction, useState } from "react"; -import { USER } from "./MemberCarousel"; -import axiosInstance from "@/config/AxiosInstance"; import { updateReadAccessUrl } from "@/lib/API-URLs"; import { AlertDialog, @@ -15,54 +13,52 @@ import { AlertDialogTitle, AlertDialogTrigger, } from "../ui/alert-dialog"; -import { FILE } from "@/app/teams/settings/_components/FileList"; import { RootState } from "@/config/store"; import { useSelector } from "react-redux"; +import { FILE } from "@/types/types"; +import createAxiosInstance from "@/config/AxiosProtectedRoute"; +import { useRouter } from "next/navigation"; type Props = { file: FILE; setIsUpdated: React.Dispatch>; - focusedUser: USER; - teamId: string; + focusedUser: any; }; export default function ReadAccessModal({ file, focusedUser, - teamId, setIsUpdated, }: Props) { const [isSubmitted, setIsSubmitted] = useState(false); const [open, setOpen] = useState(false); const user = useSelector((state:RootState) => state.auth.user); - + const axiosInstance = createAxiosInstance(user.accessToken); + const router = useRouter(); + const [errorMsg,setErrorMsg] = useState(""); const SubmitHandler = async () => { - if (file.readBy && file.readBy.includes(focusedUser.email)) { + if (file.readBy && file.readBy.includes(focusedUser._id)) { try { const res = await axiosInstance.put(`${updateReadAccessUrl}`, { - teamId, - email: user.email, - memberEmail: focusedUser.email, - readBy: file.readBy !== undefined ? file.readBy : [], - fileId: file._id, + userId:focusedUser._id, + fileId: file._id }); if (res.status === 200) setIsSubmitted(true); - } catch (err) { + } catch (err:any) { console.log(err); + setErrorMsg(err.response.data) } } else { try { const res = await axiosInstance.post(`${updateReadAccessUrl}`, { - teamId, - email: user.email, - memberEmail: focusedUser.email, - readBy: file.readBy !== undefined ? file.readBy : [], - fileId: file._id, + userId:focusedUser._id, + fileId: file._id }); if (res.status === 200) setIsSubmitted(true); - } catch (err) { + } catch (err:any) { console.log(err); + setErrorMsg(err.response.data) } } }; @@ -75,7 +71,7 @@ export default function ReadAccessModal({ - {!isSubmitted && ( + {!isSubmitted && errorMsg === "" && ( <> @@ -83,10 +79,10 @@ export default function ReadAccessModal({ - {!file?.readBy?.includes(focusedUser.email) && + {!file?.readBy?.includes(focusedUser._id) && "This will give the read file access to the member!!"} {file.readBy && - file.readBy.includes(focusedUser.email) && + file.readBy.includes(focusedUser._id) && "This will remove the read file access from the member!!"}
@@ -102,10 +98,10 @@ export default function ReadAccessModal({ <> - {!file?.readBy?.includes(focusedUser.email) && + {!file?.readBy?.includes(focusedUser._id) && "Read File Access Granted!!"} {file.readBy && - file.readBy.includes(focusedUser.email) && + file.readBy.includes(focusedUser._id) && "Read File Access Removed!!"} @@ -113,7 +109,29 @@ export default function ReadAccessModal({ { setIsUpdated(true); - setIsSubmitted(false) + setIsSubmitted(false); + router.refresh() + }} + > + Close + + + + )} + +{!isSubmitted && errorMsg !== "" && ( + <> + + + {errorMsg} + + + + { + setIsUpdated(true); + setIsSubmitted(false); + setErrorMsg("") }} > Close diff --git a/src/components/shared/WriteAccessModal.tsx b/src/components/shared/WriteAccessModal.tsx index 2e4c37b..ef58697 100644 --- a/src/components/shared/WriteAccessModal.tsx +++ b/src/components/shared/WriteAccessModal.tsx @@ -12,58 +12,53 @@ import { } from "../ui/alert-dialog"; import { Button } from "../ui/button"; import { SetStateAction, useState } from "react"; -import { FILE } from "@/app/teams/settings/_components/FileList"; -import { USER } from "./MemberCarousel"; -import axiosInstance from "@/config/AxiosInstance"; import { updateWriteAccessUrl } from "@/lib/API-URLs"; import { RootState } from "@/config/store"; import { useSelector } from "react-redux"; +import { FILE } from "@/types/types"; +import createAxiosInstance from "@/config/AxiosProtectedRoute"; +import { useRouter } from "next/navigation"; type Props = { file: FILE; setIsUpdated: React.Dispatch>; - focusedUser: USER; - teamId: string; + focusedUser: any; }; export default function WriteAccessModal({ file, focusedUser, - teamId, setIsUpdated, }:Props) { const [isSubmitted, setIsSubmitted] = useState(false); const [open, setOpen] = useState(false); const user = useSelector((state:RootState) => state.auth.user); - + const axiosInstance = createAxiosInstance(user.accessToken); + const router = useRouter() + const [errorMsg,setErrorMsg] = useState(""); const SubmitHandler = async () => { - if (file.writtenBy && file.writtenBy.includes(focusedUser.email)) { + if (file.writtenBy && file.writtenBy.includes(focusedUser._id)) { try { const res = await axiosInstance.put(`${updateWriteAccessUrl}`, { - teamId, - email: user.email, - memberEmail: focusedUser.email, - writtenBy: file.writtenBy !== undefined ? file.writtenBy : [], fileId: file._id, + userId: focusedUser._id }); if (res.status === 200) setIsSubmitted(true); - } catch (err) { + } catch (err:any) { console.log(err); + setErrorMsg(err.response.data) } } else { try { const res = await axiosInstance.post(`${updateWriteAccessUrl}`, { - teamId, - email: user.email, - memberEmail: focusedUser.email, - writtenBy: file.writtenBy !== undefined ? file.writtenBy : [], fileId: file._id, - readBy:file.readBy + userId: focusedUser._id }); if (res.status === 200) setIsSubmitted(true); - } catch (err) { + } catch (err:any) { console.log(err); + setErrorMsg(err.response.data) } } }; @@ -76,7 +71,7 @@ export default function WriteAccessModal({ - {!isSubmitted && ( + {!isSubmitted && errorMsg === "" && ( <> @@ -84,10 +79,10 @@ export default function WriteAccessModal({ - {!file?.writtenBy?.includes(focusedUser.email) && + {!file?.writtenBy?.includes(focusedUser._id) && "This will give the write file access to the member!!"} {file.writtenBy && - file.writtenBy.includes(focusedUser.email) && + file.writtenBy.includes(focusedUser._id) && "This will remove the write file access from the member!!"}
@@ -103,10 +98,10 @@ export default function WriteAccessModal({ <> - {!file?.writtenBy?.includes(focusedUser.email) && + {!file?.writtenBy?.includes(focusedUser._id) && "Write File Access Granted!!"} {file.writtenBy && - file.writtenBy.includes(focusedUser.email) && + file.writtenBy.includes(focusedUser._id) && "Write File Access Removed!!"} @@ -114,7 +109,8 @@ export default function WriteAccessModal({ { setIsUpdated(true); - setIsSubmitted(false) + setIsSubmitted(false); + router.refresh(); }} > Close @@ -122,6 +118,28 @@ export default function WriteAccessModal({ )} + + {!isSubmitted && errorMsg !== "" && ( + <> + + + {errorMsg} + + + + { + setIsUpdated(true); + setIsSubmitted(false); + setErrorMsg(""); + }} + > + Close + + + + )} + );