add user management features: implement user creation, editing, and deletion; enhance dashboard with user selection prompt; improve token verification and alert handling
This commit is contained in:
11
admin/src/utils/fetcher.ts
Normal file
11
admin/src/utils/fetcher.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export const fetchUserData = async () => {
|
||||
const response = await fetch("http://localhost:8002/api/allUsers", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||
},
|
||||
});
|
||||
const data = await response.json();
|
||||
return data;
|
||||
};
|
77
admin/src/utils/userActions.ts
Normal file
77
admin/src/utils/userActions.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export const handleDelete = async (userId: number) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`http://localhost:8002/api/deleteUser/${userId}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete user");
|
||||
}
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Error deleting user:", error);
|
||||
return { success: false };
|
||||
}
|
||||
};
|
||||
|
||||
export const handleEdit = async (
|
||||
userId: number,
|
||||
username: string,
|
||||
role: string,
|
||||
password: string
|
||||
) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`http://localhost:8002/api/editUser/${userId}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||
},
|
||||
body: JSON.stringify({ username, role, password }),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to edit user");
|
||||
}
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Error editing user:", error);
|
||||
return { success: false };
|
||||
}
|
||||
};
|
||||
|
||||
export const createUser = async (
|
||||
username: string,
|
||||
role: number,
|
||||
password: string
|
||||
) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`http://localhost:8002/api/createUser`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||
},
|
||||
body: JSON.stringify({ username, role, password }),
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to create user");
|
||||
}
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Error creating user:", error);
|
||||
return { success: false };
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user