resume.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 useAuth from '../../../Auth/useAuth.js'
  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. const fmt = React.useRef({ weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' })
  28. const [pwdID, setPwdID] = React.useState(null);
  29. const [loading, setLoading] = React.useState(false);
  30. const auth = useAuth();
  31. const token = auth.getToken();
  32. const queryClient = useQueryClient();
  33. const savePassword = async (body) => {
  34. let rest = new Service('/contrasenia/create')
  35. return await rest.postQuery(body, token)
  36. }
  37. const saveCandidato = async (body) => {
  38. let rest = new Service('/passwordcandidato/candidato')
  39. return await rest.postQuery(body, token)
  40. }
  41. const pwdMutation = useMutation('password', savePassword);
  42. const candiMutation = useMutation('candidato', saveCandidato);
  43. const saveStepper = () => {
  44. setLoading(true);
  45. let {
  46. deadpwd, dateToActived, puesto, pwd,
  47. nombres, apellidos, sendmail, nombrepuesto, nombreEmpresa,mail
  48. } = key;
  49. console.log("KEY: ", key)
  50. let pwdBody = {
  51. id: -1,
  52. pwd,
  53. link: "www.psicoadmin.com",
  54. deadpwd: new Date(deadpwd).toISOString(),
  55. state: 1,
  56. dateToActived: new Date(dateToActived).toISOString(),
  57. plaza_id: puesto[0].id
  58. }
  59. pwdMutation.mutate(pwdBody, {
  60. onSuccess: (data) => {
  61. let { id: password_id } = data.data;
  62. setPwdID(password_id);
  63. let candidatoBody = {
  64. id: -1,
  65. nombres,
  66. apellidos,
  67. sendmail: sendmail ? 1 : 0,
  68. mail,
  69. idContrasenia: password_id,
  70. nombrepuesto,
  71. nombreEmpresa
  72. }
  73. candiMutation.mutate(candidatoBody, {
  74. onSuccess: (data) => {
  75. queryClient.invalidateQueries('passwords')
  76. toast.success("Contraseña agregada exitosamente!!")
  77. setTimeout(() => {
  78. console.log("OK LETS GO >> ", data,pwdID)
  79. setLoading(false);
  80. handleClose();
  81. handleReset();
  82. }, 1000)
  83. },
  84. onError: () => {
  85. toast.error("Ups!! error al crear el candidato")
  86. setLoading(false);
  87. }
  88. })
  89. },
  90. onError: () => {
  91. console.log("No se pudo guardar pwd")
  92. setLoading(false);
  93. toast.error("Ups!! Ocurrio un error, inténtalo más tarde")
  94. }
  95. })
  96. }
  97. return (
  98. <React.Fragment>
  99. <ThemeProvider theme={theme}>
  100. {loading ? (
  101. <Box sx={{ paddingBottom: 3 }}>
  102. <LinearProgress color="inherit" />
  103. </Box>
  104. ) : null}
  105. <Table>
  106. <thead>
  107. <tr>
  108. <th>{key.pwd} ✅</th>
  109. <th></th>
  110. </tr>
  111. </thead>
  112. <tbody>
  113. <tr>
  114. <td className="title_td">{"Candidato"}</td>
  115. <td colSpan={2}>{key.nombres + " " + key.apellidos} - {key.mail}</td>
  116. </tr>
  117. <tr>
  118. <td className="title_td">{"Puesto"}</td>
  119. <td colSpan={2}>{key.puesto.length > 0 ? key.puesto[0].nombrepuesto : ''}</td>
  120. </tr>
  121. <tr>
  122. <td className="title_td">{"Empresa"}</td>
  123. <td colSpan={2}>{key.nombreEmpresa}</td>
  124. </tr>
  125. <tr>
  126. <td className="title_td">{"Fecha Activación"}</td>
  127. <td colSpan={2}>{new Date(key.dateToActived).toLocaleDateString('es-GT', fmt.current)}</td>
  128. </tr>
  129. <tr>
  130. <td className="title_td">{"Fecha de Vencimiento"}</td>
  131. <td colSpan={2}>{new Date(key.deadpwd).toLocaleDateString('es-GT', fmt.current)}</td>
  132. </tr>
  133. </tbody>
  134. </Table>
  135. <Box sx={{ mb: 2 }}>
  136. <div style={{ paddingTop: 15 }}>
  137. <Button
  138. disabled={loading}
  139. style={{
  140. color: loading ? 'white' : ''
  141. }}
  142. onClick={saveStepper}
  143. className="registerBtn"
  144. variant="contained"
  145. sx={{ mt: 1, mr: 1 }}
  146. >
  147. {'Guardar'}
  148. </Button>
  149. <Button
  150. disabled={loading}
  151. onClick={handleBack}
  152. sx={{ mt: 1, mr: 1 }}
  153. >
  154. Regresar
  155. </Button>
  156. </div>
  157. </Box>
  158. </ThemeProvider>
  159. <Backdrop
  160. sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
  161. open={loading}
  162. onClick={() => console.log("close fetching")} >
  163. <CircularProgress color="inherit" />
  164. </Backdrop>
  165. <Toaster position="bottom-right" />
  166. </React.Fragment>
  167. )
  168. }