|
@@ -5,8 +5,7 @@ import {
|
5
|
5
|
FormControl, RadioGroup, FormControlLabel, FormLabel, Checkbox,
|
6
|
6
|
TextField, Box, FormGroup, Button, Stack, Collapse
|
7
|
7
|
} from '@mui/material'
|
8
|
|
-
|
9
|
|
-import { useForm } from "react-hook-form";
|
|
8
|
+import toast, { Toaster } from 'react-hot-toast';
|
10
|
9
|
|
11
|
10
|
import { Simple as Loading } from '../../Generics/loading.jsx'
|
12
|
11
|
|
|
@@ -21,7 +20,7 @@ function Marked({ step }) {
|
21
|
20
|
}
|
22
|
21
|
|
23
|
22
|
function Title({ title, step }) {
|
24
|
|
- return (<p className="titleMarked">{title}</p>)
|
|
23
|
+ return (<p className="titleMarked"><Marked /> {title}</p>)
|
25
|
24
|
}
|
26
|
25
|
|
27
|
26
|
function PasswordInfoForm({ setUser, user }) {
|
|
@@ -62,14 +61,24 @@ function PasswordInfoForm({ setUser, user }) {
|
62
|
61
|
}
|
63
|
62
|
|
64
|
63
|
|
|
64
|
+function validateMail(email) {
|
|
65
|
+ let re = /\S+@\S+\.\S+/;
|
|
66
|
+ return re.test(email);
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+function validatePassword(password) {
|
|
70
|
+ let re = /[A-Za-z0-9]{8,}/;
|
|
71
|
+ return re.test(password);
|
|
72
|
+}
|
|
73
|
+
|
65
|
74
|
|
66
|
75
|
function PermisosList(props) {
|
67
|
76
|
|
68
|
|
- let { recursos,selectedRole, setSelectedRole } = props
|
|
77
|
+ let { recursos, selectedRole, setSelectedRole } = props
|
69
|
78
|
|
70
|
79
|
return (
|
71
|
80
|
<FormControl className="rolelist" >
|
72
|
|
- <FormLabel id="demo-radio-buttons-group-label">{recursos.label}</FormLabel>
|
|
81
|
+ <FormLabel className="demo-radio-buttons-group-label">{recursos.label}</FormLabel>
|
73
|
82
|
<RadioGroup
|
74
|
83
|
aria-labelledby="demo-radio-buttons-group-label"
|
75
|
84
|
defaultValue="female"
|
|
@@ -112,7 +121,7 @@ function TipoUsuarios(props) {
|
112
|
121
|
|
113
|
122
|
return (
|
114
|
123
|
<FormControl>
|
115
|
|
- <FormLabel id="demo-radio-buttons-group-label">Nivel de Acceso</FormLabel>
|
|
124
|
+ <FormLabel className="demo-radio-buttons-group-label">Nivel de Acceso</FormLabel>
|
116
|
125
|
<FormGroup
|
117
|
126
|
className="seluser_type"
|
118
|
127
|
row
|
|
@@ -152,14 +161,59 @@ export function TypePwd(props) {
|
152
|
161
|
// console.log(props)
|
153
|
162
|
|
154
|
163
|
const auth = useSelector(state => state.token)
|
|
164
|
+ const profile = useSelector(state => state.user.profile)
|
155
|
165
|
const [recursos, setRecursos] = useState(null)
|
156
|
166
|
const [selectedRole, setSelectedRole] = useState([])
|
157
|
167
|
const [userType, setUserType] = useState(ROLE.CANDIDATO)
|
158
|
168
|
const [user, setUser] = useState(null)
|
159
|
169
|
|
160
|
|
- const saveUser = () => {
|
|
170
|
+ const postUser = async (body) => {
|
|
171
|
+ try {
|
|
172
|
+ let rest = new Service('/addSubUser')
|
|
173
|
+ let req = {
|
|
174
|
+ "username": body.email,
|
|
175
|
+ "nombre": body.email,
|
|
176
|
+ "apelidos": body.email,
|
|
177
|
+ "decription": "somthing",
|
|
178
|
+ "pwd": body.password,
|
|
179
|
+ "idEmpresa": profile.id,
|
|
180
|
+ }
|
|
181
|
+ let response = await rest.post(req,auth.token)
|
|
182
|
+ if(response.status === 200){
|
|
183
|
+ return response.data.id
|
|
184
|
+ }else{
|
|
185
|
+ return 0
|
|
186
|
+ }
|
|
187
|
+ console.log(response)
|
|
188
|
+ } catch (e) {
|
|
189
|
+ console.log(e)
|
|
190
|
+ return 0;
|
|
191
|
+ }
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ const saveUser = async () => {
|
161
|
195
|
//TODO: trigger form submit
|
162
|
|
- console.log(user, selectedRole)
|
|
196
|
+
|
|
197
|
+ if (user === null || !user || user === undefined) return toast.error('Falta informacion del usuario')
|
|
198
|
+
|
|
199
|
+ let { email, password } = user
|
|
200
|
+
|
|
201
|
+ let validMail = validateMail(email)
|
|
202
|
+ if (!validMail) return toast.error('Correo invalido')
|
|
203
|
+
|
|
204
|
+ let validPwd = validatePassword(password)
|
|
205
|
+ if (!validPwd) return toast.error('Contraseña invalida')
|
|
206
|
+
|
|
207
|
+ if (userType === ROLE.ASISTENTE && selectedRole.length === 0) return toast.error('Selecciona al menos un rol')
|
|
208
|
+
|
|
209
|
+ console.log('saveUser:', user)
|
|
210
|
+ let id = await postUser(user)
|
|
211
|
+ if(id === 0) return toast.error('Ocurrio un error al crear el usuario')
|
|
212
|
+ toast.success('Usuario creado con exito')
|
|
213
|
+ console.log("id:", id)
|
|
214
|
+
|
|
215
|
+ //TODO: save roles
|
|
216
|
+
|
163
|
217
|
}
|
164
|
218
|
|
165
|
219
|
useEffect(() => {
|
|
@@ -205,25 +259,47 @@ export function TypePwd(props) {
|
205
|
259
|
return (
|
206
|
260
|
<div class="gapwdrole">
|
207
|
261
|
|
208
|
|
- <div className="typepwdlist">
|
|
262
|
+ <div className="typepwdlist control_flow_role">
|
209
|
263
|
<TipoUsuarios type={userType} setType={setUserType} />
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+ <Box sx={{ mb: 2 }}>
|
|
267
|
+ <div style={{ paddingTop: 15 }}>
|
|
268
|
+ <Button
|
|
269
|
+ onClick={parseInt(userType) === ROLE.CANDIDATO ? props.handleNext : saveUser}
|
|
270
|
+ className="registerBtn"
|
|
271
|
+ variant="contained"
|
|
272
|
+ sx={{ mt: 1, mr: 1 }}
|
|
273
|
+ >
|
|
274
|
+ {parseInt(userType) === ROLE.CANDIDATO ? 'Siguiente' : 'Guardar'}
|
|
275
|
+ </Button>
|
|
276
|
+ <Button
|
|
277
|
+ disabled={true}
|
|
278
|
+ onClick={props.handleBack}
|
|
279
|
+ sx={{ mt: 1, mr: 1 }}
|
|
280
|
+ >
|
|
281
|
+ Regresar
|
|
282
|
+ </Button>
|
|
283
|
+ </div>
|
|
284
|
+ </Box>
|
|
285
|
+
|
210
|
286
|
</div>
|
211
|
287
|
|
212
|
288
|
<Collapse orientation="vertical" in={parseInt(userType) === ROLE.ADMIN} >
|
213
|
289
|
<div className="typepwdlist">
|
214
|
|
- <Title title="Informacion de la contraseña" step={"B"} />
|
|
290
|
+ <FormLabel className="demo-radio-buttons-group-label">Información del usuario</FormLabel>
|
215
|
291
|
<PasswordInfoForm setUser={setUser} user={user} />
|
216
|
292
|
</div>
|
217
|
293
|
</Collapse>
|
218
|
294
|
|
219
|
295
|
<Collapse orientation="vertical" in={parseInt(userType) === ROLE.ASISTENTE} >
|
220
|
296
|
<div className="typepwdlist">
|
221
|
|
- <Title title="Informacion de la contraseña" step={"B"} />
|
|
297
|
+ <FormLabel className="demo-radio-buttons-group-label">Información del usuario</FormLabel>
|
222
|
298
|
<PasswordInfoForm setUser={setUser} user={user} />
|
223
|
299
|
</div>
|
224
|
300
|
|
225
|
301
|
<div className="typepwdlist">
|
226
|
|
- <Title title="Selecciona los privilegios" step={"C"} />
|
|
302
|
+ <FormLabel className="demo-radio-buttons-group-label">Selecciona los privilegios</FormLabel>
|
227
|
303
|
{
|
228
|
304
|
recursos &&
|
229
|
305
|
Object.keys(recursos).map((k) =>
|
|
@@ -236,26 +312,7 @@ export function TypePwd(props) {
|
236
|
312
|
</div>
|
237
|
313
|
</Collapse>
|
238
|
314
|
|
239
|
|
- <Box sx={{ mb: 2 }}>
|
240
|
|
- <div style={{ paddingTop: 15 }}>
|
241
|
|
- <Button
|
242
|
|
- onClick={parseInt(userType) === ROLE.CANDIDATO ? props.handleNext : saveUser}
|
243
|
|
- className="registerBtn"
|
244
|
|
- variant="contained"
|
245
|
|
- sx={{ mt: 1, mr: 1 }}
|
246
|
|
- >
|
247
|
|
- {parseInt(userType) === ROLE.CANDIDATO ? 'Siguiente' : 'Guardar'}
|
248
|
|
- </Button>
|
249
|
|
- <Button
|
250
|
|
- disabled={true}
|
251
|
|
- onClick={props.handleBack}
|
252
|
|
- sx={{ mt: 1, mr: 1 }}
|
253
|
|
- >
|
254
|
|
- Regresar
|
255
|
|
- </Button>
|
256
|
|
- </div>
|
257
|
|
- </Box>
|
258
|
|
-
|
|
315
|
+ <Toaster position="top-right" />
|
259
|
316
|
|
260
|
317
|
|
261
|
318
|
</div>
|