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 { faHome, faUserAlt } from '@fortawesome/free-solid-svg-icons'; import { faPhoneAlt } from '@fortawesome/free-solid-svg-icons'; import { faAt } from '@fortawesome/free-solid-svg-icons'; import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons'; import { faEllipsis } from '@fortawesome/free-solid-svg-icons'; import { faHomeAlt } from '@fortawesome/free-solid-svg-icons'; import classNames from 'classnames'; import { useCookies } from "react-cookie"; const AreasComunes = () => { let emptyCommonArea = { _id: null, dni: '', name: '', hourMin: '', hourMax: '', 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(); 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 = 'Obligatoria'; } else { item.bookable_text = 'Libre'; } 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 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 hideDeleteCommonAreaDialog = () => { setDeleteCommonAreaDialog(false); } const hideDeleteCommonAreasDialog = () => { setDeleteCommonAreasDialog(false); } const confirmDeleteCommonArea = (commonArea) => { setCommonArea(commonArea); setDeleteCommonAreaDialog(true); } const confirmDeleteSelected = () => { setDeleteCommonAreasDialog(true); }; const actionsCommonArea = (rowData) => { return (
); }; const deleteCommonAreaDialogFooter = ( <>