diff --git a/web-ui/web-react/src/components/AdministradoresComunidad.js b/web-ui/web-react/src/components/AdministradoresComunidad.js index 0ec73891..ece37bf1 100644 --- a/web-ui/web-react/src/components/AdministradoresComunidad.js +++ b/web-ui/web-react/src/components/AdministradoresComunidad.js @@ -49,6 +49,7 @@ const AdministradoresComunidad = () => { const dt = useRef(null); const [changeStatusAdminCommunityDialog, setChangeStatusAdminCommunityDialog] = useState(false); + const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar"); async function listaAdmin() { @@ -151,22 +152,22 @@ const AdministradoresComunidad = () => { } ); - + }; const deleteSelectedAdminsCommunity = () => { let _admins = listaAdmins.filter( (val) => !selectedAdminsCommunities.includes(val), ); - selectedAdminsCommunities.map((item) => { - fetch('http://localhost:4000/user/deleteAdminCommunity/' + item._id, { - cache: 'no-cache', - method: 'DELETE', - headers: { - 'Content-Type': 'application/json' - } - }) - }) + selectedAdminsCommunities.map((item) => { + fetch('http://localhost:4000/user/deleteAdminCommunity/' + item._id, { + cache: 'no-cache', + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' + } + }) + }) setListaAdmins(_admins); setDeleteAdminsCommunitiesDialog(false); setSelectedAdminsCommunities(null); @@ -225,51 +226,82 @@ const AdministradoresComunidad = () => { } const saveAdminCommunity = () => { - if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) { + let _administrators = [...listaAdmins]; - let _administrators = [...listaAdmins]; - let _adminCommunity = { ...adminCommunity }; - _adminCommunity.community_id = communityId; - console.log(_adminCommunity) - console.log(communityId) + if (adminCommunity._id) { + if (adminCommunity.name && adminCommunity.dni && + adminCommunity.last_name && adminCommunity.email && + adminCommunity.phone) { - fetch('http://localhost:4000/user/createAdminCommunity', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(_adminCommunity), - 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(); + let _adminCommunity = { ...adminCommunity }; + _adminCommunity.community_id = communityId; + + fetch('http://localhost:4000/user/createAdminCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_adminCommunity), + headers: { + 'Content-Type': 'application/json' } - ) - .then(() => { - - // _adminCommunity.community_id = communitiesList.find(c => c._id === _adminCommunity.community_id).name - - _administrators.push(_adminCommunity); - toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Administrador de Comunidad de vivienda Creada', life: 3000 }); - - setListaAdmins(_administrators); - - setAdminCommunity(emptyAdminCommunity); - }) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); + .then( + function (response) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else + return response.json(); + } + ) + .then(() => { + + // _adminCommunity.community_id = communitiesList.find(c => c._id === _adminCommunity.community_id).name + + _administrators.push(_adminCommunity); + toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Administrador de Comunidad de vivienda Creada', life: 3000 }); + + setListaAdmins(_administrators); + + setAdminCommunity(emptyAdminCommunity); + + }) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } else { + setSubmitted(true); + } } else { - setSubmitted(true); + let _admin = { ...adminCommunity }; + console.log(`Actualizando admnistrador de comunidad: ${_admin}`) + fetch(`http://localhost:4000/user/updateAdminCommunity/${_admin._id}`, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(_admin), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (response.status !== 200) + console.log(`Hubo un error en el servicio: ${response.status}`) + else return response.json() + }).then(() => { + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Inquilino editado', + life: 3000, + }) + _administrators.push(_admin); + toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador de Comunidad de vivienda Actualizada', life: 3000 }); + setListaAdmins(_administrators); + + setAdminCommunity(emptyAdminCommunity); + }) } + } const hideDeleteAdminCommunityDialog = () => { @@ -298,6 +330,18 @@ const AdministradoresComunidad = () => { setChangeStatusAdminCommunityDialog(true); }; + const editAdmin = (admin) => { + setAdminCommunity(admin); + console.log(admin); + setSaveButtonTitle('Actualizar'); + } + + + const cancelEdit = () => { + setAdminCommunity(adminCommunity); + setSaveButtonTitle('Registrar'); + } + const actionsAdminCommunity = (rowData) => { let icono = ''; let text = ''; @@ -310,15 +354,22 @@ const AdministradoresComunidad = () => { } return (
+
@@ -503,7 +554,7 @@ const AdministradoresComunidad = () => { - +
@@ -617,7 +668,23 @@ const AdministradoresComunidad = () => { {submitted && !communityId && Comunidad es requirida.}
-