2 Commits 4b17b3cad3 ... ae91fb5c63

Autor SHA1 Mensagem Data
  amenpunk ae91fb5c63 add role array 1 ano atrás
  amenpunk 51bc642379 save user 1 ano atrás
3 arquivos alterados com 109 adições e 42 exclusões
  1. 12 2
      src/App.css
  2. 1 1
      src/App.js
  3. 96 39
      src/Components/Password/Steps/TypePwd.jsx

+ 12 - 2
src/App.css

@@ -445,12 +445,14 @@
445 445
   align-items: flex-start;
446 446
   align-content: flex-start;
447 447
 }
448
-#demo-radio-buttons-group-label{
448
+.demo-radio-buttons-group-label{
449 449
   font-weight: bold;
450
+  padding-top:10px !important;
451
+  padding-bottom:10px !important;
450 452
 }
451 453
 .typepwdlist{
452 454
   border-bottom: 1px solid gainsboro;
453
-  padding: 15px;
455
+  /* padding: 15px; */
454 456
 }
455 457
 .titleMarked{
456 458
   /* color : gray; */
@@ -460,3 +462,11 @@
460 462
   flex-direction: column;
461 463
   flex-wrap: wrap;
462 464
 }
465
+
466
+.control_flow_role{
467
+  display: flex;
468
+  flex-direction: row;
469
+  flex-wrap: wrap;
470
+  justify-content: space-between;
471
+  align-items: flex-end;
472
+}

+ 1 - 1
src/App.js

@@ -12,7 +12,7 @@ import Routes from './Components/Routes'
12 12
 const queryClient = new QueryClient({
13 13
   defaultOptions: {
14 14
     queries: {
15
-      refetchOnWindowFocus: false,
15
+      refetchOnWindowFocus: true,
16 16
     },
17 17
   },
18 18
 })

+ 96 - 39
src/Components/Password/Steps/TypePwd.jsx

@@ -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
 
@@ -16,14 +15,6 @@ const ROLE = {
16 15
   CANDIDATO: 3,
17 16
 }
18 17
 
19
-function Marked({ step }) {
20
-  return (<strong className="important_marked">{step}</strong>)
21
-}
22
-
23
-function Title({ title, step }) {
24
-  return (<p className="titleMarked">{title}</p>)
25
-}
26
-
27 18
 function PasswordInfoForm({ setUser, user }) {
28 19
 
29 20
   return (
@@ -62,14 +53,24 @@ function PasswordInfoForm({ setUser, user }) {
62 53
 }
63 54
 
64 55
 
56
+function validateMail(email) {
57
+  let re = /\S+@\S+\.\S+/;
58
+  return re.test(email);
59
+}
60
+
61
+function validatePassword(password) {
62
+  let re = /[A-Za-z0-9]{8,}/;
63
+  return re.test(password);
64
+}
65
+
65 66
 
66 67
 function PermisosList(props) {
67 68
 
68
-  let { recursos,selectedRole, setSelectedRole } = props
69
+  let { recursos, selectedRole, setSelectedRole } = props
69 70
 
70 71
   return (
71 72
     <FormControl className="rolelist" >
72
-      <FormLabel id="demo-radio-buttons-group-label">{recursos.label}</FormLabel>
73
+      <FormLabel className="demo-radio-buttons-group-label">{recursos.label}</FormLabel>
73 74
       <RadioGroup
74 75
         aria-labelledby="demo-radio-buttons-group-label"
75 76
         defaultValue="female"
@@ -112,7 +113,7 @@ function TipoUsuarios(props) {
112 113
 
113 114
   return (
114 115
     <FormControl>
115
-      <FormLabel id="demo-radio-buttons-group-label">Nivel de Acceso</FormLabel>
116
+      <FormLabel className="demo-radio-buttons-group-label">Nivel de Acceso</FormLabel>
116 117
       <FormGroup
117 118
         className="seluser_type"
118 119
         row
@@ -152,16 +153,69 @@ export function TypePwd(props) {
152 153
   // console.log(props)
153 154
 
154 155
   const auth = useSelector(state => state.token)
156
+  const profile = useSelector(state => state.user.profile)
155 157
   const [recursos, setRecursos] = useState(null)
156 158
   const [selectedRole, setSelectedRole] = useState([])
157 159
   const [userType, setUserType] = useState(ROLE.CANDIDATO)
158 160
   const [user, setUser] = useState(null)
159 161
 
160
-  const saveUser = () => {
162
+  const postUser = async (body) => {
163
+    try {
164
+      let rest = new Service('/addSubUser')
165
+      let req = {
166
+        "username": body.email,
167
+        "nombre": body.email,
168
+        "apelidos": body.email,
169
+        "decription": "somthing",
170
+        "pwd": body.password,
171
+        "idEmpresa": profile.id,
172
+      }
173
+      let response = await rest.post(req, auth.token)
174
+      return response.id || 0;
175
+    } catch (e) {
176
+      console.log('CATCH:', e)
177
+      return 0;
178
+    }
179
+  }
180
+
181
+  const saveUser = async () => {
161 182
     //TODO: trigger form submit
162
-    console.log(user, selectedRole)
183
+    if (user === null || !user || user === undefined) return toast.error('Falta informacion del usuario')
184
+
185
+    let { email, password } = user
186
+
187
+    let validMail = validateMail(email)
188
+    if (!validMail) return toast.error('Correo invalido')
189
+
190
+    let validPwd = validatePassword(password)
191
+    if (!validPwd) return toast.error('Contraseña invalida')
192
+
193
+    if (userType === ROLE.ASISTENTE && selectedRole.length === 0) return toast.error('Selecciona al menos un rol')
194
+
195
+    let id = await postUser(user)
196
+    if (id === 0) return toast.error('Ocurrio un error al crear el usuario')
197
+    toast.success('Usuario creado con exito')
198
+    console.log("id:", id)
199
+    if (userType === ROLE.ASISTENTE) postResources(id)
200
+    clearForm()
201
+
202
+  }
203
+
204
+  const postResources = (user_id) => {
205
+    let rest = new Service('/rolAdd/' + user_id)
206
+    let selected = selectedRole.map(id => ({ id: id, path: "" }))
207
+    console.log('selected:', selected)
208
+    let saved = rest.post(selected, auth.token)
209
+    console.log('saved:', saved)
210
+  }
211
+
212
+  const clearForm = () => {
213
+    setUser(null)
214
+    setSelectedRole([])
215
+    setUserType(ROLE.CANDIDATO)
163 216
   }
164 217
 
218
+
165 219
   useEffect(() => {
166 220
 
167 221
     const getRecursos = () => {
@@ -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>