2022-08-01 05:52:01 +00:00
|
|
|
import React, { useEffect, useState, useRef } from 'react';
|
|
|
|
import { InputText } from 'primereact/inputtext';
|
2022-08-01 05:49:05 +00:00
|
|
|
import { Button } from 'primereact/button';
|
|
|
|
import { Dropdown } from 'primereact/dropdown';
|
2022-08-01 05:52:01 +00:00
|
|
|
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';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faHome, faUserAlt } from '@fortawesome/free-solid-svg-icons';
|
2022-08-01 07:51:54 +00:00
|
|
|
import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons';
|
2022-08-01 05:52:01 +00:00
|
|
|
import { faAt } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons';
|
2022-08-01 07:51:54 +00:00
|
|
|
import { faClipboardCheck } from '@fortawesome/free-solid-svg-icons';
|
2022-08-01 05:52:01 +00:00
|
|
|
import classNames from 'classnames';
|
2022-08-01 05:58:12 +00:00
|
|
|
import { useCookies } from "react-cookie";
|
2022-08-01 05:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
const AreasComunes = () => {
|
|
|
|
|
2022-08-01 05:52:01 +00:00
|
|
|
let emptyCommonArea = {
|
|
|
|
_id: null,
|
|
|
|
dni: '',
|
|
|
|
name: '',
|
2022-08-01 07:01:01 +00:00
|
|
|
hourMin: '',
|
|
|
|
hourMax: '',
|
2022-08-01 05:52:01 +00:00
|
|
|
community_id: '',
|
2022-08-01 07:01:01 +00:00
|
|
|
bookable: '1',
|
|
|
|
bookable_text: '',
|
2022-08-01 07:23:33 +00:00
|
|
|
status: '1',
|
|
|
|
status_text: '',
|
2022-08-01 05:52:01 +00:00
|
|
|
};
|
|
|
|
|
2022-08-01 06:31:52 +00:00
|
|
|
const [commonAreaList, setCommonAreaList] = useState([]);
|
2022-08-01 05:52:01 +00:00
|
|
|
const [commonArea, setCommonArea] = useState(emptyCommonArea);
|
2022-08-01 05:53:21 +00:00
|
|
|
const [selectedCommonAreas, setSelectedCommonAreas] = useState(null);
|
2022-08-01 06:31:52 +00:00
|
|
|
const [globalFilter, setGlobalFilter] = useState(null);
|
2022-08-01 05:53:21 +00:00
|
|
|
const [deleteCommonAreaDialog, setDeleteCommonAreaDialog] = useState(false);
|
|
|
|
const [deleteCommonAreasDialog, setDeleteCommonAreasDialog] = useState(false);
|
2022-08-01 05:52:01 +00:00
|
|
|
const [submitted, setSubmitted] = useState(false);
|
|
|
|
const toast = useRef(null);
|
|
|
|
const dt = useRef(null);
|
|
|
|
|
2022-08-01 05:58:12 +00:00
|
|
|
const [cookies, setCookie] = useCookies();
|
|
|
|
|
|
|
|
async function getCommonAreas() {
|
2022-08-01 06:31:52 +00:00
|
|
|
await fetch(`http://localhost:4000/commonArea/findByCommunity/${cookies.community_id}`, { method: 'GET' })
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then(data => data.message)
|
|
|
|
.then(data => {
|
2022-08-01 07:23:33 +00:00
|
|
|
if (data) {
|
2022-08-01 07:01:01 +00:00
|
|
|
data.map(item => {
|
2022-08-01 07:23:33 +00:00
|
|
|
if (item.bookable == '1') {
|
2022-08-01 07:51:54 +00:00
|
|
|
item.bookable_text = 'Necesaria';
|
2022-08-01 07:23:33 +00:00
|
|
|
} else {
|
2022-08-01 07:51:54 +00:00
|
|
|
item.bookable_text = 'No es necesarioa';
|
2022-08-01 07:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.status == '1') {
|
|
|
|
item.status_text = 'Activo';
|
2022-08-01 07:30:38 +00:00
|
|
|
} else if (item.status == '0') {
|
2022-08-01 07:23:33 +00:00
|
|
|
item.status_text = 'Inactivo';
|
|
|
|
} else {
|
|
|
|
item.status_text = 'Eliminado';
|
2022-08-01 07:01:01 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-08-01 07:30:38 +00:00
|
|
|
|
|
|
|
data = data.filter(
|
|
|
|
(val) => val.status != -1,
|
|
|
|
)
|
2022-08-01 07:01:01 +00:00
|
|
|
setCommonAreaList(data);
|
2022-08-01 06:31:52 +00:00
|
|
|
});
|
2022-08-01 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 06:31:52 +00:00
|
|
|
useEffect(() => {
|
|
|
|
getCommonAreas();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const deleteCommonArea = () => {
|
2022-08-01 07:23:33 +00:00
|
|
|
fetch('http://localhost:4000/commonArea/deleteCommonArea/' + commonArea._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 _common_areas = commonAreaList.filter(
|
|
|
|
(val) => val._id !== commonArea._id,
|
|
|
|
);
|
2022-08-01 07:30:38 +00:00
|
|
|
_common_areas = _common_areas.filter(
|
|
|
|
(val) => val.status != -1,
|
|
|
|
)
|
2022-08-01 07:23:33 +00:00
|
|
|
setCommonAreaList(_common_areas);
|
|
|
|
setDeleteCommonAreaDialog(false);
|
|
|
|
setCommonArea(emptyCommonArea);
|
|
|
|
toast.current.show({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Área Común Eliminada',
|
|
|
|
life: 3000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.catch(
|
|
|
|
err => {
|
|
|
|
console.log('Ocurrió un error con el fetch', err)
|
|
|
|
toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Área Común no se pudo eliminar', life: 3000 });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-08-01 06:31:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const deleteSelectedCommonAreas = () => {
|
2022-08-01 07:32:07 +00:00
|
|
|
let _common_areas = commonAreaList.filter(
|
2022-08-01 06:31:52 +00:00
|
|
|
(val) => !selectedCommonAreas.includes(val),
|
|
|
|
);
|
2022-08-01 07:30:38 +00:00
|
|
|
selectedCommonAreas.map((item) => {
|
|
|
|
fetch('http://localhost:4000/commonArea/deleteCommonArea/' + item._id, {
|
|
|
|
cache: 'no-cache',
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
_common_areas = _common_areas.filter(
|
|
|
|
(val) => val.status != -1,
|
|
|
|
)
|
2022-08-01 07:32:07 +00:00
|
|
|
setCommonAreaList(_common_areas);
|
2022-08-01 06:31:52 +00:00
|
|
|
setDeleteCommonAreasDialog(false);
|
|
|
|
setSelectedCommonAreas(null);
|
|
|
|
toast.current.show({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Éxito',
|
|
|
|
detail: 'Áreas Comúnes Eliminadas',
|
|
|
|
life: 3000,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const hideDeleteCommonAreaDialog = () => {
|
|
|
|
setDeleteCommonAreaDialog(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hideDeleteCommonAreasDialog = () => {
|
|
|
|
setDeleteCommonAreasDialog(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmDeleteCommonArea = (commonArea) => {
|
|
|
|
setCommonArea(commonArea);
|
|
|
|
setDeleteCommonAreaDialog(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmDeleteSelected = () => {
|
|
|
|
setDeleteCommonAreasDialog(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const actionsCommonArea = (rowData) => {
|
|
|
|
return (
|
|
|
|
<div className="actions">
|
|
|
|
<Button
|
|
|
|
icon="pi pi-trash"
|
|
|
|
className="p-button-rounded p-button-danger mt-2"
|
|
|
|
onClick={() => confirmDeleteCommonArea(rowData)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const deleteCommonAreaDialogFooter = (
|
|
|
|
<>
|
|
|
|
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteCommonAreaDialog} />
|
|
|
|
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteCommonArea} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const deleteCommonAreasDialogFooter = (
|
|
|
|
<>
|
|
|
|
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteCommonAreasDialog} />
|
|
|
|
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteSelectedCommonAreas} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const leftToolbarTemplate = () => {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<div className="my-2">
|
|
|
|
<Button label="Eliminar" icon="pi pi-trash" className="p-button-danger" onClick={confirmDeleteSelected} disabled={!selectedCommonAreas || !selectedCommonAreas.length} />
|
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const rightToolbarTemplate = () => {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<Button label="Exportar" icon="pi pi-upload" className="p-button-help" />
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const header = (
|
|
|
|
<React.Fragment>
|
|
|
|
|
|
|
|
<div className="flex flex-column md:flex-row md:justify-content-between md:align-items-center">
|
|
|
|
<h5 className="m-0">Áreas Comunes</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>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
const headerName = (
|
|
|
|
<>
|
|
|
|
<p>{' '}
|
|
|
|
<FontAwesomeIcon icon={faUserAlt} style={{ color: "#C08135" }} /> {' '}
|
|
|
|
Nombre
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
const headerHourMin = (
|
|
|
|
<>
|
|
|
|
<p>
|
|
|
|
{' '}
|
|
|
|
<FontAwesomeIcon icon={faUserAlt} style={{ color: "#D7A86E" }} />{' '}
|
|
|
|
Hora de apertura
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
const headerHourMax = (
|
|
|
|
<>
|
|
|
|
<p> {' '}
|
|
|
|
<FontAwesomeIcon icon={faIdCardAlt} style={{ color: "#C08135" }} />{' '}
|
|
|
|
Hora de cierre
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
const headerBookable = (
|
|
|
|
<>
|
|
|
|
<p> {' '}
|
2022-08-01 07:51:54 +00:00
|
|
|
<FontAwesomeIcon icon={faClipboardCheck} style={{ color: "#D7A86E" }} />{' '}
|
2022-08-01 07:23:33 +00:00
|
|
|
Reservación
|
2022-08-01 06:31:52 +00:00
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
2022-08-01 07:23:33 +00:00
|
|
|
const headerStatus = (
|
|
|
|
<>
|
|
|
|
<p> {' '}
|
2022-08-01 07:51:54 +00:00
|
|
|
<FontAwesomeIcon icon={faCircleQuestion} style={{ color: "#C08135" }} />{' '}
|
2022-08-01 07:23:33 +00:00
|
|
|
Estado
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const bookableBodyTemplate = (rowData) => {
|
2022-08-01 07:51:54 +00:00
|
|
|
let class_color = '';
|
|
|
|
if(rowData.bookable == '1') {
|
|
|
|
class_color = '0';
|
|
|
|
} else {
|
|
|
|
class_color = '1';
|
|
|
|
|
|
|
|
}
|
2022-08-01 07:23:33 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<span
|
2022-08-01 07:51:54 +00:00
|
|
|
className={`status status-${class_color}`}
|
2022-08-01 07:23:33 +00:00
|
|
|
>
|
|
|
|
{rowData.bookable_text}
|
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-08-01 06:31:52 +00:00
|
|
|
|
2022-08-01 07:01:01 +00:00
|
|
|
const statusBodyTemplate = (rowData) => {
|
|
|
|
return (
|
2022-08-01 07:23:33 +00:00
|
|
|
<>
|
|
|
|
<span
|
|
|
|
className={`status status-${rowData.status}`}
|
|
|
|
>
|
|
|
|
{rowData.status_text}
|
|
|
|
</span>
|
|
|
|
</>
|
2022-08-01 07:01:01 +00:00
|
|
|
);
|
2022-08-01 07:23:33 +00:00
|
|
|
};
|
2022-08-01 06:31:52 +00:00
|
|
|
|
|
|
|
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={commonAreaList} dataKey="_id" paginator rows={5} selection={selectedCommonAreas} onSelectionChange={(e) => setSelectedCommonAreas(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"
|
2022-08-01 07:02:48 +00:00
|
|
|
currentPageReportTemplate="Mostrando {first} a {last} de {totalRecords} áreas comunes"
|
|
|
|
globalFilter={globalFilter} emptyMessage="No hay áreas comunes registrados.">
|
2022-08-01 06:31:52 +00:00
|
|
|
<Column selectionMode="multiple" headerStyle={{ width: '3rem' }}></Column>
|
|
|
|
<Column field="name" sortable header={headerName} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
|
2022-08-01 07:23:33 +00:00
|
|
|
<Column field="hourMin" header={headerHourMin} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }} alignFrozen="left"></Column>
|
|
|
|
<Column field="hourMax" header={headerHourMax} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}> </Column>
|
2022-08-01 07:51:54 +00:00
|
|
|
<Column field="bookable" sortable header={headerBookable} body={bookableBodyTemplate} style={{ flexGrow: 1, flexBasis: '200px', minWidth: '200px', wordBreak: 'break-word' }}></Column>
|
2022-08-01 07:23:33 +00:00
|
|
|
<Column field="status" sortable header={headerStatus} body={statusBodyTemplate} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
|
2022-08-01 06:31:52 +00:00
|
|
|
<Column style={{ flexGrow: 1, flexBasis: '130px', minWidth: '130px' }} body={actionsCommonArea}></Column>
|
|
|
|
</DataTable>
|
|
|
|
<Dialog visible={deleteCommonAreaDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteCommonAreaDialogFooter} onHide={hideDeleteCommonAreaDialog}>
|
|
|
|
<div className="flex align-items-center justify-content-center">
|
|
|
|
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
|
|
|
|
{commonArea && <span>¿Estás seguro que desea eliminar a <b>{commonArea.name}</b>?</span>}
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
<Dialog visible={deleteCommonAreasDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteCommonAreasDialogFooter} onHide={hideDeleteCommonAreasDialog}>
|
|
|
|
<div className="flex align-items-center justify-content-center">
|
|
|
|
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
|
|
|
|
{selectedCommonAreas && <span>¿Está seguro eliminar las áreas comunes seleccionadas?</span>}
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2022-08-01 05:49:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default React.memo(AreasComunes);
|