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 { faCircleQuestion } 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', status_text: '', }; 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); const [changeStatusAdminCommunityDialog, setChangeStatusAdminCommunityDialog] = useState(false); const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar"); const [adminDialog, setAdminDialog] = useState(false); async function listaAdmin() { await fetch('http://localhost:4000/user/findAdminComunidad/', { method: 'GET' }) .then((response) => response.json()) .then((data) => { return Promise.all(data.message.map(item => { if (item.status == '1') { item.status_text = 'Activo'; } else if (item.status == '0') { item.status_text = 'Inactivo'; } else { item.status_text = 'Eliminado'; } //item.full_name returns the repositorie name if (item.community_id || item.community_id != '') { 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 }) } else { item.community_name = "Sin Comunidad Asignada"; return item; } })); }) .then(data => { data = data.filter( (val) => val.status != -1, ); console.log(data) setListaAdmins(data); }); } async function getCommunites() { let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }) .then((response) => response.json()) .then(data => data.message) .then(data => { data = data.filter( (val) => val.status != -1, ) setCommunitiesList(data); }) } useEffect(() => { listaAdmin(); }, []) useEffect(() => { getCommunites(); }, []) const cList = communitiesList.map((item) => ({ label: item.name, value: item._id, })) const deleteAdminCommunity = () => { fetch('http://localhost:4000/user/deleteAdminCommunity/' + adminCommunity._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 _administrators = listaAdmins.filter( (val) => val._id !== adminCommunity._id, ); setListaAdmins(_administrators); setDeleteAdminCommunityDialog(false); setAdminCommunity(emptyAdminCommunity); toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador Comunidad Eliminada', life: 3000 }); } ) .catch( err => { console.log('Ocurrió un error con el fetch', err) toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Administrador Comunidad no se pudo eliminar', life: 3000 }); } ); }; const deleteSelectedAdminsCommunity = () => { let _admins = listaAdmins.filter( (val) => !selectedAdminsCommunities.includes(val), ); selectedAdminsCommunities.map((item) => { fetch('http://localhost:4000/user/deleteAdminCommunity/' + 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 cambiarStatusAdminCommuniy = () => { if (adminCommunity.status == '1') { adminCommunity.status = '0'; adminCommunity.status_text = 'Inactivo'; } else if (adminCommunity.status == '0') { adminCommunity.status = '1'; adminCommunity.status_text = 'Activo'; } var data = { id: adminCommunity._id, status: adminCommunity.status, }; fetch('http://localhost:4000/user/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) { setChangeStatusAdminCommunityDialog(false); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Administrador de Comunidad Actualizado', life: 3000, }); } ) .catch( err => console.log('Ocurrió un error con el fetch', err) ); } const findIndexById = (id) => { let index = -1; for (let i = 0; i < listaAdmins.length; i++) { if (listaAdmins[i]._id === id) { index = i; break; } } return index; } const findRepeated = (name, value) => { let _administrators = [...listaAdmins]; let value_filtered = _administrators.filter(item => item[`${name}`] === value); return value_filtered.length } const saveAdminCommunity = () => { let _administrators = [...listaAdmins]; let _admin = { ...adminCommunity }; _admin.community_id = communityId; if (adminCommunity._id === null) { if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) { fetch('http://localhost:4000/user/createAdminCommunity', { cache: 'no-cache', method: 'POST', body: JSON.stringify(_admin), 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(_admin); toast.current.show({ severity: 'success', summary: 'Exito', 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); } } else { console.log(`Actualizando admnistrador de comunidad: ${_admin}`) _admin.community_id = communityId; console.log(`Actualizando admnistrador de comunidad: ${_admin}`) fetch(`http://localhost:4000/user/updateAdminCommunity/${_admin._id}`, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(_admin), headers: { 'Content-Type': 'application/json', }, }).then((response) => { if (response.status !== 200) console.log(`Hubo un error en el servicio: ${response.status}`) else return response.json() }).then(() => { toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Administrador de comunidad actualizado', life: 3000, }) listaAdmin(); setCommunityId(''); setAdminCommunity(emptyAdminCommunity); }) } } const hideDeleteAdminCommunityDialog = () => { setDeleteAdminCommunityDialog(false); } const hideDeleteAdminsCommunitysDialog = () => { setDeleteAdminsCommunitiesDialog(false); } const confirmDeleteAdminCommunity = (adminCommunity) => { setAdminCommunity(adminCommunity); setDeleteAdminCommunityDialog(true); } const confirmDeleteSelected = () => { setDeleteAdminsCommunitiesDialog(true); }; const hideChangeStatusAdmimCommunityDialog = () => { setChangeStatusAdminCommunityDialog(false); }; const confirmChangeStatuAdminCommunity = (adminCommunity) => { setAdminCommunity(adminCommunity); setChangeStatusAdminCommunityDialog(true); }; const editAdmin = (admin) => { setAdminCommunity(admin); setSaveButtonTitle('Actualizar'); setCommunityId(admin.community_id) setAdminDialog(true) } const cancelEdit = () => { setAdminCommunity(emptyAdminCommunity); setSaveButtonTitle('Registrar'); setCommunityId(''); } const openNewAdmin = () => { setAdminCommunity(emptyAdminCommunity); setAdminDialog(true) setSubmitted(false); }; const hideAdminDialog = () => { setSubmitted(false); setAdminDialog(false) setAdminCommunity(emptyAdminCommunity); setSaveButtonTitle('Registrar'); }; const actionsAdminCommunity = (rowData) => { let icono = ''; let text = ''; if (rowData.status == '0') { icono = "pi pi-eye"; text = "Activar Administrador de Comunidad" } else if (rowData.status == '1') { icono = "pi pi-eye-slash"; text = "Inactivar Administrador de Comunidad" } return (
); }; const deleteAdminCommunityDialogFooter = ( <>