Reac front end for psicometric app

resume.jsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import * as React from 'react';
  2. import { Table } from 'react-bootstrap';
  3. import {
  4. Box, Button, LinearProgress,
  5. Backdrop, CircularProgress
  6. } from '@mui/material';
  7. import toast, { Toaster } from 'react-hot-toast';
  8. import { useMutation, useQueryClient } from 'react-query';
  9. import { Service } from '../../../Utils/HTTP.js'
  10. import { useSelector } from 'react-redux';
  11. import { createTheme, ThemeProvider } from '@mui/material/styles';
  12. let theme = createTheme({
  13. status: {
  14. success: '#fd4b4b'
  15. },
  16. palette: {
  17. primary: {
  18. main: '#fd4b4b',
  19. },
  20. secondary: {
  21. main: '#fd4b4b',
  22. },
  23. },
  24. });
  25. export function Resume(props) {
  26. let { handleBack, password: key, handleClose, handleReset } = props
  27. console.log(key)
  28. const fmt = React.useRef({ weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' })
  29. const [loading, setLoading] = React.useState(false);
  30. const auth = useSelector((state) => state.token)
  31. const profile = useSelector((state) => state.user.profile)
  32. const queryClient = useQueryClient();
  33. const savePassword = async (body) => {
  34. let rest = new Service('/contrasenia/create')
  35. return await rest.postQuery(body, auth.token)
  36. }
  37. const saveCandidato = async (body) => {
  38. let rest = new Service('/passwordcandidato/candidato')
  39. return await rest.postQuery(body, auth.token)
  40. }
  41. const pwdMutation = useMutation('password', savePassword);
  42. const candiMutation = useMutation('candidato', saveCandidato);
  43. console.log(candiMutation)
  44. const saveStepper = () => {
  45. setLoading(true);
  46. let { deadpwd, dateToActived } = key;
  47. let pwdBody = {
  48. id: -1,
  49. pwd: btoa(key.pwd),
  50. link: "www.psicoadmin.com",
  51. deadpwd: new Date(deadpwd).toISOString(),
  52. state: 1,
  53. dateToActived: new Date(dateToActived).toISOString(),
  54. plaza_id: key.puesto[0].id
  55. }
  56. console.log("PWD BODY: ", pwdBody)
  57. pwdMutation.mutate(pwdBody, {
  58. onSuccess: (result) => {
  59. let { id: idContrasenia } = result.data;
  60. let candidatos_body = key.candidatos.map((candi) => ({
  61. "id": -1,
  62. "nombres": candi.nombres,
  63. "apellidos": candi.apellidos,
  64. "mail": candi.mail,
  65. "sendmail": 0,
  66. "idContrasenia": idContrasenia,
  67. "nombrepuesto": 'Senior Software',
  68. "nombreEmpresa": 'Google'
  69. }))
  70. console.log(candidatos_body)
  71. // setLoading(false);
  72. // return;
  73. queryClient.invalidateQueries('passwords')
  74. setLoading(false);
  75. handleClose();
  76. handleReset();
  77. /*
  78. candiMutation.mutate(candidatos_body, {
  79. onSuccess: (data) => {
  80. queryClient.invalidateQueries('passwords')
  81. toast.success("Contraseña agregada exitosamente!!")
  82. setTimeout(() => {
  83. console.log("OK LETS GO >> ", data)
  84. setLoading(false);
  85. handleClose();
  86. handleReset();
  87. }, 1000)
  88. },
  89. onError: () => {
  90. toast.error("Ups!! error al crear el candidato")
  91. setLoading(false);
  92. }
  93. })
  94. */
  95. },
  96. onError: () => {
  97. console.log("No se pudo guardar pwd")
  98. setLoading(false);
  99. toast.error("Ups!! Ocurrio un error, inténtalo más tarde")
  100. }
  101. })
  102. }
  103. const getLi = (user, i) => {
  104. return (
  105. <li key={i} style={{ listStyleType: 'none' }}>
  106. {user.nombres + " " + user.apellidos} - {user.mail}
  107. </li>
  108. )
  109. }
  110. return (
  111. <React.Fragment>
  112. <ThemeProvider theme={theme}>
  113. {loading ? (
  114. <Box sx={{ paddingBottom: 3 }}>
  115. <LinearProgress color="inherit" />
  116. </Box>
  117. ) : null}
  118. <Table>
  119. <thead>
  120. <tr className="table_password">
  121. <th>{"Contraseña: "}</th>
  122. <th>{key.pwd} - {btoa(key.pwd)}</th>
  123. </tr>
  124. </thead>
  125. <tbody>
  126. <tr>
  127. <td className="title_td">{"Candidatos:"}</td>
  128. <td colSpan={2}>
  129. <ul style={{ margin: 0, padding: 0 }}>
  130. {key.candidatos ? key.candidatos.map((u, i) => getLi(u, i)) : null}
  131. </ul>
  132. </td>
  133. </tr>
  134. <tr>
  135. <td className="title_td">{"Puesto:"}</td>
  136. <td colSpan={2}>{key.puesto.length > 0 ? key.puesto[0].nombrepuesto : ''}</td>
  137. </tr>
  138. <tr>
  139. <td className="title_td">{"Empresa:"}</td>
  140. <td colSpan={2}>{profile.nombre}</td>
  141. </tr>
  142. <tr>
  143. <td className="title_td">{"Fecha Activación:"}</td>
  144. <td colSpan={2}>{new Date(key.dateToActived).toLocaleDateString('es-GT', fmt.current)}</td>
  145. </tr>
  146. <tr>
  147. <td className="title_td">{"Fecha de Vencimiento:"}</td>
  148. <td colSpan={2}>{new Date(key.deadpwd).toLocaleDateString('es-GT', fmt.current)}</td>
  149. </tr>
  150. </tbody>
  151. </Table>
  152. <Box sx={{ mb: 2 }}>
  153. <div style={{ paddingTop: 15 }}>
  154. <Button
  155. disabled={loading}
  156. style={{
  157. color: loading ? 'white' : ''
  158. }}
  159. onClick={saveStepper}
  160. className="registerBtn"
  161. variant="contained"
  162. sx={{ mt: 1, mr: 1 }}
  163. >
  164. {'Guardar'}
  165. </Button>
  166. <Button
  167. disabled={loading}
  168. onClick={handleBack}
  169. sx={{ mt: 1, mr: 1 }}
  170. >
  171. Regresar
  172. </Button>
  173. </div>
  174. </Box>
  175. </ThemeProvider>
  176. <Backdrop
  177. sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
  178. open={loading}
  179. onClick={() => console.log("close fetching")} >
  180. <CircularProgress color="inherit" />
  181. </Backdrop>
  182. <Toaster position="bottom-right" />
  183. </React.Fragment>
  184. )
  185. }