Reac front end for psicometric app

PersonalInfo.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { useState } from 'react'
  2. import { useFormik, Form, FormikProvider } from 'formik';
  3. import { useNavigate } from 'react-router-dom';
  4. import {
  5. Stack, TextField,Box, Button,
  6. Backdrop, CircularProgress,
  7. } from '@mui/material';
  8. import toast, { Toaster } from 'react-hot-toast';
  9. import * as Yup from 'yup';
  10. import { Service } from '../../Utils/HTTP';
  11. // import useAuth from '../../Auth/useAuth';;
  12. export function PersonalInfo(props) {
  13. let { handleBack, setClient, client } = props
  14. let navigate = useNavigate()
  15. const RegisterSchema = Yup.object().shape({
  16. nit: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado largo!').required('El nit es requerido'),
  17. cui: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado Largo!').required('Tu CUI/DPI es requerido'),
  18. direccion: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado Largo!').required('La direccion es requerida'),
  19. nacimiento: Yup.date().required('Tu fecha nacimiento es requerida'),
  20. telefono: Yup.number('Ingresa únicamente números').required('Tu numero de telefono es requerido')
  21. });
  22. let [open, setOpen] = useState(false);
  23. const formik = useFormik({
  24. initialValues: {
  25. nit: client.nit,
  26. cui: client.cui,
  27. direccion: client.direccion,
  28. nacimiento: client.nacimiento,
  29. telefono: client.telefono
  30. },
  31. validationSchema: RegisterSchema,
  32. onSubmit: (values) => {
  33. setOpen(true);
  34. let new_client = {
  35. ...client, ...values
  36. }
  37. setClient(new_client)
  38. let body = {
  39. "nombre" : new_client.firstName,
  40. "apelidos" : new_client.lastName,
  41. "telefono" : new_client.telefono,
  42. "direccio": new_client.direccion,
  43. "fechacumple": new_client.nacimiento,
  44. "nit": new_client.nit,
  45. "cui": new_client.cui,
  46. "email": new_client.email,
  47. "username": new_client.email,
  48. "pwd": new_client.password
  49. }
  50. let req = new Service('/registro');
  51. req.post(body)
  52. .then( data => {
  53. setTimeout(() => {
  54. setOpen(false);
  55. return navigate('/')
  56. }, 2000)
  57. console.log("DATA ::", data)
  58. let nombre = client.firstName + " " + client.lastName
  59. toast.success(`Bienvenido ${nombre} !!`)
  60. })
  61. .catch(error => {
  62. console.log("ERROR ::", error)
  63. toast.error(`Ups ocurrió un error puede que tu usuario yo exista o sea un error interno, intenta nuevamente más tarde o ponte en contacto con nosotros`)
  64. return setOpen(false);
  65. })
  66. }
  67. });
  68. const { errors, touched, handleSubmit, getFieldProps } = formik;
  69. return(
  70. <FormikProvider style={{ padding : 15}} value={formik}>
  71. <Form autoComplete="off" noValidate onSubmit={handleSubmit}>
  72. <Stack spacing={3}>
  73. <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
  74. <TextField
  75. label="Numero de Nit"
  76. fullWidth
  77. {...getFieldProps('nit')}
  78. error={Boolean(touched.nit && errors.nit)}
  79. helperText={touched.nit && errors.nit}
  80. />
  81. <TextField
  82. label="CUI/DPI"
  83. fullWidth
  84. {...getFieldProps('cui')}
  85. error={Boolean(touched.cui && errors.cui)}
  86. helperText={touched.cui && errors.cui}
  87. />
  88. </Stack>
  89. <TextField
  90. fullWidth
  91. type="text"
  92. label="Dirección"
  93. {...getFieldProps('direccion')}
  94. error={Boolean(touched.direccion && errors.direccion)}
  95. helperText={touched.direccion && errors.direccion}
  96. />
  97. <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
  98. <TextField
  99. type="date"
  100. label="Fecha de nacimiento"
  101. fullWidth
  102. InputLabelProps={{ required: true }}
  103. {...getFieldProps('nacimiento')}
  104. error={Boolean(touched.nacimiento && errors.nacimiento)}
  105. helperText={touched.nacimiento && errors.nacimiento}
  106. />
  107. <TextField
  108. label="Telefono"
  109. fullWidth
  110. {...getFieldProps('telefono')}
  111. error={Boolean(touched.telefono && errors.telefono)}
  112. helperText={touched.telefono && errors.telefono}
  113. />
  114. </Stack>
  115. <Box sx={{ mb: 2 }}>
  116. <div style={{ paddingTop: 15 }}>
  117. <Button
  118. type="submit"
  119. className="registerBtn"
  120. variant="contained"
  121. // onClick={handleNext}
  122. sx={{ mt: 1, mr: 1 }}
  123. >
  124. {'Registrarme'}
  125. </Button>
  126. <Button
  127. onClick={handleBack}
  128. sx={{ mt: 1, mr: 1 }}
  129. >
  130. Regresar
  131. </Button>
  132. </div>
  133. </Box>
  134. </Stack>
  135. </Form>
  136. <Toaster
  137. position="top-center"
  138. reverseOrder={false}
  139. />
  140. <Backdrop
  141. sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
  142. open={open}
  143. onClick={() => false}
  144. >
  145. <CircularProgress color="inherit" />
  146. </Backdrop>
  147. </FormikProvider>
  148. )
  149. }