2 Commits 4b17b3cad3 ... ae91fb5c63

Author SHA1 Message Date
  amenpunk ae91fb5c63 add role array 1 year ago
  amenpunk 51bc642379 save user 1 year ago
3 changed files with 109 additions and 42 deletions
  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
   align-items: flex-start;
445
   align-items: flex-start;
446
   align-content: flex-start;
446
   align-content: flex-start;
447
 }
447
 }
448
-#demo-radio-buttons-group-label{
448
+.demo-radio-buttons-group-label{
449
   font-weight: bold;
449
   font-weight: bold;
450
+  padding-top:10px !important;
451
+  padding-bottom:10px !important;
450
 }
452
 }
451
 .typepwdlist{
453
 .typepwdlist{
452
   border-bottom: 1px solid gainsboro;
454
   border-bottom: 1px solid gainsboro;
453
-  padding: 15px;
455
+  /* padding: 15px; */
454
 }
456
 }
455
 .titleMarked{
457
 .titleMarked{
456
   /* color : gray; */
458
   /* color : gray; */
460
   flex-direction: column;
462
   flex-direction: column;
461
   flex-wrap: wrap;
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
 const queryClient = new QueryClient({
12
 const queryClient = new QueryClient({
13
   defaultOptions: {
13
   defaultOptions: {
14
     queries: {
14
     queries: {
15
-      refetchOnWindowFocus: false,
15
+      refetchOnWindowFocus: true,
16
     },
16
     },
17
   },
17
   },
18
 })
18
 })

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

5
   FormControl, RadioGroup, FormControlLabel, FormLabel, Checkbox,
5
   FormControl, RadioGroup, FormControlLabel, FormLabel, Checkbox,
6
   TextField, Box, FormGroup, Button, Stack, Collapse
6
   TextField, Box, FormGroup, Button, Stack, Collapse
7
 } from '@mui/material'
7
 } from '@mui/material'
8
-
9
-import { useForm } from "react-hook-form";
8
+import toast, { Toaster } from 'react-hot-toast';
10
 
9
 
11
 import { Simple as Loading } from '../../Generics/loading.jsx'
10
 import { Simple as Loading } from '../../Generics/loading.jsx'
12
 
11
 
16
   CANDIDATO: 3,
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
 function PasswordInfoForm({ setUser, user }) {
18
 function PasswordInfoForm({ setUser, user }) {
28
 
19
 
29
   return (
20
   return (
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
 function PermisosList(props) {
67
 function PermisosList(props) {
67
 
68
 
68
-  let { recursos,selectedRole, setSelectedRole } = props
69
+  let { recursos, selectedRole, setSelectedRole } = props
69
 
70
 
70
   return (
71
   return (
71
     <FormControl className="rolelist" >
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
       <RadioGroup
74
       <RadioGroup
74
         aria-labelledby="demo-radio-buttons-group-label"
75
         aria-labelledby="demo-radio-buttons-group-label"
75
         defaultValue="female"
76
         defaultValue="female"
112
 
113
 
113
   return (
114
   return (
114
     <FormControl>
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
       <FormGroup
117
       <FormGroup
117
         className="seluser_type"
118
         className="seluser_type"
118
         row
119
         row
152
   // console.log(props)
153
   // console.log(props)
153
 
154
 
154
   const auth = useSelector(state => state.token)
155
   const auth = useSelector(state => state.token)
156
+  const profile = useSelector(state => state.user.profile)
155
   const [recursos, setRecursos] = useState(null)
157
   const [recursos, setRecursos] = useState(null)
156
   const [selectedRole, setSelectedRole] = useState([])
158
   const [selectedRole, setSelectedRole] = useState([])
157
   const [userType, setUserType] = useState(ROLE.CANDIDATO)
159
   const [userType, setUserType] = useState(ROLE.CANDIDATO)
158
   const [user, setUser] = useState(null)
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
     //TODO: trigger form submit
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
   useEffect(() => {
219
   useEffect(() => {
166
 
220
 
167
     const getRecursos = () => {
221
     const getRecursos = () => {
205
   return (
259
   return (
206
     <div class="gapwdrole">
260
     <div class="gapwdrole">
207
 
261
 
208
-      <div className="typepwdlist">
262
+      <div className="typepwdlist control_flow_role">
209
         <TipoUsuarios type={userType} setType={setUserType} />
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
       </div>
286
       </div>
211
 
287
 
212
       <Collapse orientation="vertical" in={parseInt(userType) === ROLE.ADMIN} >
288
       <Collapse orientation="vertical" in={parseInt(userType) === ROLE.ADMIN} >
213
         <div className="typepwdlist">
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
           <PasswordInfoForm setUser={setUser} user={user} />
291
           <PasswordInfoForm setUser={setUser} user={user} />
216
         </div>
292
         </div>
217
       </Collapse>
293
       </Collapse>
218
 
294
 
219
       <Collapse orientation="vertical" in={parseInt(userType) === ROLE.ASISTENTE} >
295
       <Collapse orientation="vertical" in={parseInt(userType) === ROLE.ASISTENTE} >
220
         <div className="typepwdlist">
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
           <PasswordInfoForm setUser={setUser} user={user} />
298
           <PasswordInfoForm setUser={setUser} user={user} />
223
         </div>
299
         </div>
224
 
300
 
225
         <div className="typepwdlist">
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
             recursos &&
304
             recursos &&
229
             Object.keys(recursos).map((k) =>
305
             Object.keys(recursos).map((k) =>
236
         </div>
312
         </div>
237
       </Collapse>
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
     </div>
318
     </div>