add functions

This commit is contained in:
Mariela 2022-07-22 14:59:59 -06:00
parent e34fb7d5cc
commit 4afbb4388f
2 changed files with 116 additions and 11 deletions

View File

@ -8,7 +8,7 @@ import { Dialog } from 'primereact/dialog';
import { Toolbar } from 'primereact/toolbar';
const AdministradoresSistema = () => {
``
const [administrators, setAdministrators] = useState([]);
const [urlFetch, setUrlFetch] = useState('http://localhost:4000/user/findAdminSistema/');
const [sysadmin, setSysAdmin] = useState(emptySysAdmin);
@ -19,7 +19,6 @@ const AdministradoresSistema = () => {
const toast = useRef(null);
const dt = useRef(null);
let emptySysAdmin = {
_id: null,
dni: '',
@ -123,10 +122,8 @@ const AdministradoresSistema = () => {
function (response) {
let _sysadmin = administrators.filter(val => val._id !== sysadmin._id);
setAdministrators(_sysadmin);
setDeleteAdminSystemDialog(false);
setSysAdmin(emptySysAdmin);
toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador del Sistema Eliminado', life: 3000 });
}
)
@ -155,7 +152,6 @@ const AdministradoresSistema = () => {
toast.current.show({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 });
}
const actionsAdmin = (rowData) => {
return (
<div className="actions">
@ -205,14 +201,13 @@ const AdministradoresSistema = () => {
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteSelectedAdminsSystem} />
</>
);
return (
<div className="grid">
<div className="col-12">
<Toast ref={toast} />
<div className="card">
< Toolbar className="mb-4" left={leftToolbarTemplate} right={rightToolbarTemplate}></Toolbar>
<DataTable ref={dt} value={administrators} dataKey="_id" paginator rows={5} selection={selectedAdministrators} onSelectionChange={(e) => setSelectedAdministrators(e.value)}
scrollable scrollHeight="400px" scrollDirection="both" header={header}
rowsPerPageOptions={[5, 10, 25]} className="datatable-responsive mt-3"
@ -227,9 +222,7 @@ const AdministradoresSistema = () => {
<Column field="email" sortable header="📧 Correo electrónico" style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="phone" sortable header="📞 Telefóno" style={{ flexGrow: 1,flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column header="Acciones" style={{ flexGrow: 1,flexBasis: '60px' }} body={actionsAdmin}></Column>
</DataTable>
<Dialog visible={deleteAdminSystemDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminSystemDialogFooter} onHide={hideDeleteAdminSystemDialog}>
<div className="flex align-items-center justify-content-center">
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
@ -273,12 +266,11 @@ const AdministradoresSistema = () => {
</div>
</div>
</div>
)
}
const comparisonFn = function (prevProps, nextProps) {
return prevProps.location.pathname === nextProps.location.pathname;
};
export default React.memo(AdministradoresSistema, comparisonFn);

View File

@ -11,6 +11,7 @@ import classNames from 'classnames';
const Communities = () => {
let emptyCommunity = {
_id: null,
name: '',
province: provinciaId,
canton: cantonId,
@ -227,6 +228,118 @@ const Communities = () => {
setCommunity(_community);
}
const hideDeleteAdminSystemDialog = () => {
setDeleteAdminSystemDialog(false);
}
const hideDeleteAdminsSystemsDialog = () => {
setDeleteAdminsSystemDialog(false);
}
const confirmDeleteAdminSystem = (sysAdmin) => {
setSysAdmin(sysAdmin);
setDeleteAdminSystemDialog(true);
}
const confirmDeleteSelected = () => {
setDeleteAdminsSystemDialog(true);
}
const deleteSysAdmin = () => {
fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, {
cache: 'no-cache',
method: 'DELETE',
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) {
let _sysadmin = communities.filter(val => val._id !== community._id);
setAdministrators(_sysadmin);
setDeleteAdminSystemDialog(false);
setSysAdmin(emptySysAdmin);
toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador del Sistema Eliminado', life: 3000 });
}
)
.catch(
err => {
console.log('Ocurrió un error con el fetch', err)
toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Administrador del Sistema no se pudo eliminar', life: 3000 });
}
);
}
const deleteSelectedAdminsSystem = () => {
let _communities = communitiesList.filter(val => !selectedAdministrators.includes(val));
selectedAdministrators.map((item) => {
fetch('http://localhost:4000/user/deleteAdminSystem/' + item._id, {
cache: 'no-cache',
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
})
setCommunitiesList(_communities);
setDeleteAdminsSystemDialog(false);
setSelectedAdministrators(null);
toast.current.show({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 });
}
const actionsAdmin = (rowData) => {
return (
<div className="actions">
<Button icon="pi pi-trash" className="p-button-rounded p-button-danger mt-2" onClick={() => confirmDeleteAdminSystem(rowData)} />
</div>
);
}
const leftToolbarTemplate = () => {
return (
<React.Fragment>
<div className="my-2">
<Button label="Eliminar" icon="pi pi-trash" className="p-button-danger" onClick={confirmDeleteSelected} disabled={!selectedAdministrators || !selectedAdministrators.length} />
</div>
</React.Fragment>
)
}
const rightToolbarTemplate = () => {
return (
<React.Fragment>
<Button label="Exportar" icon="pi pi-upload" className="p-button-help" />
</React.Fragment>
)
}
const header = (
<div className="flex flex-column md:flex-row md:justify-content-between md:align-items-center">
<h5 className="m-0">Administradores del sistema</h5>
<span className="block mt-2 md:mt-0 p-input-icon-left">
<i className="pi pi-search" />
<InputText type="search" onInput={(e) => setGlobalFilter(e.target.value)} placeholder="Buscar..." />
</span>
</div>
);
const deleteAdminSystemDialogFooter = (
<>
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminSystemDialog} />
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteSysAdmin} />
</>
);
return (
<div className="grid">
<div className="col-12">