From 18bab545e783ae30fcb4aad11c766a845c4cd66b Mon Sep 17 00:00:00 2001 From: Mariela Date: Mon, 22 Aug 2022 11:00:08 -0600 Subject: [PATCH] fix stylos y validaciones del registro --- .../src/components/AdministradoresSistema.js | 332 ++++++++++++------ 1 file changed, 227 insertions(+), 105 deletions(-) diff --git a/web-ui/web-react/src/components/AdministradoresSistema.js b/web-ui/web-react/src/components/AdministradoresSistema.js index dd3f84af..7bd0bd6b 100644 --- a/web-ui/web-react/src/components/AdministradoresSistema.js +++ b/web-ui/web-react/src/components/AdministradoresSistema.js @@ -15,6 +15,22 @@ import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; import classNames from 'classnames'; const AdministradoresSistema = () => { + + + let emptySysAdmin = { + _id: null, + dni: '', + name: '', + last_name: '', + email: '', + phone: '', + password: '', + user_type: '1', + status: '1', + status_text: '', + }; + + const [administrators, setAdministrators] = useState([]); const [urlFetch, setUrlFetch] = useState( 'http://localhost:4000/user/findAdminSistema/', @@ -37,19 +53,6 @@ const AdministradoresSistema = () => { const [editAdminDialog, setEditAdminDialog] = useState(false); - let emptySysAdmin = { - _id: null, - dni: '', - name: '', - last_name: '', - email: '', - phone: '', - password: '', - user_type: '1', - status: '1', - status_text: '', - }; - async function fetchP() { let nombres = await fetch(urlFetch, { method: 'GET' }); let adminRes = await nombres.json(); @@ -82,96 +85,110 @@ const AdministradoresSistema = () => { } + const findRepeated = (name, value) => { + let _administrators = [...administrators]; + let value_filtered = _administrators.filter(item => item[`${name}`] === value); + return value_filtered.length + } + + function guardarAdmin() { let _administrators = [...administrators]; let _admin = { ...sysadmin }; - if (sysadmin._id) { - if (sysadmin.name && sysadmin.dni && sysadmin.last_name && sysadmin.email && - sysadmin.phone) { + if (_admin.name && _admin.dni && _admin.last_name && _admin.email && + _admin.phone) { - fetch('http://localhost:4000/user/updateAdminSystem/', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(_admin), - headers: { - 'Content-Type': 'application/json' - } - }) - .then( - function (response) { - if (response.status != 201) - console.log('Ocurrió un error con el servicio: ' + response.status); - else - return response.json(); - } - ) - .then( - function (response) { - const index = findIndexById(sysadmin._id); - _administrators[index] = _admin; - toast.current.show({ - severity: 'success', - summary: 'Exito', - detail: 'Administrador Actualizado', - life: 3000, - }); - setAdministrators(_administrators) - setEditAdminDialog(false); - setSysAdmin(emptySysAdmin); - } - ) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); - } else { + if (findRepeated('email', _admin.email) || findRepeated('dni', _admin.dni)) { setSubmitted(true); + } else { + if (_admin._id) { + + + + fetch('http://localhost:4000/user/updateAdminSystem/', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_admin), + headers: { + 'Content-Type': 'application/json' + } + }) + .then( + function (response) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else + return response.json(); + } + ) + .then( + function (response) { + const index = findIndexById(sysadmin._id); + + _administrators[index] = _admin; + toast.current.show({ + severity: 'success', + summary: 'Exito', + detail: 'Administrador Actualizado', + life: 3000, + }); + setAdministrators(_administrators) + setEditAdminDialog(false); + setSysAdmin(emptySysAdmin); + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + + + + + + + } else { + + + fetch('http://localhost:4000/user/createAdminSystem/', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_admin), + headers: { + 'Content-Type': 'application/json' + } + }) + .then( + function (response) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else + return response.json(); + } + ) + .then( + function (response) { + _administrators.push(_admin); + setAdministrators(_administrators) + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } } - } else { - 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, - user_type: "1", //1 es admin - status: "1" - }; - setSysAdmin(data) - fetch('http://localhost:4000/user/createAdminSystem/', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json' - } - }) - .then( - function (response) { - if (response.status != 201) - console.log('Ocurrió un error con el servicio: ' + response.status); - else - return response.json(); - } - ) - .then( - function (response) { - _administrators.push(_admin); - setAdministrators(_administrators) - } - ) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); + + } else { + setSubmitted(true); + } } @@ -256,11 +273,14 @@ const AdministradoresSistema = () => { const infoAdmin = (sysadmin) => { setSysAdmin({ ...sysadmin }); setAdminDialog(true); + }; const hideEditAdminDialog = () => { setSubmitted(false); setEditAdminDialog(false); + setSysAdmin(emptySysAdmin); + }; const editAdmin = (sysadmin) => { @@ -862,9 +882,10 @@ const AdministradoresSistema = () => { {submitted && sysadmin.name === '' && Nombre es requirido.} + -
+
@@ -897,13 +918,16 @@ const AdministradoresSistema = () => { required autoFocus className={classNames({ - 'p-invalid': submitted && sysadmin.email === '', + 'p-invalid': submitted && (sysadmin.email === '' || findRepeated('email', sysadmin.email) > 0), })} />
{submitted && sysadmin.email === '' && ( Correo electrónico es requerido. )} + {submitted && findRepeated('email', sysadmin.email) > 0 && + Correo electrónico se encuentra repetido. + }
@@ -918,13 +942,16 @@ const AdministradoresSistema = () => { required autoFocus className={classNames({ - 'p-invalid': submitted && sysadmin.dni === '', + 'p-invalid': submitted && (sysadmin.dni === '' || findRepeated('dni', sysadmin.dni)), })} />
{submitted && sysadmin.dni === '' && ( Identificación es requerida. )} + {submitted && findRepeated('dni', sysadmin.dni) > 0 && + Identificación se encuentra repetida. + }
@@ -959,29 +986,124 @@ const AdministradoresSistema = () => {
Registro de un administrador del sistema
-
- - +
+ + +
+
+ + + + onInputChange(e, 'name')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.name === '', + })} + /> +
+ {submitted && sysadmin.name === '' && + Nombre es requirido.} +
-
- - +
+ +
+
+ + + + onInputChange(e, 'last_name')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.last_name === '', + })} + /> +
+ {submitted && sysadmin.last_name === '' && ( + Apellido(s) es requerido. + )} +
-
+
- +
+
+ + + + onInputChange(e, 'email')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && (sysadmin.email === '' || findRepeated('email', sysadmin.email) > 0), + })} + /> +
+ {submitted && sysadmin.email === '' && ( + Correo electrónico es requerido. + )} + {submitted && findRepeated('email', sysadmin.email) > 0 && + Correo electrónico se encuentra repetido. + } +
-
- - +
+ +
+
+ + + + onInputChange(e, 'dni')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && (sysadmin.dni === '' || findRepeated('dni', sysadmin.dni) > 0), + })} + /> +
+ {submitted && sysadmin.dni === '' && ( + Identificación es requerida. + )} + {submitted && findRepeated('dni', sysadmin.dni) > 0 && + Identificación se encuentra repetida. + } +
- - + +
+
+ + + + onInputChange(e, 'phone')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.phone === '', + })} + /> +
+ {submitted && sysadmin.phone === '' && ( + Teléfono es requerido. + )} +
- + +
+
+
);