浏览代码

add and remove user candilist

amenpunk 2 年之前
父节点
当前提交
c29b698948
共有 1 个文件被更改,包括 60 次插入71 次删除
  1. 60 71
      src/Components/Password/Operation.jsx

+ 60 - 71
src/Components/Password/Operation.jsx

1
 import * as React from 'react';
1
 import * as React from 'react';
2
 import {
2
 import {
3
   Button, Dialog, DialogActions, DialogContent,
3
   Button, Dialog, DialogActions, DialogContent,
4
-  FormControlLabel, Checkbox, Box, Stack,
5
-  TextField,CircularProgress
4
+  FormControlLabel, Checkbox, Stack,
5
+  TextField, CircularProgress, Divider, Typography
6
 } from '@mui/material'
6
 } from '@mui/material'
7
 
7
 
8
 import { AddCircle } from '@mui/icons-material/';
8
 import { AddCircle } from '@mui/icons-material/';
23
 import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
23
 import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
24
 
24
 
25
 
25
 
26
-function Candidatos(props){
26
+function Candidatos(props) {
27
 
27
 
28
   const CandidatoSchema = Yup.object().shape({
28
   const CandidatoSchema = Yup.object().shape({
29
     nombres:
29
     nombres:
38
         .email("Correo no valido")
38
         .email("Correo no valido")
39
   });
39
   });
40
 
40
 
41
-  let [password, setPassword] = React.useState([]);
42
 
41
 
43
-  let { candidatos } = props
44
-  console.log('operation props candidatos', candidatos)
42
+  let { candidatos, add , remove } = props
45
 
43
 
46
   const formik = useFormik({
44
   const formik = useFormik({
47
     initialValues: {
45
     initialValues: {
50
       mail: "",
48
       mail: "",
51
     },
49
     },
52
     onSubmit: () => {
50
     onSubmit: () => {
53
-      if(password.length <= 0){
54
-        toast.error("Seleciona almenos un destino")
55
-        return;
56
-      }
57
       console.log('submited')
51
       console.log('submited')
58
     },
52
     },
59
     validationSchema: CandidatoSchema,
53
     validationSchema: CandidatoSchema,
60
   });
54
   });
61
 
55
 
62
-  const { errors, touched, handleSubmit, getFieldProps, values, resetForm,isValid } = formik;
56
+  var { errors, touched, handleSubmit, getFieldProps, values, resetForm, isValid } = formik;
63
 
57
 
64
   const addToList = () => {
58
   const addToList = () => {
65
 
59
 
66
-    if(!values.nombres || !values.apellidos || !values.mail){
60
+    if (!values.nombres || !values.apellidos || !values.mail) {
67
       return toast.error("Completa la informacion del candidato")
61
       return toast.error("Completa la informacion del candidato")
68
     }
62
     }
69
 
63
 
70
-    if(!isValid) {
64
+    if (!isValid) {
71
       return toast.error("Completa la informacion del candidato")
65
       return toast.error("Completa la informacion del candidato")
72
     }
66
     }
73
 
67
 
76
       'apellidos': values.apellidos,
70
       'apellidos': values.apellidos,
77
       'mail': values.mail,
71
       'mail': values.mail,
78
     }
72
     }
79
-    let new_users = [...password.candidatos, user ]
80
-
81
-    setPassword({...candidatos, candidatos: new_users })
73
+    add(user)
82
     resetForm();
74
     resetForm();
83
 
75
 
84
   }
76
   }
85
 
77
 
