TTime/frontend/src/Components/GetUsersInProject.tsx

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-03-20 16:59:54 +01:00
import { Dispatch, useEffect } from "react";
import { api } from "../API/API";
2024-03-31 20:54:00 +02:00
export interface ProjectMember {
Username: string;
UserRole: string;
}
2024-03-20 16:59:54 +01:00
/**
2024-04-02 13:14:08 +02:00
* Gets all members of a project
* @param string - The project's name
* @param Dispatch - A setStateAction for the array you want to put members in
2024-03-20 16:59:54 +01:00
* @returns {void} Nothing
* @example
2024-04-02 13:14:08 +02:00
* const [users, setUsers] = useState<User[]>([]);
* GetUsersInProject({ projectName: props.projectname, setUsersProp: setUsers });
2024-03-20 16:59:54 +01:00
*/
function GetUsersInProject(props: {
projectName: string;
2024-03-31 20:54:00 +02:00
setUsersProp: Dispatch<React.SetStateAction<ProjectMember[]>>;
2024-03-20 16:59:54 +01:00
}): void {
2024-03-31 20:54:00 +02:00
const setUsers: Dispatch<React.SetStateAction<ProjectMember[]>> =
2024-03-20 16:59:54 +01:00
props.setUsersProp;
useEffect(() => {
const fetchUsers = async (): Promise<void> => {
try {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getAllUsersProject(props.projectName, token);
if (response.success) {
setUsers(response.data ?? []);
} else {
2024-04-02 13:14:08 +02:00
console.error("Failed to fetch members:", response.message);
2024-03-20 16:59:54 +01:00
}
} catch (error) {
2024-04-02 13:14:08 +02:00
console.error("Error fetching members:", error);
2024-03-20 16:59:54 +01:00
}
};
void fetchUsers();
}, [props.projectName, setUsers]);
}
export default GetUsersInProject;