import React, { useEffect, useState, useRef } from 'react'; import { InputText } from 'primereact/inputtext'; import { Button } from 'primereact/button'; import { Dropdown } from 'primereact/dropdown'; 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 { faClock, faHome, faHomeAlt, faUserAlt } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; import { faAt } from '@fortawesome/free-solid-svg-icons'; import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons'; import { faClipboardCheck } from '@fortawesome/free-solid-svg-icons'; import classNames from 'classnames'; import { useCookies } from "react-cookie"; import { RadioButton } from 'primereact/radiobutton'; const AreasComunes = () => { let emptyCommonArea = { _id: null, name: '', hourMin: '00:00', hourMax: '01:00', community_id: '', bookable: '1', bookable_text: '', status: '1', status_text: '', }; const [commonAreaList, setCommonAreaList] = useState([]); const [commonArea, setCommonArea] = useState(emptyCommonArea); const [selectedCommonAreas, setSelectedCommonAreas] = useState(null); const [globalFilter, setGlobalFilter] = useState(null); const [deleteCommonAreaDialog, setDeleteCommonAreaDialog] = useState(false); const [deleteCommonAreasDialog, setDeleteCommonAreasDialog] = useState(false); const [submitted, setSubmitted] = useState(false); const toast = useRef(null); const dt = useRef(null); const [cookies, setCookie] = useCookies(); const [changeStatusAreaDialog, setChangeStatusAreaDialog] = useState(false); const [areaDialog, setAreaDialog] = useState(false); const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar") async function getCommonAreas() { await fetch(`http://localhost:4000/commonArea/findByCommunity/${cookies.community_id}`, { method: 'GET' }) .then((response) => response.json()) .then(data => data.message) .then(data => { if (data) { data.map(item => { if (item.bookable == '1') { item.bookable_text = 'Necesaria'; } else { item.bookable_text = 'No es necesaria'; } if (item.status == '1') { item.status_text = 'Activo'; } else if (item.status == '0') { item.status_text = 'Inactivo'; } else { item.status_text = 'Eliminado'; } }) } data = data.filter( (val) => val.status != -1, ) setCommonAreaList(data); }); } useEffect(() => { getCommonAreas(); }, []); const findIndexById = (id) => { let index = -1; for (let i = 0; i < commonAreaList.length; i++) { if (commonAreaList[i]._id === id) { index = i; break; } } return index; } const saveCommonArea = () => { let _common_areas = [...commonAreaList]; let _common_area = { ...commonArea }; _common_area.community_id = cookies.community_id; if ( commonArea.name && commonArea.hourMin < commonArea.hourMax ) { if (commonArea._id) { fetch('http://localhost:4000/commonArea/updateCommonArea', { cache: 'no-cache', method: 'POST', body: JSON.stringify(_common_area), 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 (data) { return data.message; }) .then((data) => { const index = findIndexById(commonArea._id); if (data) { if (data.bookable == '1') { data.bookable_text = 'Necesaria'; } else { data.bookable_text = 'No es necesaria'; } if (data.status == '1') { data.status_text = 'Activo'; } else if (data.status == '0') { data.status_text = 'Inactivo'; } else { data.status_text = 'Eliminado'; } } _common_areas[index] = _common_area; toast.current.show({ severity: 'success', summary: 'Actualización exitosa', detail: 'Área Común Actualizada', life: 3000, }); setCommonAreaList(_common_areas); setAreaDialog(false); setCommonArea(emptyCommonArea); }) .catch((err) => { console.log('Ocurrió un error con el fetch', err); toast.current.show({ severity: 'danger', summary: 'Error', detail: 'No se pudo registrar el área común', life: 3000 }); }); } else { // console.log(houses) fetch('http://localhost:4000/commonArea/createCommonArea', { cache: 'no-cache', method: 'POST', body: JSON.stringify(_common_area), headers: { 'Content-Type': 'application/json', }, }) .then(function (response) { if (response.status != 201 && response.status != 200) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); }) .then(function (data) { return data.message; }) .then((data) => { if (data) { if (data.bookable == '1') { data.bookable_text = 'Necesaria'; } else { data.bookable_text = 'No es necesaria'; } if (data.status == '1') { data.status_text = 'Activo'; } else if (data.status == '0') { data.status_text = 'Inactivo'; } else { data.status_text = 'Eliminado'; } } _common_areas.push(_common_area); toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Área Común Registrado', life: 3000, }); setCommonAreaList(_common_areas); setAreaDialog(false); setCommonArea(emptyCommonArea); }) .catch((err) => { console.log('Ocurrió un error con el fetch', err); toast.current.show({ severity: 'danger', summary: 'Error', detail: 'No se pudo registrar el área común', life: 3000 }); }); } } else { setSubmitted(true); } }; const deleteCommonArea = () => { 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, ); _common_areas = _common_areas.filter( (val) => val.status != -1, ) 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 }); } ); }; const deleteSelectedCommonAreas = () => { let _common_areas = commonAreaList.filter( (val) => !selectedCommonAreas.includes(val), ); 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, ) setCommonAreaList(_common_areas); setDeleteCommonAreasDialog(false); setSelectedCommonAreas(null); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Áreas Comúnes Eliminadas', life: 3000, }); }; const cambiarStatuscommonArea = () => { if (commonArea.status == '1') { commonArea.status = '0'; commonArea.status_text = 'Inactivo'; } else if (commonArea.status == '0') { commonArea.status = '1'; commonArea.status_text = 'Activo'; } var data = { id: commonArea._id, status: commonArea.status, }; fetch('http://localhost:4000/commonArea/changeStatus', { cache: 'no-cache', method: 'POST', body: JSON.stringify(data), 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) { setChangeStatusAreaDialog(false); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Área Común Actualizada', life: 3000, }); } ) .catch( err => console.log('Ocurrió un error con el fetch', err) ); } const hideDeleteCommonAreaDialog = () => { setDeleteCommonAreaDialog(false); } const hideDeleteCommonAreasDialog = () => { setDeleteCommonAreasDialog(false); } const confirmDeleteCommonArea = (commonArea) => { setCommonArea(commonArea); setDeleteCommonAreaDialog(true); } const confirmDeleteSelected = () => { setDeleteCommonAreasDialog(true); }; const hideChangeStatusAreaDialog = () => { setChangeStatusAreaDialog(false); }; const confirmChangeStatusArea = (commonArea) => { setCommonArea(commonArea); setChangeStatusAreaDialog(true); }; const hideAreaDialog = () => { setSubmitted(false); setAreaDialog(false); setCommonArea(emptyCommonArea); setSaveButtonTitle('Registrar'); }; const openNewArea = () => { setCommonArea(emptyCommonArea); setAreaDialog(true); setSubmitted(false); }; const editArea = (area) => { setCommonArea({ ...area }); setAreaDialog(true); setSaveButtonTitle('Actualizar'); }; const actionsCommonArea = (rowData) => { let icono = ''; let text = ''; if (rowData.status == '0') { icono = "pi pi-eye"; text = "Activar Área Común" } else if (rowData.status == '1') { icono = "pi pi-eye-slash"; text = "Inactivar Área Común" } return (
); }; const onBookableChange = (e) => { let _commonArea = { ...commonArea }; _commonArea['bookable'] = e.value; setCommonArea(_commonArea); }; const onInputChange = (e, name) => { const val = (e.target && e.target.value) || ''; let _commonArea = { ...commonArea }; _commonArea[`${name}`] = val; setCommonArea(_commonArea); }; const onTimeChange = (e, name) => { e.target.value.split(':')[1] = "00"; const val = (e.target && e.target.value.split(':')[0]) || ''; let _area = { ...commonArea }; document.getElementById(`${name}`).value = val + ":00"; _area[`${name}`] = val + ":00"; setCommonArea(_area); }; const areaDialogFooter = ( <>