diff --git a/src/app/api/teams/members/[id]/route.ts b/src/app/api/teams/members/[id]/route.ts index ccd9cd0..99488fa 100644 --- a/src/app/api/teams/members/[id]/route.ts +++ b/src/app/api/teams/members/[id]/route.ts @@ -6,16 +6,17 @@ export async function GET( request: Request, { params }: { params: { id: string } } ) { - const {id} = params; - - - if (!id) return new Response('Parameters missing!!',{status: 401}); - - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); - - const teamInfo = await client.query(api.teams.getTeamById,{_id:id as Id<"teams">}); - - const memberDataPromises = teamInfo.teamMembers.map((mem:string) => + const { id } = params; + + if (!id) return new Response("Parameters missing!!", { status: 401 }); + + const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + + const teamInfo = await client.query(api.teams.getTeamById, { + _id: id as Id<"teams">, + }); + + const memberDataPromises = teamInfo.teamMembers.map((mem: string) => client.query(api.user.getUser, { email: mem }) ); @@ -23,6 +24,31 @@ export async function GET( const memberData = results.flatMap((result) => result || []); - return Response.json({memberData}); + return Response.json({ memberData }); +} + +export async function PUT( + request: Request, + { params }: { params: { id: string } } +) { + const { id } = params; + + const {email} = await request.json(); + + if (!id) return new Response("Parameters missing!!", { status: 401 }); + + const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + + const teamInfo = await client.query(api.teams.getTeamById,{_id:id as Id<"teams">}); + + if(!teamInfo.teamMembers.includes(email)) return new Response("Not the member of team!!", { status: 404 }); + + if(teamInfo.createdBy === email) return new Response("Owner of the team!!", { status: 404 }); + + const updatedTeamMembers = teamInfo.teamMembers.filter((writer: any) => writer !== email); + + await client.mutation(api.teams.addMember, { _id: id as Id<"teams">, memberArray:updatedTeamMembers }); + + return new Response("Member removd!!", { status: 200 }); } diff --git a/src/app/dashboard/_components/SideNavTopSection.tsx b/src/app/dashboard/_components/SideNavTopSection.tsx index 113b02d..0afd70f 100644 --- a/src/app/dashboard/_components/SideNavTopSection.tsx +++ b/src/app/dashboard/_components/SideNavTopSection.tsx @@ -48,6 +48,8 @@ function SideNavTopSection({ user, setActiveTeamInfo }: any) { const [teamMembersData, setTeamData] = useState([]); const [ActiveTeamMembers, setActiveTeamMembers] = useState([]); + console.log(activeTeam) + useEffect(() => { user && getTeamList(); }, [user]); diff --git a/src/components/shared/DeleteTeamMember.tsx b/src/components/shared/DeleteTeamMember.tsx new file mode 100644 index 0000000..196dcfb --- /dev/null +++ b/src/components/shared/DeleteTeamMember.tsx @@ -0,0 +1,113 @@ +"use client"; +import React, { useState } from "react"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "../ui/alert-dialog"; +import { Button } from "../ui/button"; +import { CheckCircle2, Trash2 } from "lucide-react"; +import axiosInstance from "@/config/AxiosInstance"; +import { deleteTeamMemberUrl } from "@/lib/API-URLs"; +import { useSelector } from "react-redux"; +import { RootState } from "@/app/store"; +import { useRouter } from "next/navigation"; + +type Props = { + email: string; +}; + +export default function DeleteTeamMember({ email }: Props) { + const [isSubmitted, setIsSubmitted] = useState(false); + const router = useRouter(); + const teamId = useSelector((state: RootState) => state.team.teamId); + const [isError,setIsError] = useState(false); + const [errorMsg, setErrorMsg] = useState(""); + + const DeleteHandler = async () => { + try { + axiosInstance.put(`${deleteTeamMemberUrl}/${teamId}`, { email }) + .then((res)=>{ + if(res.status === 200) setIsSubmitted(true); + }).catch((err)=>{ + setIsError(true); + setErrorMsg(err.response.data) + }) + } catch (err:any) { + setIsError(true); + setErrorMsg(err.response.data) + } + }; + + return ( + + + + + + {!isSubmitted && !isError && ( + <> + + Are you absolutely sure? + + This action cannot be undone. This will remove the member from + the team and files created by the member will be permanently + deleted. + + + + Cancel + + + + )} + {isSubmitted && ( + <> + + +

Team Member Removed Successfully!!

{" "} + +
+
+ + { + router.push('/dashboard') + }} + > + Continue + + + + )} + {isError && ( + <> + + +

{errorMsg}

{" "} + +
+
+ + { + router.push('/dashboard') + }} + > + Continue + + + + )} +
+
+ ); +} diff --git a/src/components/shared/MemberCarousel.tsx b/src/components/shared/MemberCarousel.tsx index 494a0f6..3ee2c51 100644 --- a/src/components/shared/MemberCarousel.tsx +++ b/src/components/shared/MemberCarousel.tsx @@ -13,9 +13,8 @@ import { CardHeader, } from "@/components/ui/card"; import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar"; -import { Button } from "../ui/button"; -import { Trash2 } from "lucide-react"; -import { SetStateAction, useState } from "react"; +import { SetStateAction } from "react"; +import DeleteTeamMember from "./DeleteTeamMember"; export type USER = { name: string; @@ -31,7 +30,6 @@ type Props = { }; export default function MemberCarousel({ teamMembersData,focusedUser,setFocusedUser }: Props) { - return (
@@ -60,12 +58,7 @@ export default function MemberCarousel({ teamMembersData,focusedUser,setFocusedU

Email : {user.email}

- +
diff --git a/src/lib/API-URLs.ts b/src/lib/API-URLs.ts index dd55a95..b7f52e7 100644 --- a/src/lib/API-URLs.ts +++ b/src/lib/API-URLs.ts @@ -1,3 +1,4 @@ export const changeToPrivateUrl = "/api/files/private" export const changeToPublicUrl = "/api/files/public" -export const getTeamMembersData = "/api/teams/members"; \ No newline at end of file +export const getTeamMembersData = "/api/teams/members"; +export const deleteTeamMemberUrl = "/api/teams/members"; \ No newline at end of file