From fcd0b23bb4b94f4302b6a68200d868dfb31c3164 Mon Sep 17 00:00:00 2001 From: kemboi590 Date: Mon, 22 Jul 2024 23:26:57 +0300 Subject: [PATCH] can now change user roles --- src/features/users/usersAPI.ts | 2 +- src/pages/dashboard/main/Account.tsx | 38 ++++++++++++------- .../ManageVehicles/CreateVehicleSpecs.tsx | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/features/users/usersAPI.ts b/src/features/users/usersAPI.ts index a921217..6e94433 100644 --- a/src/features/users/usersAPI.ts +++ b/src/features/users/usersAPI.ts @@ -7,7 +7,7 @@ export interface TUser { contact_phone: string; address: string; image_url: string; - role: "user" | "admin" | "both" | "disabled" | null; + role: "user" | "admin"; password: string; } diff --git a/src/pages/dashboard/main/Account.tsx b/src/pages/dashboard/main/Account.tsx index b698c29..875a085 100644 --- a/src/pages/dashboard/main/Account.tsx +++ b/src/pages/dashboard/main/Account.tsx @@ -4,7 +4,7 @@ import { Toaster, toast } from 'sonner'; function Account() { const { data: usersData, isLoading: usersLoading, error: usersError, refetch: refetchUsers } = usersAPI.useGetUsersQuery(); - const [isDisabling, setIsDisabling] = useState(null); + const [isUpdatingRole, setIsUpdatingRole] = useState(null); const [updateUser] = usersAPI.useUpdateUserMutation(); const [filters, setFilters] = useState({ @@ -29,17 +29,17 @@ function Account() { }); }; - const handleDisableUser = async (userId: number) => { - setIsDisabling(userId); + const handleRoleChange = async (userId: number, newRole: "user" | "admin") => { + setIsUpdatingRole(userId); try { - await updateUser({ id: userId, role: 'disabled' }); - toast.success('User disabled successfully'); + await updateUser({ id: userId, role: newRole }); + toast.success('User role updated successfully'); refetchUsers(); } catch (error) { - console.error('Error disabling user', error); - toast.error('Error disabling user'); + console.error('Error updating user role', error); + toast.error('Error updating user role'); } finally { - setIsDisabling(null); + setIsUpdatingRole(null); } }; @@ -116,6 +116,7 @@ function Account() { Email Contact Phone Address + Role Action @@ -127,19 +128,30 @@ function Account() { {user.email} {user.contact_phone} {user.address} + + + diff --git a/src/pages/dashboard/main/ManageVehicles/CreateVehicleSpecs.tsx b/src/pages/dashboard/main/ManageVehicles/CreateVehicleSpecs.tsx index d1a3df3..9d7b583 100644 --- a/src/pages/dashboard/main/ManageVehicles/CreateVehicleSpecs.tsx +++ b/src/pages/dashboard/main/ManageVehicles/CreateVehicleSpecs.tsx @@ -191,7 +191,7 @@ const CreateVehicle = () => {

{errors.features?.message}

- {/* Image upload input */} + {/* Imag e upload input */}