diff --git a/servicio-comunidad-viviendas/src/communities/communities.controller.ts b/servicio-comunidad-viviendas/src/communities/communities.controller.ts index 159c67fa..a4e29859 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.controller.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.controller.ts @@ -25,7 +25,7 @@ export class CommunitiesController { @MessagePattern({cmd: 'findCommunityName'}) findOneName(@Payload() id: string) { - let _id = id['_id']; + let _id = id['id']; return this.communitiesService.findOneName(_id); } diff --git a/servicio-comunidad-viviendas/src/communities/communities.service.ts b/servicio-comunidad-viviendas/src/communities/communities.service.ts index 1b9b6bdf..0ef3e060 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.service.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.service.ts @@ -47,7 +47,7 @@ export class CommunitiesService { return this.communityModel.findOne({ _id: id }).exec(); } findOneName(id: string): Promise { - return this.communityModel.findOne({ _id: "62be68215692582bbfd77134" }).exec(); + return this.communityModel.findOne({ _id: id }).exec(); } update(id: string, community: CommunityDocument) { diff --git a/web-ui/web-react/src/components/AdministradoresComunidad.js b/web-ui/web-react/src/components/AdministradoresComunidad.js index 22eb4cc8..cf498010 100644 --- a/web-ui/web-react/src/components/AdministradoresComunidad.js +++ b/web-ui/web-react/src/components/AdministradoresComunidad.js @@ -1,51 +1,293 @@ -import React, { useEffect, useState } from 'react'; +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'; + const AdministradoresComunidad = () => { - const [listaAdmins,setListaAdmins]=useState([]); - const [listaAdminComunidad,setListaAdminComunidad]=useState([]); + 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 toast = useRef(null); + const dt = useRef(null); - async function listaAdmin(){ - let nombres=await fetch('http://localhost:4000/user/findAdminComunidad/', {method:'GET'}); - let nombresRes= await nombres.json(); - setListaAdmins(nombresRes.message); - } + let emptyAdminCommunity = { + _id: null, + dni: '', + name: '', + last_name: '', + email: '', + phone: '', + password: '', + community_id: '', + community_name: '', + user_type: '2', + status: '' + }; - async function listaComunidades(nombre){ - let nombres=await fetch('http://localhost:4000/community/findCommunityName/'+nombre, {method:'GET'}); - let nombresRes= await nombres.json(); - setListaAdminComunidad(nombresRes.message); - } - listaAdmins.map(function(administrador){ - listaComunidades(administrador.community_id); - administrador.community_id=listaAdminComunidad.name; - }) - useEffect(()=>{ + 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 => { + console.log(data.message.name); + item.community_name = data.message.name + return item + }) + })); + }).then(data => setListaAdmins(data) + ); + + } + + async function nombreComunidad(id) { + let nombres = await fetch('http://localhost:4000/community/findCommunityName/' + id, { method: 'GET' }); + let nombresRes = await nombres.json(); + return await nombresRes.message['name']; + } + + + async function setNameCommunities() { + Promise.all(listaAdmins.map(async function (administrador) { + // await listaComunidades(administrador.community_id); + administrador.community_id = await listaAdminComunidad.name; + })) + } + + useEffect(() => { listaAdmin(); - },[]) + + }, []) + + + 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 Eliminado', life: 3000 }); + } + + + + const hideDeleteAdminCommunityDialog = () => { + setDeleteAdminCommunityDialog(false); + } + + const hideDeleteAdminsCommunitysDialog = () => { + setDeleteAdminsCommunitiesDialog(false); + } + + const confirmDeleteAdminCommunity = (adminCommunity) => { + setAdminCommunity(adminCommunity); + setDeleteAdminCommunityDialog(true); + } + + const confirmDeleteSelected = () => { + setDeleteAdminsCommunitiesDialog(true); + } + + const actionsAdminCommunity = (rowData) => { + return ( +
+
+ ); + } + + const leftToolbarTemplate = () => { + return ( + +
+
+
+ ) + } + + const rightToolbarTemplate = () => { + return ( + +