Browse Source

save user

amenpunk 1 year ago
parent
commit
51bc642379
2 changed files with 100 additions and 34 deletions
  1. 11 2
      src/App.css
  2. 89 32
      src/Components/Password/Steps/TypePwd.jsx

+ 11 - 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
+}

+ 89 - 32
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
 
21
 }
20
 }
22
 
21
 
23
 function Title({ title, step }) {
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
 function PasswordInfoForm({ setUser, user }) {
26
 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
 function PermisosList(props) {
75
 function PermisosList(props) {
67
 
76
 
68
-  let { recursos,selectedRole, setSelectedRole } = props
77
+  let { recursos, selectedRole, setSelectedRole } = props
69
 
78
 
70
   return (
79
   return (
71
     <FormControl className="rolelist" >
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
       <RadioGroup
82
       <RadioGroup
74
         aria-labelledby="demo-radio-buttons-group-label"
83
         aria-labelledby="demo-radio-buttons-group-label"
75
         defaultValue="female"
84
         defaultValue="female"
112
 
121
 
113
   return (
122
   return (
114
     <FormControl>
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
       <FormGroup
125
       <FormGroup
117
         className="seluser_type"
126
         className="seluser_type"
118
         row
127
         row
152
   // console.log(props)
161
   // console.log(props)
153
 
162
 
154
   const auth = useSelector(state => state.token)
163
   const auth = useSelector(state => state.token)
164
+  const profile = useSelector(state => state.user.profile)
155
   const [recursos, setRecursos] = useState(null)
165
   const [recursos, setRecursos] = useState(null)
156
   const [selectedRole, setSelectedRole] = useState([])
166
   const [selectedRole, setSelectedRole] = useState([])
157
   const [userType, setUserType] = useState(ROLE.CANDIDATO)
167
   const [userType, setUserType] = useState(ROLE.CANDIDATO)
158
   const [user, setUser] = useState(null)
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
     //TODO: trigger form submit
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
   useEffect(() => {
219
   useEffect(() => {
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>