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 { Toast } from 'primereact/toast';
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);
const [selectedAdministrators, setSelectedAdministrators] = useState(null);
const [globalFilter, setGlobalFilter] = useState(null);
const [deleteAdminSystemDialog, setDeleteAdminSystemDialog] = useState(false);
const [deleteAdminsSystemDialog, setDeleteAdminsSystemDialog] = useState(false);
const toast = useRef(null);
const dt = useRef(null);
let emptySysAdmin = {
@ -95,11 +98,12 @@ const AdministradoresSistema = () => {
setDeleteAdminSystemDialog(true);
}
const confirmDeleteSelected = () => {
setDeleteAdminsSystemDialog(true);
}
const deleteSysAdmin = () => {
var data = {
id: sysadmin._id
}
fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, {
cache: 'no-cache',
method: 'DELETE',
@ -109,7 +113,7 @@ const AdministradoresSistema = () => {
})
.then(
function (response) {
if (response.status != 201 )
if (response.status != 201)
console.log('Ocurrió un error con el servicio: ' + response.status);
else
return response.json();
@ -120,10 +124,10 @@ const AdministradoresSistema = () => {
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 });
setDeleteAdminSystemDialog(false);
setSysAdmin(emptySysAdmin);
toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador del Sistema Eliminado', life: 3000 });
}
)
.catch(
@ -132,19 +136,62 @@ const AdministradoresSistema = () => {
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) => {
return (
<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>
);
}
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} />
@ -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 (
<div className="grid">
<div className="col-12">
<Toast ref={toast} />
<div className="card">
<h5>Administradores del sistema</h5>
<DataTable value={administrators} scrollable scrollHeight="400px" scrollDirection="both" className="mt-3">
<Column field="name" header="Nombre" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="last_name" header="Apellidos" style={{ flexGrow: 1, flexBasis: '160px' }} alignFrozen="left"></Column>
<Column field="dni" header="Identificación" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="email" header="Correo electrónico" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="phone" header="Telefóno" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column body={actionsAdmin}></Column>
< 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"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
currentPageReportTemplate="Mostrando {first} a {last} de {totalRecords} administradores del sistema"
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>
<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' }} />
{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>
</Dialog>
</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);