import React, { useEffect, useState, useRef } from 'react'; import { InputText } from 'primereact/inputtext'; import { Button } from 'primereact/button'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Dropdown } from 'primereact/dropdown'; import { Toast } from 'primereact/toast'; import classNames from 'classnames'; import { Dialog } from 'primereact/dialog'; import { Toolbar } from 'primereact/toolbar'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faHome } from '@fortawesome/free-solid-svg-icons'; import { faMapLocationDot } from '@fortawesome/free-solid-svg-icons'; import { faPhoneAlt } from '@fortawesome/free-solid-svg-icons'; import { faEllipsis } from '@fortawesome/free-solid-svg-icons'; const Communities = () => { let emptyCommunity = { _id: null, name: '', province: provinciaId, canton: cantonId, district: districtId, phone: '', num_houses: 0, status: 'activo', date_entry: new Date(), houses: [], }; const [communitiesList, setCommunitiesList] = useState([]); const [community, setCommunity] = useState(emptyCommunity); const [housesList, setHousesList] = useState([]); const [provincesList, setProvincesList] = useState([]); const [provinciaId, setProvinciaId] = useState(null); const [cantonsList, setCantonsList] = useState([]); const [cantonId, setCantonId] = useState(null); const [districtsList, setDistrictsList] = useState([]); const [districtId, setDistrictId] = useState(null); const [codeHouses, setCodeHouses] = useState(''); const [submitted, setSubmitted] = useState(false); const [selectedCommunities, setSelectedCommunities] = useState(null); const [globalFilter, setGlobalFilter] = useState(null); const [deleteCommunityDialog, setDeleteCommunityDialog] = useState(false); const [deleteCommunitiesDialog, setDeleteCommunitiesDialog] = useState(false); const toast = useRef(null); const dt = useRef(null); const p = provincesList.map((item) => ({ label: item.name, value: item.code, })); const c = cantonsList.map((item) => ({ label: item.name, value: item.code, parent: item.parentCode, })); const d = districtsList.map((item) => ({ label: item.name, value: item.code, parent: item.parentCode, })); useEffect(() => { fillProvinces(); }, []); useEffect(() => { fillCantons(); }, [provinciaId]); useEffect(() => { fillDistricts(); }, [cantonId]); async function getProvinces() { const response = await fetch('assets/demo/data/provincias.json', { method: 'GET', }); return await response.json(); } async function fillProvinces() { const getP = await getProvinces(); setProvincesList(await getP); } async function getCantons() { const response = await fetch('assets/demo/data/cantones.json', { method: 'GET', }); return await response.json(); } async function fillCantons() { const resJson = await getCantons(); const cantones = await resJson.filter(function (i, n) { return i.parentCode === provinciaId; }); setCantonsList(await cantones); } async function getDistricts() { const response = await fetch('assets/demo/data/distritos.json', { method: 'GET', }); return await response.json(); } async function fillDistricts() { const resJson = await getDistricts(); const districts = await resJson.filter(function (i, n) { return i.parentCode === cantonId; }); setDistrictsList(await districts); } const handleProvinces = (event) => { const getprovinciaId = event.target.value; setProvinciaId(getprovinciaId); }; const handleCanton = (event) => { const getCantonId = event.target.value; setCantonId(getCantonId); }; const handleDistrict = (event) => { const getDistrictId = event.target.value; setDistrictId(getDistrictId); }; const handleCodeHouses = (event) => { const getcodehouse = event.target.value; setCodeHouses(getcodehouse); }; async function getCommunites() { let response = await fetch( 'http://localhost:4000/community/allCommunities', { method: 'GET' }, ); let resJson = await response.json(); let pList = await getProvinces(); let cList = await getCantons(); let dList = await getDistricts(); await resJson.message.map((item) => { item.province = pList.find((p) => p.code === item.province).name; item.canton = cList.find((p) => p.code === item.canton).name; item.district = dList.find((p) => p.code === item.district).name; if (!item.id_admin) { item.name_admin = 'Sin Administrador'; } }); setCommunitiesList(await resJson.message); } useEffect(() => { getCommunites(); }, []); const saveCommunity = () => { if ( community.name && community.num_houses > 0 && provinciaId && cantonId && districtId && community.phone ) { let _communities = [...communitiesList]; let _community = { ...community }; _community.province = provinciaId; _community.canton = cantonId; _community.district = districtId; for (let i = 0; i < _community.num_houses; i++) { _community.houses.push({ number_house: codeHouses + (i + 1), }); } // console.log(houses) fetch('http://localhost:4000/community/createCommunity', { cache: 'no-cache', method: 'POST', body: JSON.stringify(_community), 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(() => { _community.province = provincesList.find( (p) => p.code === _community.province, ).name; _community.canton = cantonsList.find( (p) => p.code === _community.canton, ).name; _community.district = districtsList.find( (p) => p.code === _community.district, ).name; _communities.push(_community); toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Comunidad de vivienda Creada', life: 3000, }); setCommunitiesList(_communities); setProvinciaId(''); setCantonId(''); setDistrictId(''); setCodeHouses(''); setCommunity(emptyCommunity); }) .catch((err) => console.log('Ocurrió un error con el fetch', err)); } else { setSubmitted(true); } }; const onInputChange = (e, name) => { const val = (e.target && e.target.value) || ''; let _community = { ...community }; _community[`${name}`] = val; setCommunity(_community); }; const hideDeleteCommunityDialog = () => { setDeleteCommunityDialog(false); }; const hideDeleteCommunitiesDialog = () => { setDeleteCommunitiesDialog(false); }; const confirmDeleteCommunity = (community) => { setCommunity(community); setDeleteCommunityDialog(true); }; const confirmDeleteSelected = () => { setDeleteCommunitiesDialog(true); }; const deleteCommunity = () => { /* fetch('http://localhost:4000/community/deleteCommunity/' + community._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 _community = communities.filter(val => val._id !== community._id); setCommunities(_community); setDeleteCommunityDialog(false); setCommunity(emptyCommunity); toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Comunidad de Viviendas Eliminada', life: 3000 }); } ) .catch( err => { console.log('Ocurrió un error con el fetch', err) toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Comunidad de Viviendas no se pudo eliminar', life: 3000 }); } ); */ let _community = communitiesList.filter((val) => val._id !== community._id); setCommunitiesList(_community); setDeleteCommunityDialog(false); setCommunity(emptyCommunity); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Comunidad de Viviendas Eliminada', life: 3000, }); }; const deleteSelectedCommunities = () => { let _communities = communitiesList.filter( (val) => !selectedCommunities.includes(val), ); /* selectedCommunities.map((item) => { fetch('http://localhost:4000/user/deleteCommunity/' + item._id, { cache: 'no-cache', method: 'DELETE', headers: { 'Content-Type': 'application/json' } }) })*/ setCommunitiesList(_communities); setDeleteCommunitiesDialog(false); setSelectedCommunities(null); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Comunidades de Viviendas Eliminadas', life: 3000, }); }; const actionsCommunity = (rowData) => { return (
); }; const leftToolbarTemplate = () => { return (
); }; const rightToolbarTemplate = () => { return ( ); }; export default React.memo(Communities);