From 40d0c8d1d12a08cf2df89aaa55cdbe11749aba39 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Mon, 22 Aug 2022 05:05:03 -0600 Subject: [PATCH] add format to edit guards --- .../src/components/GuardasSeguridad.js | 149 +++++++++++++----- 1 file changed, 112 insertions(+), 37 deletions(-) diff --git a/web-ui/web-react/src/components/GuardasSeguridad.js b/web-ui/web-react/src/components/GuardasSeguridad.js index 0c1b14d2..92336abb 100644 --- a/web-ui/web-react/src/components/GuardasSeguridad.js +++ b/web-ui/web-react/src/components/GuardasSeguridad.js @@ -13,6 +13,7 @@ import { faAt } from '@fortawesome/free-solid-svg-icons'; import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; import { useCookies } from "react-cookie"; +import classNames from 'classnames'; const GuardasSeguridad = () => { @@ -68,35 +69,60 @@ const GuardasSeguridad = () => { function registrarGuarda() { var data = { - dni: document.getElementById('identificacion').value, - name: document.getElementById('nombre').value, - last_name: document.getElementById('apellidos').value, - email: document.getElementById('correo_electronico').value, - phone: document.getElementById('telefono').value, - password: document.getElementById('correo_electronico').value, + dni: document.getElementById('dni').value, + name: document.getElementById('name').value, + last_name: document.getElementById('last_name').value, + email: document.getElementById('email').value, + phone: document.getElementById('phone').value, + password: document.getElementById('email').value, user_type: "4", //4 es guarda status: "1", community_id: cookies.community_id }; - console.log('ssss'); - fetch('http://localhost:4000/user/createGuard', { - cache: 'no-cache', - method: 'POST', - mode: 'cors', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json' - } - }).then((response) => { - if (response.status != 201) - console.log(`Ocurrió un error con el servicio: ${response.status}`); - else - return response.json(); - }).then(() => { - listaGuardasF(); - }).catch( - err => console.log('Ocurrió un error con el fetch', err) - ); + if (guarda._id === null) { + console.log('ssss'); + fetch('http://localhost:4000/user/createGuard', { + cache: 'no-cache', + method: 'POST', + mode: 'cors', + body: JSON.stringify(data), + headers: { + 'Content-Type': 'application/json' + } + }).then((response) => { + if (response.status != 201) + console.log(`Ocurrió un error con el servicio: ${response.status}`); + else + return response.json(); + }).then(() => { + listaGuardasF(); + }).catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } else { + data._id = guarda._id; + console.log(`Actualizando guarda: ${data}`); + fetch(`http://localhost:4000/user/updateGuard/${guarda._id}`, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(data), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (response.status !== 200) + console.log(`Ocurrió un error con el servicio: ${response.status}`); + else return response.json(); + }).then(() => { + toast.current.show({ + severity: 'success', + summary: 'Guarda actualizada', + detail: 'Guarda actualizado correctamente' + }); + setGuarda(emptyGuarda); + listaGuardasF(); + }) + } } const cambiarStatusUser = () => { @@ -410,6 +436,13 @@ const GuardasSeguridad = () => { ); }; + const onInputChange = (e, name) => { + const value = (e.target && e.target.value) || '' + let _guarda = { ...guarda } + _guarda[`${name}`] = value + setGuarda(_guarda) + } + return (
@@ -541,27 +574,69 @@ const GuardasSeguridad = () => {
-
Registro de un guarda de seguridad
+
Registro de un Guarda de Seguridad
- - + +
+
+ + + + onInputChange(e, 'name')} required autoFocus className={classNames({ 'p-invalid': submitted && guarda.name === '' })} /> +
+ {submitted && guarda.name === '' && Nombre es requerido.} +
- - + +
+
+ + + + onInputChange(e, 'last_name')} required autoFocus className={classNames({ 'p-invalid': submitted && guarda.last_name === '' })} /> +
+ {submitted && guarda.last_name === '' && Apellidos son requeridos.} +
- - + +
+
+ + + + onInputChange(e, 'email')} required autoFocus className={classNames({ 'p-invalid': submitted && guarda.email === '' })} /> +
+ {submitted && guarda.email === '' && Correo electrónico es requerido.} +
- - + +
+
+ + + + onInputChange(e, 'dni')} required autoFocus className={classNames({ 'p-invalid': submitted && guarda.dni === '' })} /> +
+ {submitted && guarda.email === '' && Identificación es requerida.} +
-
- - +
+ +
+
+ + + + onInputChange(e, 'phone')} type='tel' required autoFocus className={classNames({ 'p-invalid': submitted && guarda.phone === '' })} /> +
+ {submitted + && guarda.phone === '' + && Número de teléfono es requerido.} +