From a266a6f7fce56b859e653ac4d041afbff2004dd6 Mon Sep 17 00:00:00 2001 From: Davenludd Date: Tue, 9 Apr 2024 10:59:27 +0200 Subject: [PATCH] Update ProjectMembers component to allow deleting users from a project --- frontend/src/Components/ProjectMembers.tsx | 75 ++++++++++++++++------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/frontend/src/Components/ProjectMembers.tsx b/frontend/src/Components/ProjectMembers.tsx index 52e8559..cffd7f8 100644 --- a/frontend/src/Components/ProjectMembers.tsx +++ b/frontend/src/Components/ProjectMembers.tsx @@ -1,6 +1,7 @@ import { useState } from "react"; import { Link, useParams } from "react-router-dom"; import GetUsersInProject, { ProjectMember } from "./GetUsersInProject"; +import { api } from "../API/API"; function ProjectMembers(): JSX.Element { const { projectName } = useParams(); @@ -11,34 +12,68 @@ function ProjectMembers(): JSX.Element { setUsersProp: setProjectMembers, }); + const handleUserDeleteClick = async (username: string): Promise => { + const token = localStorage.getItem("accessToken") ?? ""; + const response = await api.removeUserFromProject( + username, + projectName ?? "", + token, + ); + console.log(response.data); + + // Remove the deleted user from the state + setProjectMembers((prevMembers) => + prevMembers.filter((member) => member.Username !== username), + ); + }; + return ( <>

All Members In: {projectName}{" "}

- {projectMembers.map((projectMember: ProjectMember, index: number) => ( -

-
-
-

{projectMember.Username}

- Role: -

{projectMember.UserRole}

-
-
-
- -

- View Reports -

- + {projectMembers.map((projectMember: ProjectMember, index: number) => { + if (projectMember.Username === "admin") { + return null; // Skip rendering for admin user + } + return ( +

+
+
+

{projectMember.Username}

+ Role: +

{projectMember.UserRole}

+
+
+
+ {projectMember.Username !== + localStorage.getItem("username") && ( +

{ + confirm( + "Are you sure you want to delete this user?", + ) && + void handleUserDeleteClick(projectMember.Username); + }} + > + Delete User +

+ )} + +

+ View Reports +

+ +
-

-

- ))} + + ); + })}
);