diff --git a/frontend/components/admin-user-management/admin-edit-user-modal.tsx b/frontend/components/admin-user-management/admin-edit-user-modal.tsx index c33a1868ff..c99d9cb820 100644 --- a/frontend/components/admin-user-management/admin-edit-user-modal.tsx +++ b/frontend/components/admin-user-management/admin-edit-user-modal.tsx @@ -14,6 +14,8 @@ import { Label } from "../ui/label"; import { updateUser } from "@/lib/update-user"; import { useAuth } from "@/app/auth/auth-context"; import { useToast } from "@/components/hooks/use-toast"; +import { Switch } from "../ui/switch"; +import { updateUserPrivilege } from "@/lib/update-user-privilege"; import { User } from "@/lib/schemas/user-schema"; interface AdminEditUserModalProps extends React.HTMLProps { @@ -33,6 +35,7 @@ const AdminEditUserModal: React.FC = ({ id?: string; username?: string; email?: string; + isAdmin?: boolean; } | undefined >(); @@ -49,6 +52,19 @@ const AdminEditUserModal: React.FC = ({ const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); + updateInfo() + .then(() => updatePrivilege()) + .then(() => { + toast({ + title: "Success", + description: "User updated successfully!", + }); + closeModal(); + }) + .catch(() => null); + }; + + const updateInfo = async () => { if (!auth?.token) { // Will not reach this point as button is disabled // when token is missing @@ -75,19 +91,10 @@ const AdminEditUserModal: React.FC = ({ editingUser?.username, editingUser?.email ); - if (!response.ok) { - toast({ - title: "Unknown Error", - description: "An unexpected error has occurred", - }); - } switch (response.status) { case 200: - toast({ - title: "Success", - description: "User updated successfully!", - }); - break; + props.onUserUpdate(); + return; case 400: // In theory, they should never be able to send out a request // with missing fields due to disabled submission button @@ -95,48 +102,118 @@ const AdminEditUserModal: React.FC = ({ title: "Missing Fields", description: "Please fill in at least 1 field", }); - return; + break; case 401: toast({ title: "Access denied", description: "Invalid session", }); - return; + break; case 403: toast({ title: "Access denied", description: "Only admins can update other user", }); - return; + break; case 404: toast({ title: "User not found", description: "User with specified ID not found", }); - return; + break; case 409: toast({ title: "Duplicated Username or Email", description: "The username or email you entered is already in use", }); - return; + break; case 500: toast({ title: "Server Error", description: "The server encountered an error", }); - return; + break; default: toast({ title: "Unknown Error", description: "An unexpected error has occured", }); - return; + break; + } + throw new Error("Update user info request failed"); + }; + + const updatePrivilege = async () => { + if (!auth?.token) { + // Will not reach this point as button is disabled + // when token is missing + toast({ + title: "Access denied", + description: "No authentication token found", + }); + return; + } + + if (!editingUser?.id) { + // Will not reach this point as button is disabled + // when editing user's id is missing + toast({ + title: "Invalid selection", + description: "No user selected", + }); + return; } - // Remove old states, update UI and close modal - props.onUserUpdate(); - closeModal(); + if (editingUser?.isAdmin == null) { + // Will not reach this point as button is disabled + // when field is missing + toast({ + title: "Invalid selection", + description: "No user selected", + }); + return; + } + + const response = await updateUserPrivilege( + auth.token, + editingUser?.id, + editingUser?.isAdmin + ); + + switch (response.status) { + case 200: + props.onUserUpdate(); + return; + case 400: + // In theory, they should never be able to send out a request + // with missing fields due to disabled submission button + break; + case 401: + toast({ + title: "Access denied", + description: "Invalid session", + }); + break; + case 403: + toast({ + title: "Access denied", + description: "Only admins can update other user", + }); + break; + case 404: + toast({ + title: "User not found", + description: "User with specified ID not found", + }); + break; + default: + toast({ + title: "Unknown Error", + description: "An unexpected error has occured", + }); + break; + } + throw new Error("Update user privilege request failed"); }; return ( @@ -177,6 +254,19 @@ const AdminEditUserModal: React.FC = ({ required /> +
+
+ +
+ + setEditingUser({ ...editingUser, isAdmin: e }) + } + required + /> +