Component for getting all users
This commit is contained in:
parent
8300fb3a6f
commit
cc231dbfaa
1 changed files with 35 additions and 0 deletions
35
frontend/src/Components/GetAllUsers.tsx
Normal file
35
frontend/src/Components/GetAllUsers.tsx
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Dispatch, useEffect } from "react";
|
||||
import { api } from "../API/API";
|
||||
|
||||
/**
|
||||
* Gets all usernames in the system and puts them in an array
|
||||
* @param props - A setStateAction for the array you want to put users in
|
||||
* @returns {void} Nothing
|
||||
* @example
|
||||
* const [users, setUsers] = useState<string[]>([]);
|
||||
* GetAllUsers({ setUsersProp: setUsers });
|
||||
*/
|
||||
function GetAllUsers(props: {
|
||||
setUsersProp: Dispatch<React.SetStateAction<string[]>>;
|
||||
}): void {
|
||||
const setUsers: Dispatch<React.SetStateAction<string[]>> = props.setUsersProp;
|
||||
useEffect(() => {
|
||||
const fetchUsers = async (): Promise<void> => {
|
||||
try {
|
||||
const token = localStorage.getItem("accessToken") ?? "";
|
||||
const response = await api.getAllUsers(token);
|
||||
if (response.success) {
|
||||
setUsers(response.data ?? []);
|
||||
} else {
|
||||
console.error("Failed to fetch users:", response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error fetching users:", error);
|
||||
}
|
||||
};
|
||||
|
||||
void fetchUsers();
|
||||
}, [setUsers]);
|
||||
}
|
||||
|
||||
export default GetAllUsers;
|
Loading…
Reference in a new issue