agregar funciones en backend

This commit is contained in:
Mariela 2022-08-22 18:19:27 -06:00
parent 1865ce73ae
commit 81cb378a1a
1 changed files with 116 additions and 49 deletions

View File

@ -49,6 +49,7 @@ const AdministradoresComunidad = () => {
const dt = useRef(null); const dt = useRef(null);
const [changeStatusAdminCommunityDialog, setChangeStatusAdminCommunityDialog] = useState(false); const [changeStatusAdminCommunityDialog, setChangeStatusAdminCommunityDialog] = useState(false);
const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar");
async function listaAdmin() { async function listaAdmin() {
@ -151,22 +152,22 @@ const AdministradoresComunidad = () => {
} }
); );
}; };
const deleteSelectedAdminsCommunity = () => { const deleteSelectedAdminsCommunity = () => {
let _admins = listaAdmins.filter( let _admins = listaAdmins.filter(
(val) => !selectedAdminsCommunities.includes(val), (val) => !selectedAdminsCommunities.includes(val),
); );
selectedAdminsCommunities.map((item) => { selectedAdminsCommunities.map((item) => {
fetch('http://localhost:4000/user/deleteAdminCommunity/' + item._id, { fetch('http://localhost:4000/user/deleteAdminCommunity/' + item._id, {
cache: 'no-cache', cache: 'no-cache',
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}) })
}) })
setListaAdmins(_admins); setListaAdmins(_admins);
setDeleteAdminsCommunitiesDialog(false); setDeleteAdminsCommunitiesDialog(false);
setSelectedAdminsCommunities(null); setSelectedAdminsCommunities(null);
@ -225,51 +226,82 @@ const AdministradoresComunidad = () => {
} }
const saveAdminCommunity = () => { const saveAdminCommunity = () => {
if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) { let _administrators = [...listaAdmins];
let _administrators = [...listaAdmins]; if (adminCommunity._id) {
let _adminCommunity = { ...adminCommunity }; if (adminCommunity.name && adminCommunity.dni &&
_adminCommunity.community_id = communityId; adminCommunity.last_name && adminCommunity.email &&
console.log(_adminCommunity) adminCommunity.phone) {
console.log(communityId)
fetch('http://localhost:4000/user/createAdminCommunity', { let _adminCommunity = { ...adminCommunity };
cache: 'no-cache', _adminCommunity.community_id = communityId;
method: 'POST',
body: JSON.stringify(_adminCommunity), fetch('http://localhost:4000/user/createAdminCommunity', {
headers: { cache: 'no-cache',
'Content-Type': 'application/json' method: 'POST',
} body: JSON.stringify(_adminCommunity),
}) headers: {
.then( 'Content-Type': 'application/json'
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( .then(
err => console.log('Ocurrió un error con el fetch', err) 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 { } 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 = () => { const hideDeleteAdminCommunityDialog = () => {
@ -298,6 +330,18 @@ const AdministradoresComunidad = () => {
setChangeStatusAdminCommunityDialog(true); setChangeStatusAdminCommunityDialog(true);
}; };
const editAdmin = (admin) => {
setAdminCommunity(admin);
console.log(admin);
setSaveButtonTitle('Actualizar');
}
const cancelEdit = () => {
setAdminCommunity(adminCommunity);
setSaveButtonTitle('Registrar');
}
const actionsAdminCommunity = (rowData) => { const actionsAdminCommunity = (rowData) => {
let icono = ''; let icono = '';
let text = ''; let text = '';
@ -310,15 +354,22 @@ const AdministradoresComunidad = () => {
} }
return ( return (
<div className="actions"> <div className="actions">
<Button
icon="pi pi-pencil"
className="p-button-rounded p-button-success mt-2 mx-2"
onClick={() => editAdmin(rowData)}
title="Editar"
/>
<Button <Button
icon={`${icono}`} icon={`${icono}`}
className="p-button-rounded p-button-warning mt-2 mx-2" className="p-button-rounded p-button-warning mt-2 mx-2"
onClick={() => confirmChangeStatuAdminCommunity(rowData)} onClick={() => confirmChangeStatuAdminCommunity(rowData)}
title={`${text}`} title={`${text}`}
/> />
<Button <Button
icon="pi pi-trash" icon="pi pi-trash"
className="p-button-rounded p-button-danger mt-2" className="p-button-rounded p-button-danger mt-2 mx-2"
onClick={() => confirmDeleteAdminCommunity(rowData)} onClick={() => confirmDeleteAdminCommunity(rowData)}
/> />
</div> </div>
@ -503,7 +554,7 @@ const AdministradoresComunidad = () => {
<Column field="phone" header={headerPhone} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column> <Column field="phone" header={headerPhone} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="community_name" sortable header={headerCommuntiy} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column> <Column field="community_name" sortable header={headerCommuntiy} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="status" sortable header={headerStatus} body={statusBodyTemplate} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column> <Column field="status" sortable header={headerStatus} body={statusBodyTemplate} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column style={{ flexGrow: 1, flexBasis: '130px', minWidth: '130px' }} body={actionsAdminCommunity}></Column> <Column style={{ flexGrow: 1, flexBasis: '80px', minWidth: '80px' }} body={actionsAdminCommunity}></Column>
</DataTable> </DataTable>
<Dialog visible={deleteAdminCommunityDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminCommunityDialogFooter} onHide={hideDeleteAdminCommunityDialog}> <Dialog visible={deleteAdminCommunityDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminCommunityDialogFooter} onHide={hideDeleteAdminCommunityDialog}>
<div className="flex align-items-center justify-content-center"> <div className="flex align-items-center justify-content-center">
@ -617,7 +668,23 @@ const AdministradoresComunidad = () => {
{submitted && !communityId && <small className="p-invalid">Comunidad es requirida.</small>} {submitted && !communityId && <small className="p-invalid">Comunidad es requirida.</small>}
</div> </div>
</div> </div>
<Button label="Registrar" onClick={saveAdminCommunity} /> <div style={{
display: "flex",
justifyContent: "center",
gap: "10px",
width: "100%"
}}>
<Button
label={`${saveButtonTitle}`}
onClick={saveAdminCommunity}
/>
{saveButtonTitle === 'Actualizar' && (
<Button
label="Cancelar"
onClick={cancelEdit}
className="p-button-danger" />)}
</div>
</div> </div>
</div> </div>
</div> </div>