@@ -2,16 +2,12 @@ import axiosInstance from '@/helpers/axios-instance';
2
2
import { useEffect , useState } from 'react' ;
3
3
import { toast } from 'react-toastify' ;
4
4
import 'react-toastify/dist/ReactToastify.css' ;
5
-
6
- enum role {
7
- admin = 'ADMIN' ,
8
- user = 'USER' ,
9
- }
5
+ import { Role } from '@/types/role-type' ;
10
6
11
7
type User = {
12
8
_id : string ;
13
9
fullName : string ;
14
- role : role ;
10
+ role : Role ;
15
11
email : string ;
16
12
} ;
17
13
@@ -27,9 +23,9 @@ const AdminUsers = () => {
27
23
}
28
24
} ;
29
25
30
- const handleClick = async ( userId : string , role : role ) => {
26
+ const handleClick = async ( userId : string , role : Role ) => {
31
27
try {
32
- const response = await axiosInstance . patch ( '/api/user/' + userId , { role : role } ) ;
28
+ const response = await axiosInstance . patch ( '/api/user/' + userId , { role } ) ;
33
29
if ( response . status === 200 ) {
34
30
fetchData ( ) ;
35
31
toast . success ( 'User updated successfully!' ) ;
@@ -44,48 +40,44 @@ const AdminUsers = () => {
44
40
} , [ ] ) ;
45
41
46
42
return (
47
- < >
48
- < div className = "w-full p-3 px-5 sm:p-12" >
49
- < h1 className = "absolute left-16 top-3 text-2xl font-bold text-light-title dark:text-dark-title sm:static" >
50
- Users
51
- </ h1 >
52
- < div className = "mt-2 sm:mt-12" >
53
- { users ?. map ( ( user : User ) => {
54
- return (
55
- < div
56
- key = { user ?. _id }
57
- className = "mb-3 flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card"
43
+ < div className = "w-full p-3 px-5 sm:p-12" >
44
+ < h1 className = "absolute left-16 top-3 text-2xl font-bold text-light-title dark:text-dark-title sm:static" >
45
+ Users
46
+ </ h1 >
47
+ < div className = "mt-2 sm:mt-12" >
48
+ { users ?. map ( ( user : User ) => (
49
+ < div
50
+ key = { user ?. _id }
51
+ className = "mb-3 flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card"
52
+ >
53
+ < div className = "flex flex-col gap-[10px]" >
54
+ < p className = "text-base font-medium text-light-title dark:text-dark-title" >
55
+ { user ?. fullName }
56
+ </ p >
57
+ < p className = "text-base font-medium text-light-description dark:text-dark-description" >
58
+ { user ?. email }
59
+ </ p >
60
+ </ div >
61
+ { user . role === Role . Admin && (
62
+ < button
63
+ onClick = { ( ) => handleClick ( user . _id , Role . User ) }
64
+ className = "h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white"
65
+ >
66
+ Admin
67
+ </ button >
68
+ ) }
69
+ { user . role === Role . User && (
70
+ < button
71
+ onClick = { ( ) => handleClick ( user . _id , Role . Admin ) }
72
+ className = "h-fit rounded-xl border border-black bg-transparent px-4 py-2 text-sm font-semibold text-black dark:text-white"
58
73
>
59
- < div className = "flex flex-col gap-[10px] " >
60
- < p className = "text-base font-medium text-light-title dark:text-dark-title" >
61
- { user ?. fullName }
62
- </ p >
63
- < p className = "text-base font-medium text-light-description dark:text-dark-description" >
64
- { user ?. email }
65
- </ p >
66
- </ div >
67
- { user . role === role . admin && (
68
- < button
69
- onClick = { ( ) => handleClick ( user . _id , role . user ) }
70
- className = "h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white"
71
- >
72
- Admin
73
- </ button >
74
- ) }
75
- { user . role === role . user && (
76
- < button
77
- onClick = { ( ) => handleClick ( user . _id , role . admin ) }
78
- className = "h-fit rounded-xl border border-black bg-transparent px-4 py-2 text-sm font-semibold text-white"
79
- >
80
- Admin
81
- </ button >
82
- ) }
83
- </ div >
84
- ) ;
85
- } ) }
86
- </ div >
74
+ User
75
+ </ button >
76
+ ) }
77
+ </ div >
78
+ ) ) }
87
79
</ div >
88
- </ >
80
+ </ div >
89
81
) ;
90
82
} ;
91
83
0 commit comments