86
-  const removeFromList = (umail) => {
87
-    let without = password.candidatos.filter( user => user.mail !== umail )
88
-    setPassword({...password, candidatos: without })
89
-  }
90
-
91
   return (
78
   return (
92
     <FormikProvider style={{ padding: 25 }} value={formik}>
79
     <FormikProvider style={{ padding: 25 }} value={formik}>
80
+      <Typography style={{ padding: 5, marginBottom: 15 }}>Ingresa la informacion del candidato</Typography>
81
+      <Divider />
93
       <Form autoComplete="off" noValidate onSubmit={handleSubmit}>
82
       <Form autoComplete="off" noValidate onSubmit={handleSubmit}>
94
         <Stack spacing={3}>
83
         <Stack spacing={3}>
95
-          <Stack style={{paddingTop: 15}} direction={{ xs: 'column', sm: 'row' }} spacing={2}>
84
+          <Stack style={{ paddingTop: 15 }} direction={{ xs: 'column', sm: 'row' }} spacing={2}>
96
             <TextField
85
             <TextField
97
               label="Nombre"
86
               label="Nombre"
98
               fullWidth
87
               fullWidth
121
             />
110
             />
122
 
111
 
123
             <Button onClick={addToList}>
112
             <Button onClick={addToList}>
124
-              <AddCircle style={{color:'var(--main)'}}/>
113
+              <AddCircle style={{ color: 'var(--main)' }} />
125
             </Button>
114
             </Button>
126
 
115
 
127
           </Stack>
116
           </Stack>
128
 
117
 
129
-          <MailTable 
130
-            remove={removeFromList} 
118
+          <MailTable
119
+            remove={remove}
131
             users={candidatos}
120
             users={candidatos}
132
-            />
133
-
134
-          <Box sx={{ mb: 2 }}>
135
-            <div style={{ paddingTop: 15 }}>
136
-              <Button
137
-                type="submit"
138
-                className="registerBtn"
139
-                variant="contained"
140
-                sx={{ mt: 1, mr: 1 }}
141
-              >
142
-                {'Siguiente'}
143
-              </Button>
144
-              <Button
145
-                disabled={false}
146
-                onClick={() => console.log('regresar')}
147
-                sx={{ mt: 1, mr: 1 }}
148
-              >
149
-                Regresar
150
-              </Button>
151
-            </div>
152
-          </Box>
121
+          />
153
 
122
 
154
         </Stack>
123
         </Stack>
155
         <Toaster position="top-right" />
124
         <Toaster position="top-right" />
160
 
129
 
161
 export function ModalEdit(props) {
130
 export function ModalEdit(props) {
162
 
131
 
132
+  //TODO:
133
+  //se debe crear un objeto de estado que almacena los nuevos cambios de la password
134
+  //enviar por props las utilizades de edicion y eliminar
135
+
163
   const auth = useSelector((state) => state.token)
136
   const auth = useSelector((state) => state.token)
164
   let [data, setData] = React.useState(null)
137
   let [data, setData] = React.useState(null)
165
   let { password, open, handleOpen } = props
138
   let { password, open, handleOpen } = props
167
 
140
 
168
   React.useEffect(() => {
141
   React.useEffect(() => {
169
 
142
 
170
-    const getPassword = async () => {
171
-      let rest = new Service(`/contrasenia/${btoa(pwd)}/${plz}`)
172
-      return await rest.getQuery(auth.token)
173
-    }
174
-
175
-    getPassword()
143
+    let rest = new Service(`/contrasenia/${btoa(pwd)}/${plz}`)
144
+    rest.getQuery(auth.token)
176
       .then(resp => setData(resp.data))
145
       .then(resp => setData(resp.data))
177
       .catch(error => console.log(error))
146
       .catch(error => console.log(error))
147
+
178
   }, [auth.token, pwd, plz])
148
   }, [auth.token, pwd, plz])
179
 
149
 
180
   return (
150
   return (
208
 
178
 
209
 function ModalForm(props) {
179
 function ModalForm(props) {
210
 
180
 
181
+  let [candidatos,setCandidatos] = React.useState(null);
211
   const pwdSchema = Yup.object().shape({
182
   const pwdSchema = Yup.object().shape({
212
     id: Yup.number(),
183
     id: Yup.number(),
213
     pwd: Yup.string().required("Escoge un nombre valido"),
184
     pwd: Yup.string().required("Escoge un nombre valido"),
218
 
189
 
219
   const queryClient = useQueryClient();
190
   const queryClient = useQueryClient();
220
   let { password } = props
191
   let { password } = props
221
-  console.log("227 PWD: ", password)
222
 
192
 
223
-  let candidatos = password.candidatospwds.map( pwd => {
224
-    let { apellidos, nombre,mail} = pwd.candi
225
-    return { nombres: nombre, apellidos, mail }
226
-  })
193
+  React.useEffect(() => {
194
+    let mapCandi = password.candidatospwds.map(pwd => {
195
+      let { apellidos, nombre, mail } = pwd.candi
196
+      return { nombres: nombre, apellidos, mail }
197
+    })
198
+    setCandidatos(mapCandi)
199
+  },[password.candidatospwds])
200
+
201
+  function removeCandidato (umail) {
202
+    console.log('remove:', umail)
203
+    let without = candidatos.filter( user => user.mail !== umail )
204
+    setCandidatos(without)
205
+  }
206
+
207
+  function addCandidato (candidato) {
208
+    setCandidatos([...candidatos, candidato ])
209
+  }
227
 
210
 
228
   const formik = useFormik({
211
   const formik = useFormik({
229
     initialValues: {
212
     initialValues: {
246
       rest.put(fields, props.token)
229
       rest.put(fields, props.token)
247
         .then(result => {
230
         .then(result => {
248
           queryClient.invalidateQueries('passwords')
231
           queryClient.invalidateQueries('passwords')
249
-          console.log(result)
250
           setTimeout(() => {
232
           setTimeout(() => {
251
             props.handleOpen(false)
233
             props.handleOpen(false)
252
           }, 1000)
234
           }, 1000)
264
 
246
 
265
   return (
247
   return (
266
     <Row>
248
     <Row>
249
+
267
       <Col>
250
       <Col>
268
         <FormikProvider value={formik}>
251
         <FormikProvider value={formik}>
269
           <Form style={{ padding: 20, maxWidth: 950 }} autoComplete="off" noValidate onSubmit={handleSubmit}>
252
           <Form style={{ padding: 20, maxWidth: 950 }} autoComplete="off" noValidate onSubmit={handleSubmit}>
347
                     />}
330
                     />}
348
                 />
331
                 />
349
               </LocalizationProvider>
332
               </LocalizationProvider>
350
-              <DialogActions>
351
-                <Button onClick={() => props.handleOpen(false)}>
352
-                  Cerrar
353
-                </Button>
354
-                <Button
355
-                  type="submit"
356
-                  className="registerBtn"
357
-                  style={{ color: 'white' }}
358
-                >
359
-                  Guardar
360
-                </Button>
361
-              </DialogActions>
333
+
362
 
334
 
363
             </Stack>
335
             </Stack>
364
           </Form>
336
           </Form>
366
         </FormikProvider >
338
         </FormikProvider >
367
       </Col>
339
       </Col>
368
       <Col>
340
       <Col>
369
-        <Candidatos candidatos={candidatos} />
341
+        <Candidatos
342
+          add={addCandidato} 
343
+          remove={removeCandidato} 
344
+          candidatos={candidatos} 
345
+        />
370
       </Col>
346
       </Col>
347
+
348
+      <DialogActions>
349
+        <Button onClick={() => props.handleOpen(false)}>
350
+          Cerrar
351
+        </Button>
352
+        <Button
353
+          type="submit"
354
+          className="registerBtn"
355
+          style={{ color: 'white' }}
356
+        >
357
+          Guardar
358
+        </Button>
359
+      </DialogActions>
371
     </Row>
360
     </Row>
372
   )
361
   )
373
 }
362
 }