Merge branch 'frontend' into gruppDM
This commit is contained in:
commit
7ed9398bcb
13 changed files with 149 additions and 38 deletions
|
@ -58,6 +58,8 @@ interface API {
|
|||
getUserProjects(token: string): Promise<APIResponse<Project[]>>;
|
||||
/** Gets a project from id*/
|
||||
getProject(id: number): Promise<APIResponse<Project>>;
|
||||
/** Gets a project from id*/
|
||||
getAllUsers(token: string): Promise<APIResponse<string[]>>;
|
||||
}
|
||||
|
||||
// Export an instance of the API
|
||||
|
@ -94,7 +96,7 @@ export const api: API = {
|
|||
token: string,
|
||||
): Promise<APIResponse<User>> {
|
||||
try {
|
||||
const response = await fetch("/api/userdelete", {
|
||||
const response = await fetch(`/api/userdelete/${username}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
@ -352,4 +354,32 @@ export const api: API = {
|
|||
};
|
||||
}
|
||||
},
|
||||
|
||||
// Gets all users
|
||||
async getAllUsers(token: string): Promise<APIResponse<string[]>> {
|
||||
try {
|
||||
const response = await fetch("/api/users/all", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + token,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return Promise.resolve({
|
||||
success: false,
|
||||
message: "Failed to get users",
|
||||
});
|
||||
} else {
|
||||
const data = (await response.json()) as string[];
|
||||
return Promise.resolve({ success: true, data });
|
||||
}
|
||||
} catch (e) {
|
||||
return Promise.resolve({
|
||||
success: false,
|
||||
message: "API is not ok",
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
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;
|
|
@ -32,16 +32,11 @@ function LoginCheck(props: {
|
|||
prevAuth = 1;
|
||||
return prevAuth;
|
||||
});
|
||||
} else if (token !== "" && props.username === "pm") {
|
||||
} else if (token !== "") {
|
||||
props.setAuthority((prevAuth) => {
|
||||
prevAuth = 2;
|
||||
return prevAuth;
|
||||
});
|
||||
} else if (token !== "" && props.username === "user") {
|
||||
props.setAuthority((prevAuth) => {
|
||||
prevAuth = 3;
|
||||
return prevAuth;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.error("Token was undefined");
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
import { useState } from "react";
|
||||
import { PublicUser } from "../Types/goTypes";
|
||||
import UserInfoModal from "./UserInfoModal";
|
||||
|
||||
/**
|
||||
* The props for the UserProps component
|
||||
*/
|
||||
interface UserProps {
|
||||
users: PublicUser[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A list of users for admin manage users page, that sets an onClick
|
||||
* function for eact user <li> element, which displays a modul with
|
||||
|
@ -20,7 +12,7 @@ interface UserProps {
|
|||
* return <UserList users={users} />;
|
||||
*/
|
||||
|
||||
export function UserListAdmin(props: UserProps): JSX.Element {
|
||||
export function UserListAdmin(props: { users: string[] }): JSX.Element {
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [username, setUsername] = useState("");
|
||||
|
||||
|
@ -46,12 +38,12 @@ export function UserListAdmin(props: UserProps): JSX.Element {
|
|||
{props.users.map((user) => (
|
||||
<li
|
||||
className="pt-5"
|
||||
key={user.userId}
|
||||
key={user}
|
||||
onClick={() => {
|
||||
handleClick(user.username);
|
||||
handleClick(user);
|
||||
}}
|
||||
>
|
||||
{user.username}
|
||||
{user}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
|
|
@ -2,15 +2,13 @@ import BasicWindow from "../../Components/BasicWindow";
|
|||
import Button from "../../Components/Button";
|
||||
import BackButton from "../../Components/BackButton";
|
||||
import { UserListAdmin } from "../../Components/UserListAdmin";
|
||||
import { PublicUser } from "../../Types/goTypes";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import GetAllUsers from "../../Components/GetAllUsers";
|
||||
import { useState } from "react";
|
||||
|
||||
function AdminManageUsers(): JSX.Element {
|
||||
//TODO: Change so that it reads users from database
|
||||
const users: PublicUser[] = [];
|
||||
for (let i = 1; i <= 20; i++) {
|
||||
users.push({ userId: "id" + i, username: "Example User " + i });
|
||||
}
|
||||
const [users, setUsers] = useState<string[]>([]);
|
||||
GetAllUsers({ setUsersProp: setUsers });
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
|
|
|
@ -11,8 +11,6 @@ function App(): JSX.Element {
|
|||
if (authority === 1) {
|
||||
navigate("/admin");
|
||||
} else if (authority === 2) {
|
||||
navigate("/pm");
|
||||
} else if (authority === 3) {
|
||||
navigate("/yourProjects");
|
||||
}
|
||||
}, [authority, navigate]);
|
||||
|
|
|
@ -43,10 +43,6 @@ const router = createBrowserRouter([
|
|||
path: "/admin",
|
||||
element: <AdminMenuPage />,
|
||||
},
|
||||
{
|
||||
path: "/pm",
|
||||
element: <YourProjectsPage />,
|
||||
},
|
||||
{
|
||||
path: "/yourProjects",
|
||||
element: <YourProjectsPage />,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue