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 { 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 { Dropdown } from 'primereact/dropdown'; import classNames from 'classnames'; const AdministradoresComunidad = () => { let emptyAdminCommunity = { _id: null, dni: '', name: '', last_name: '', email: '', phone: '', password: '', confirmPassword: '', community_id: '', community_name: '', user_type: '2', date_entry: new Date(), status: '1' }; const [listaAdmins, setListaAdmins] = useState([]); const [listaAdminComunidad, setListaAdminComunidad] = useState([]); const [adminCommunity, setAdminCommunity] = useState(emptyAdminCommunity); const [selectedAdminsCommunities, setSelectedAdminsCommunities] = useState(null); const [globalFilter, setGlobalFilter] = useState(null); const [deleteAdminCommunityDialog, setDeleteAdminCommunityDialog] = useState(false); const [deleteAdminsCommunitiesDialog, setDeleteAdminsCommunitiesDialog] = useState(false); const [communitiesList, setCommunitiesList] = useState([]); const [communityId, setCommunityId] = useState(null); const [submitted, setSubmitted] = useState(false); const toast = useRef(null); const dt = useRef(null); async function listaAdmin() { let nombres = await fetch('http://localhost:4000/user/findAdminComunidad/', { method: 'GET' }) .then((response) => response.json()) .then((data) => { return Promise.all(data.message.map(item => { //item.full_name returns the repositorie name return fetch(`http://localhost:4000/community/findCommunityName/${item.community_id}`, { method: 'GET' }) .then((response2) => response2.json()) .then(data => data.message) .then(data => { item.community_name = data['name'] return item }) })); }) .then(data => setListaAdmins(data)); } async function getCommunites() { let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }); let resList = await response.json(); let list = await resList.message; console.log(list); setCommunitiesList(await list); } useEffect(() => { listaAdmin(); }, []) useEffect(() => { getCommunites(); },[]) const cList = communitiesList.map((item) => ({ label: item.name, value: item._id, })) const deleteAdminCommunity = () => { /* 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 _administrators = listaAdmins.filter( (val) => val._id !== adminCommunity._id, ); setListaAdmins(_administrators); setDeleteAdminCommunityDialog(false); setAdminCommunity(emptyAdminCommunity); toast.current.show({ severity: 'success', summary: 'Administrador de Comunidad Eliminada', life: 3000, }); }; const deleteSelectedAdminsCommunity = () => { let _admins = listaAdmins.filter( (val) => !selectedAdminsCommunities.includes(val), ); /* selectedCommunities.map((item) => { fetch('http://localhost:4000/user/deleteCommunity/' + item._id, { cache: 'no-cache', method: 'DELETE', headers: { 'Content-Type': 'application/json' } }) })*/ setListaAdmins(_admins); setDeleteAdminsCommunitiesDialog(false); setSelectedAdminsCommunities(null); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Administradores de Comunidad de Viviendas Eliminados', life: 3000, }); }; const saveAdminCommunity = () => { if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) { let _administrators = [...listaAdmins]; let _adminCommunity = { ...adminCommunity }; _adminCommunity.community_id = communityId; console.log(_adminCommunity) console.log(communityId) fetch('http://localhost:4000/user/createAdminCommunity', { cache: 'no-cache', method: 'POST', body: JSON.stringify(_adminCommunity), 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(() => { // _adminCommunity.community_id = communitiesList.find(c => c._id === _adminCommunity.community_id).name _administrators.push(_adminCommunity); toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Administrador de Comunidad de vivienda Creada', life: 3000 }); setListaAdmins(_administrators); setAdminCommunity(emptyAdminCommunity); }) .catch( err => console.log('Ocurrió un error con el fetch', err) ); } else { setSubmitted(true); } } const hideDeleteAdminCommunityDialog = () => { setDeleteAdminCommunityDialog(false); } const hideDeleteAdminsCommunitysDialog = () => { setDeleteAdminsCommunitiesDialog(false); } const confirmDeleteAdminCommunity = (adminCommunity) => { setAdminCommunity(adminCommunity); setDeleteAdminCommunityDialog(true); } const confirmDeleteSelected = () => { setDeleteAdminsCommunitiesDialog(true); }; const actionsAdminCommunity = (rowData) => { return (
{' '}
{' '}
{' '}
{' '}
{' '}
{' '}