@@ -5,7 +5,7 @@ import { RiLoader4Fill } from "react-icons/ri"
55import Modal from "../components/modal"
66import Select from "../components/select"
77
8- function Logout ( ) {
8+ function Admin ( ) {
99 const accessToken = useSelector ( ( state ) => state . accessToken )
1010 const loggedUser = useSelector ( ( state ) => state . user ) ;
1111 const [ users , setUsers ] = useState ( null )
@@ -37,7 +37,12 @@ function Logout() {
3737
3838 async function createUser ( ) {
3939 try {
40+ if ( ! users ) return
4041 if ( ! username || ! email || ! password ) return
42+ if ( users . find ( ( user ) => user . username === username ) ) {
43+ toast . error ( 'An user with the same username already exists!' )
44+ return
45+ }
4146 const response = await fetch ( `${ import . meta. env . VITE_API_URL || "/api" } /users` , {
4247 method : 'POST' ,
4348 headers : {
@@ -66,61 +71,61 @@ function Logout() {
6671 return (
6772 < div className = "flex flex-col justify-center items-center gap-3" >
6873 < h1 className = "text-3xl" > Admin panel</ h1 >
69- < h2 className = "text-xl mt-4" > Create an user</ h2 >
70- < div className = "w-96 max-w-full" >
71- < label className = "input-label" htmlFor = "username" >
72- Username
73- </ label >
74- < input
75- className = "input-field"
76- id = "username"
77- type = "username"
78- placeholder = "Username"
79- value = { username }
80- onChange = { ( e ) => setUsername ( e . target . value ) }
81- onKeyDown = { ( e ) => e . key === 'Enter' && emailInput . current . focus ( ) }
82- />
83- </ div >
84- < div className = "w-96 max-w-full" >
85- < label className = "input-label" htmlFor = "email" >
86- Email
87- </ label >
88- < input
89- ref = { emailInput }
90- className = "input-field"
91- id = "email"
92- type = "email"
93- placeholder = "Email"
94- value = { email }
95- onChange = { ( e ) => setEmail ( e . target . value ) }
96- onKeyDown = { ( e ) => e . key === 'Enter' && passwordInput . current . focus ( ) }
97- />
98- </ div >
99- < div className = "w-96 max-w-full" >
100- < label className = "input-label" htmlFor = "password" >
101- Password
102- </ label >
103- < input
104- ref = { passwordInput }
105- className = "input-field"
106- id = "password"
107- type = "password"
108- placeholder = "Password"
109- value = { password }
110- onChange = { ( e ) => setPassword ( e . target . value ) }
111- onKeyDown = { ( e ) => e . key === 'Enter' && createUser ( ) }
112- />
113- </ div >
114- < button className = "button max-w-full" onClick = { ( ) => createUser ( ) } >
115- Create user
116- </ button >
11774 < h2 className = "text-xl mt-6" > Users list</ h2 >
11875 { ! users && (
11976 < div className = "flex justify-center items-center" >
12077 < RiLoader4Fill className = "text-3xl animate-spin" />
12178 </ div >
12279 ) }
123- { users && (
80+ { users && < >
81+ < h2 className = "text-xl mt-4" > Create an user</ h2 >
82+ < div className = "w-96 max-w-full" >
83+ < label className = "input-label" htmlFor = "username" >
84+ Username
85+ </ label >
86+ < input
87+ className = "input-field"
88+ id = "username"
89+ type = "username"
90+ placeholder = "Username"
91+ value = { username }
92+ onChange = { ( e ) => setUsername ( e . target . value ) }
93+ onKeyDown = { ( e ) => e . key === 'Enter' && emailInput . current . focus ( ) }
94+ />
95+ </ div >
96+ < div className = "w-96 max-w-full" >
97+ < label className = "input-label" htmlFor = "email" >
98+ Email
99+ </ label >
100+ < input
101+ ref = { emailInput }
102+ className = "input-field"
103+ id = "email"
104+ type = "email"
105+ placeholder = "Email"
106+ value = { email }
107+ onChange = { ( e ) => setEmail ( e . target . value ) }
108+ onKeyDown = { ( e ) => e . key === 'Enter' && passwordInput . current . focus ( ) }
109+ />
110+ </ div >
111+ < div className = "w-96 max-w-full" >
112+ < label className = "input-label" htmlFor = "password" >
113+ Password
114+ </ label >
115+ < input
116+ ref = { passwordInput }
117+ className = "input-field"
118+ id = "password"
119+ type = "password"
120+ placeholder = "Password"
121+ value = { password }
122+ onChange = { ( e ) => setPassword ( e . target . value ) }
123+ onKeyDown = { ( e ) => e . key === 'Enter' && createUser ( ) }
124+ />
125+ </ div >
126+ < button className = "button max-w-full" onClick = { ( ) => createUser ( ) } >
127+ Create user
128+ </ button >
124129 < div className = "mt-2 rounded-lg border border-gray-600 p-4" >
125130 < div className = "flex flex-col gap-1" >
126131 < div className = "grid grid-cols-5 gap-4 mb-2 font-bold" >
@@ -148,7 +153,7 @@ function Logout() {
148153 ) ) }
149154 </ div >
150155 </ div >
151- ) }
156+ </ > }
152157 </ div >
153158 )
154159}
@@ -308,4 +313,4 @@ function DeleteUserModal({ user, getUsers }) {
308313 </ >
309314}
310315
311- export default Logout
316+ export default Admin
0 commit comments