Reac front end for psicometric app

Operation.jsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import * as React from 'react';
  2. import { Edit as EditIcon, Send as SendIcon } from '@mui/icons-material'
  3. import {
  4. Button, Dialog, DialogActions, DialogContent,
  5. DialogContentText, DialogTitle
  6. } from '@mui/material'
  7. import * as Yup from 'yup';
  8. import { useQuery } from 'react-query'
  9. import { Service } from '../../Utils/HTTP.js'
  10. import useAuth from '../../Auth/useAuth.js';
  11. import { useFormik, Form, FormikProvider } from 'formik';
  12. import { Stack, TextField } from '@mui/material';
  13. import DateFnsUtils from '@date-io/date-fns';
  14. import { DesktopDatePicker, LocalizationProvider } from '@mui/lab';
  15. export function Operation(props) {
  16. let [open, setOpen] = React.useState(false);
  17. const handleOpen = (status) => setOpen(status);
  18. return (
  19. <div>
  20. <div className="operation_buttons">
  21. <EditIcon onClick={() => setOpen(true)} className="icon_op" />
  22. <SendIcon className="icon_op" />
  23. </div>
  24. {
  25. open ?
  26. <ModalEdit
  27. password={props}
  28. open={open}
  29. handleOpen={handleOpen}
  30. />
  31. : null
  32. }
  33. </div>
  34. )
  35. }
  36. function ModalEdit(props) {
  37. let { password, open, handleOpen } = props
  38. let { pwd, plz } = password
  39. const auth = useAuth();
  40. const token = React.useRef(auth.getToken());
  41. const getPassword = async () => {
  42. let rest = new Service(`/contrasenia/${pwd}/${plz}`)
  43. return await rest.getQuery(token.current)
  44. }
  45. let { data: result } = useQuery('contra', getPassword);
  46. let initialValues = {
  47. id: result?.data?.id,
  48. pwd: result?.data?.pwd,
  49. deadpwd: result?.data?.deadpwd,
  50. state: result?.data?.state,
  51. dateToActived: result?.data?.dateToActived,
  52. plaza_id: result?.data?.plaza_id,
  53. }
  54. return (
  55. <Dialog
  56. open={open}
  57. onClose={() => handleOpen(false)}
  58. aria-labelledby="alert-dialog-title"
  59. aria-describedby="alert-dialog-description"
  60. >
  61. <DialogTitle id="alert-dialog-title">
  62. {pwd}
  63. </DialogTitle>
  64. <DialogContent>
  65. <DialogContentText id="alert-dialog-description">
  66. <ModalForm initialValues={initialValues} />
  67. </DialogContentText>
  68. </DialogContent>
  69. <DialogActions>
  70. <Button onClick={() => handleOpen(false)} autoFocus>
  71. Salir
  72. </Button>
  73. <Button onClick={() => handleOpen(false)}>Guardar</Button>
  74. </DialogActions>
  75. </Dialog>
  76. )
  77. }
  78. function ModalForm(props) {
  79. console.log("PROPS >> ", props)
  80. const pwdSchema = Yup.object().shape({
  81. id: Yup.number(),
  82. pwd: Yup.string().required(),
  83. deadpwd: Yup.date(),
  84. state: Yup.number().required(),
  85. dateToActived: Yup.date().required(),
  86. plaza_id: Yup.number().required()
  87. })
  88. const formik = useFormik({
  89. initialValues: props.initialValues,
  90. onSubmit: (fields) => {
  91. console.log('campos> ', fields)
  92. },
  93. validationSchema: pwdSchema,
  94. })
  95. const { errors, touched, handleSubmit, getFieldProps, values, setValues } = formik;
  96. console.log('formik values >> ', values )
  97. return (
  98. <FormikProvider value={formik}>
  99. <Form style={{padding : 15,minWidth:450}} autoComplete="off" noValidate onSubmit={handleSubmit}>
  100. <Stack spacing={4}>
  101. <TextField
  102. fullWidth
  103. type="text"
  104. label="Nombre o identificador"
  105. {...getFieldProps('pwd')}
  106. error={Boolean(touched.pwd && errors.pwd)}
  107. helperText={touched.pwd && errors.pwd}
  108. />
  109. <TextField
  110. fullWidth
  111. type="text"
  112. label="Nombre o identificador"
  113. {...getFieldProps('pwd')}
  114. error={Boolean(touched.pwd && errors.pwd)}
  115. helperText={touched.pwd && errors.pwd}
  116. />
  117. <TextField
  118. fullWidth
  119. type="text"
  120. label="Nombre o identificador"
  121. {...getFieldProps('pwd')}
  122. error={Boolean(touched.pwd && errors.pwd)}
  123. helperText={touched.pwd && errors.pwd}
  124. />
  125. <TextField
  126. fullWidth
  127. type="text"
  128. label="Nombre o identificador"
  129. {...getFieldProps('pwd')}
  130. error={Boolean(touched.pwd && errors.pwd)}
  131. helperText={touched.pwd && errors.pwd}
  132. />
  133. <LocalizationProvider
  134. dateAdapter={DateFnsUtils}>
  135. <DesktopDatePicker
  136. label="Fecha de Activación"
  137. fullWidth
  138. inputFormat="dd/MM/yyyy"
  139. {...getFieldProps('dateToActived')}
  140. value={values.dateToActived}
  141. onChange={(val) => setValues({ ...values, dateToActived: new Date(val) })
  142. }
  143. renderInput={(params) =>
  144. <TextField
  145. error={Boolean(touched.dateToActived && errors.dateToActived)}
  146. helperText={touched.dateToActived && errors.dateToActived}
  147. disabled={true}
  148. label="Fecha de Activación"
  149. fullWidth
  150. {...params}
  151. />}
  152. />
  153. </LocalizationProvider>
  154. <LocalizationProvider
  155. dateAdapter={DateFnsUtils}>
  156. <DesktopDatePicker
  157. label="Fecha de Vencimiento"
  158. fullWidth
  159. inputFormat="dd/MM/yyyy"
  160. {...getFieldProps('deadpwd')}
  161. value={values.deadpwd}
  162. onChange={(val) => setValues({ ...values, deadpwd: new Date(val) })
  163. }
  164. renderInput={(params) =>
  165. <TextField
  166. error={Boolean(touched.deadpwd && errors.deadpwd)}
  167. helperText={touched.deadpwd && errors.deadpwd}
  168. disabled={true}
  169. label="Fecha de Vencimiento"
  170. fullWidth
  171. {...params}
  172. />}
  173. />
  174. </LocalizationProvider>
  175. </Stack>
  176. </Form>
  177. </FormikProvider >
  178. )
  179. }