arreglar css de la tabla y eliminaciones masivas

This commit is contained in:
Mariela 2022-07-22 02:20:20 -06:00
parent ed88d5ace0
commit 40ec94708b
1 changed files with 93 additions and 23 deletions

View File

@ -5,16 +5,19 @@ import { DataTable } from 'primereact/datatable';
import { Column } from 'primereact/column'; import { Column } from 'primereact/column';
import { Toast } from 'primereact/toast'; import { Toast } from 'primereact/toast';
import { Dialog } from 'primereact/dialog'; import { Dialog } from 'primereact/dialog';
import { Toolbar } from 'primereact/toolbar';
const AdministradoresSistema = () => { const AdministradoresSistema = () => {
const [administrators, setAdministrators] = useState([]); const [administrators, setAdministrators] = useState([]);
const [urlFetch, setUrlFetch] = useState('http://localhost:4000/user/findAdminSistema/'); const [urlFetch, setUrlFetch] = useState('http://localhost:4000/user/findAdminSistema/');
const [sysadmin, setSysAdmin] = useState(emptySysAdmin); const [sysadmin, setSysAdmin] = useState(emptySysAdmin);
const [selectedAdministrators, setSelectedAdministrators] = useState(null);
const [globalFilter, setGlobalFilter] = useState(null);
const [deleteAdminSystemDialog, setDeleteAdminSystemDialog] = useState(false); const [deleteAdminSystemDialog, setDeleteAdminSystemDialog] = useState(false);
const [deleteAdminsSystemDialog, setDeleteAdminsSystemDialog] = useState(false); const [deleteAdminsSystemDialog, setDeleteAdminsSystemDialog] = useState(false);
const toast = useRef(null); const toast = useRef(null);
const dt = useRef(null);
let emptySysAdmin = { let emptySysAdmin = {
@ -95,11 +98,12 @@ const AdministradoresSistema = () => {
setDeleteAdminSystemDialog(true); setDeleteAdminSystemDialog(true);
} }
const confirmDeleteSelected = () => {
setDeleteAdminsSystemDialog(true);
}
const deleteSysAdmin = () => { const deleteSysAdmin = () => {
var data = {
id: sysadmin._id
}
fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, { fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, {
cache: 'no-cache', cache: 'no-cache',
method: 'DELETE', method: 'DELETE',
@ -132,19 +136,62 @@ const AdministradoresSistema = () => {
toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Administrador del Sistema no se pudo eliminar', life: 3000 }); toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Administrador del Sistema no se pudo eliminar', life: 3000 });
} }
); );
} }
const deleteSelectedAdminsSystem = () => {
let _administrators = administrators.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'
}
})
})
setAdministrators(_administrators);
setDeleteAdminsSystemDialog(false);
setSelectedAdministrators(null);
toast.current.show({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 });
}
const actionsAdmin = (rowData) => { const actionsAdmin = (rowData) => {
return ( return (
<div className="actions"> <div className="actions">
<Button icon="pi pi-trash" className="p-button-rounded p-button-warning mt-2" onClick={() => confirmDeleteAdminSystem(rowData)} /> <Button icon="pi pi-trash" className="p-button-rounded p-button-danger mt-2" onClick={() => confirmDeleteAdminSystem(rowData)} />
</div> </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 = ( const deleteAdminSystemDialogFooter = (
<> <>
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminSystemDialog} /> <Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminSystemDialog} />
@ -152,27 +199,47 @@ const AdministradoresSistema = () => {
</> </>
); );
const deleteAdminsSystemDialogFooter = (
<>
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminsSystemsDialog} />
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteSelectedAdminsSystem} />
</>
);
return ( return (
<div className="grid"> <div className="grid">
<div className="col-12"> <div className="col-12">
<Toast ref={toast} /> <Toast ref={toast} />
<div className="card"> <div className="card">
<h5>Administradores del sistema</h5> < Toolbar className="mb-4" left={leftToolbarTemplate} right={rightToolbarTemplate}></Toolbar>
<DataTable value={administrators} scrollable scrollHeight="400px" scrollDirection="both" className="mt-3">
<Column field="name" header="Nombre" style={{ flexGrow: 1, flexBasis: '160px' }}></Column> <DataTable ref={dt} value={administrators} dataKey="_id" paginator rows={5} selection={selectedAdministrators} onSelectionChange={(e) => setSelectedAdministrators(e.value)}
<Column field="last_name" header="Apellidos" style={{ flexGrow: 1, flexBasis: '160px' }} alignFrozen="left"></Column> scrollable scrollHeight="400px" scrollDirection="both" header={header}
<Column field="dni" header="Identificación" style={{ flexGrow: 1, flexBasis: '160px' }}></Column> rowsPerPageOptions={[5, 10, 25]} className="datatable-responsive mt-3"
<Column field="email" header="Correo electrónico" style={{ flexGrow: 1, flexBasis: '160px' }}></Column> paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
<Column field="phone" header="Telefóno" style={{ flexGrow: 1, flexBasis: '160px' }}></Column> currentPageReportTemplate="Mostrando {first} a {last} de {totalRecords} administradores del sistema"
<Column body={actionsAdmin}></Column> globalFilter={globalFilter} emptyMessage="No hay administradores del sistema registrados."
>
<Column selectionMode="multiple" headerStyle={{ width: '3rem' }}></Column>
<Column field="name" sortable header="Nombre" style={{ flexGrow: 1, minWidth: '160px', maxWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="last_name" sortable header="Apellidos" style={{ flexGrow: 1, minWidth: '160px', maxWidth: '160px', wordBreak: 'break-word' }} alignFrozen="left"></Column>
<Column field="dni" sortable header="Identificación" style={{ flexGrow: 1, minWidth: '160px', maxWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="email" sortable header="Correo electrónico" style={{ flexGrow: 1, minWidth: '200px', maxWidth: '200px', wordBreak: 'break-word' }}></Column>
<Column field="phone" sortable header="Telefóno" style={{ flexGrow: 1, minWidth: '160px', maxWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column header="Acciones" style={{ minWidth: '60px', maxWidth: '60px' }} body={actionsAdmin}></Column>
</DataTable> </DataTable>
<Dialog visible={deleteAdminSystemDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminSystemDialogFooter} onHide={hideDeleteAdminSystemDialog}> <Dialog visible={deleteAdminSystemDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminSystemDialogFooter} onHide={hideDeleteAdminSystemDialog}>
<div className="flex align-items-center justify-content-center"> <div className="flex align-items-center justify-content-center">
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} /> <i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
{sysadmin && <span>Are you sure you want to delete <b>{sysadmin.name}</b>?</span>} {sysadmin && <span>¿Estás seguro que desea eliminar a <b>{sysadmin.name}</b>?</span>}
</div>
</Dialog>
<Dialog visible={deleteAdminsSystemDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminsSystemDialogFooter} onHide={hideDeleteAdminsSystemsDialog}>
<div className="flex align-items-center justify-content-center">
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
{sysadmin && <span>¿Está seguro eliminar los adminsitradores del sistema seleccionados?</span>}
</div> </div>
</Dialog> </Dialog>
</div> </div>
@ -211,4 +278,7 @@ const AdministradoresSistema = () => {
) )
} }
export default React.memo(AdministradoresSistema); const comparisonFn = function (prevProps, nextProps) {
return prevProps.location.pathname === nextProps.location.pathname;
};
export default React.memo(AdministradoresSistema, comparisonFn);