Merge branch 'gruppPP' into BumBranch

This commit is contained in:
al8763be 2024-03-19 02:12:37 +01:00
commit e3fd9f52ca
6 changed files with 136 additions and 29 deletions

View file

@ -0,0 +1,34 @@
import { User } from "../Types/goTypes";
import { api, APIResponse } from "../API/API";
/**
* Use to remove a user from the system
* @param props - The username of user to remove
* @returns {boolean} True if removed, false if not
* @example
* const exampleUsername = "user";
* DeleteUser({ usernameToDelete: exampleUsername });
*/
function DeleteUser(props: { usernameToDelete: string }): boolean {
//console.log(props.usernameToDelete); FOR DEBUG
let removed = false;
api
.removeUser(
props.usernameToDelete,
localStorage.getItem("accessToken") ?? "",
)
.then((response: APIResponse<User>) => {
if (response.success) {
removed = true;
} else {
console.error(response.message);
}
})
.catch((error) => {
console.error("An error occurred during creation:", error);
});
return removed;
}
export default DeleteUser;

View file

@ -0,0 +1,54 @@
import { Link } from "react-router-dom";
import Button from "./Button";
import DeleteUser from "./DeleteUser";
import UserProjectListAdmin from "./UserProjectListAdmin";
function UserInfoModal(props: {
isVisible: boolean;
username: string;
onClose: () => void;
}): JSX.Element {
if (!props.isVisible) return <></>;
return (
<div
className="fixed inset-0 bg-black bg-opacity-30 backdrop-blur-sm
flex justify-center items-center"
>
<div className="border-4 border-black bg-white p-2 rounded-lg text-center">
<p className="font-bold text-[30px]">{props.username}</p>
<Link to="/AdminChangeUserName">
<p className="mb-[20px] hover:font-bold hover:cursor-pointer underline">
(Change Username)
</p>
</Link>
<div>
<h2 className="font-bold text-[22px] mb-[20px]">
Member of these projects:
</h2>
<div className="pr-6 pl-6">
<UserProjectListAdmin />
</div>
</div>
<div className="items-center space-x-6 pr-6 pl-6">
<Button
text={"Delete"}
onClick={function (): void {
DeleteUser({ usernameToDelete: props.username });
}}
type="button"
/>
<Button
text={"Close"}
onClick={function (): void {
props.onClose();
}}
type="button"
/>
</div>
</div>
</div>
);
}
export default UserInfoModal;

View file

@ -1,5 +1,6 @@
import { Link } from "react-router-dom"; import { useState } from "react";
import { PublicUser } from "../Types/goTypes"; import { PublicUser } from "../Types/goTypes";
import UserInfoModal from "./UserInfoModal";
/** /**
* The props for the UserProps component * The props for the UserProps component
@ -9,27 +10,52 @@ interface UserProps {
} }
/** /**
* A list of users for admin manage users page, that links admin to the right user page * A list of users for admin manage users page, that sets an onClick
* thanks to the state property * function for eact user <li> element, which displays a modul with
* @param props - The users to display * user info.
* @param props - An array of users users to display
* @returns {JSX.Element} The user list * @returns {JSX.Element} The user list
* @example * @example
* const users = [{ id: 1, userName: "Random name" }]; * const users = [{ id: 1, userName: "ExampleName" }];
* return <UserList users={users} />; * return <UserList users={users} />;
*/ */
export function UserListAdmin(props: UserProps): JSX.Element { export function UserListAdmin(props: UserProps): JSX.Element {
const [modalVisible, setModalVisible] = useState(false);
const [username, setUsername] = useState("");
const handleClick = (username: string): void => {
setUsername(username);
setModalVisible(true);
};
const handleClose = (): void => {
setUsername("");
setModalVisible(false);
};
return ( return (
<div> <>
<ul className="font-bold underline text-[30px] cursor-pointer padding"> <UserInfoModal
{props.users.map((user) => ( onClose={handleClose}
<Link to="/adminUserInfo" key={user.userId} state={user.username}> isVisible={modalVisible}
<li className="pt-5" key={user.userId}> username={username}
/>
<div>
<ul className="font-bold underline text-[30px] cursor-pointer padding">
{props.users.map((user) => (
<li
className="pt-5"
key={user.userId}
onClick={() => {
handleClick(user.username);
}}
>
{user.username} {user.username}
</li> </li>
</Link> ))}
))} </ul>
</ul> </div>
</div> </>
); );
} }

View file

@ -1,17 +1,17 @@
import React, { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { api } from "../API/API"; import { api } from "../API/API";
import { Project } from "../Types/goTypes"; import { Project } from "../Types/goTypes";
const UserProjectListAdmin: React.FC = () => { function UserProjectListAdmin(): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]); const [projects, setProjects] = useState<Project[]>([]);
useEffect(() => { useEffect(() => {
const fetchProjects = async (): Promise<void> => { const fetchProjects = async (): Promise<void> => {
try { try {
const token = localStorage.getItem("accessToken") ?? ""; const token = localStorage.getItem("accessToken") ?? "";
const username = "NoUser"; // getUsernameFromContext(); // Assuming you have a function to get the username from your context // const username = props.username;
const response = await api.getUserProjects(username, token); const response = await api.getUserProjects(token);
if (response.success) { if (response.success) {
setProjects(response.data ?? []); setProjects(response.data ?? []);
} else { } else {
@ -26,18 +26,16 @@ const UserProjectListAdmin: React.FC = () => {
}, []); }, []);
return ( return (
<div> <div className="border-2 border-black bg-white p-2 rounded-lg text-center">
<h2>User Projects</h2>
<ul> <ul>
{projects.map((project) => ( {projects.map((project) => (
<li key={project.id}> <li key={project.id}>
<span>{project.name}</span> <span>{project.name}</span>
{/* Add any additional project details you want to display */}
</li> </li>
))} ))}
</ul> </ul>
</div> </div>
); );
}; }
export default UserProjectListAdmin; export default UserProjectListAdmin;

View file

@ -1,3 +1,4 @@
import BackButton from "../../Components/BackButton";
import BasicWindow from "../../Components/BasicWindow"; import BasicWindow from "../../Components/BasicWindow";
import Button from "../../Components/Button"; import Button from "../../Components/Button";
@ -13,13 +14,7 @@ function AdminChangeUsername(): JSX.Element {
}} }}
type="button" type="button"
/> />
<Button <BackButton />
text="Back"
onClick={(): void => {
return;
}}
type="button"
/>
</> </>
); );