diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index d9b740b9..e8e98459 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -353,6 +353,33 @@ export class AppController { ); } + @Put('community/updateCommunity/:id') + updateCommunity( + @Param('id') id: string, + @Body('name') name: string, + @Body('province') province: string, + @Body('canton') canton: string, + @Body('district') district: string, + @Body('num_houses') num_houses: number, + @Body('phone') phone: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + @Body('houses') houses: [], + ) { + return this.appService.updateCommunity( + id, + name, + province, + canton, + district, + num_houses, + phone, + status, + date_entry, + houses, + ); + } + @Get('community/allCommunities') allcommunities() { return this.appService.allCommunities(); diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 235090ec..f8a24bbe 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -426,6 +426,25 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } + updateCommunity(id: string, name: string, province: string, canton: string, district: string, num_houses: number, phone: string, status: string, date_entry: Date, houses: unknown) { + const pattern = { cmd: 'updateCommunity' }; + const payload = { + id: id, + name: name, + province: province, + canton: canton, + district: district, + num_houses: num_houses, + phone: phone, + status: status, + date_entry: date_entry, + houses: houses, + }; + return this.clientCommunityApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + allCommunities() { const pattern = { cmd: 'findAllCommunities' }; const payload = {}; diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index a401fd17..548388fb 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -16,7 +16,7 @@ import { faHashtag } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; const Communities = () => { - let emptyCommunity = { + const emptyCommunity = { _id: null, name: '', province: provinciaId, @@ -32,7 +32,6 @@ const Communities = () => { const [communitiesList, setCommunitiesList] = useState([]); const [community, setCommunity] = useState(emptyCommunity); - const [housesList, setHousesList] = useState([]); const [provincesList, setProvincesList] = useState([]); const [provinciaId, setProvinciaId] = useState(null); @@ -42,6 +41,7 @@ const Communities = () => { const [districtId, setDistrictId] = useState(null); const [codeHouses, setCodeHouses] = useState(''); const [submitted, setSubmitted] = useState(false); + const [saveButtonLabel, setSaveButtonLabel] = useState('Registrar'); const [selectedCommunities, setSelectedCommunities] = useState(null); const [globalFilter, setGlobalFilter] = useState(null); const [deleteCommunityDialog, setDeleteCommunityDialog] = useState(false); @@ -50,15 +50,11 @@ const Communities = () => { const toast = useRef(null); const dt = useRef(null); - - //para el perfil de la comunidad const [tenants, setTenants] = useState([]); const [communityDialog, setCommunityDialog] = useState(false); - - const p = provincesList.map((item) => ({ label: item.name, value: item.code, @@ -76,7 +72,6 @@ const Communities = () => { parent: item.parentCode, })); - async function getProvinces() { const response = await fetch('assets/demo/data/provincias.json', { method: 'GET', @@ -98,7 +93,7 @@ const Communities = () => { async function fillCantons() { const resJson = await getCantons(); - const cantones = await resJson.filter(function (i, n) { + const cantones = await resJson.filter((i, _n) => { return i.parentCode === provinciaId; }); setCantonsList(await cantones); @@ -113,13 +108,12 @@ const Communities = () => { async function fillDistricts() { const resJson = await getDistricts(); - const districts = await resJson.filter(function (i, n) { + const districts = await resJson.filter((i, _n) => { return i.parentCode === cantonId; }); setDistrictsList(await districts); } - useEffect(() => { fillProvinces(); }, []); @@ -132,7 +126,6 @@ const Communities = () => { fillDistricts(); }, [cantonId]); - const handleProvinces = (event) => { const getprovinciaId = event.target.value; setProvinciaId(getprovinciaId); @@ -162,9 +155,7 @@ const Communities = () => { let pList = await getProvinces(); let cList = await getCantons(); let dList = await getDistricts(); - let data = await resJson.message.filter( - (val) => val.status != -1, - ) + let data = await resJson.message.filter((val) => val.status != -1); await data.map((item) => { if (item.status == '1') { item.status_text = 'Activo'; @@ -188,21 +179,20 @@ const Communities = () => { }, []); async function tenantsList(id) { - await fetch(`http://localhost:4000/user/findTenants/${id}`, { method: 'GET' }) + await fetch(`http://localhost:4000/user/findTenants/${id}`, { + method: 'GET', + }) .then((response) => response.json()) - .then(data => data.message) - .then(data => { - data = data.filter( - (val) => val.status != -1, - ) - setTenants(data) + .then((data) => data.message) + .then((data) => { + data = data.filter((val) => val.status != -1); + setTenants(data); }); } useEffect(() => { tenantsList(community._id); - }, []) - + }, []); const saveCommunity = () => { if ( @@ -213,73 +203,91 @@ const Communities = () => { districtId && community.phone ) { - let _communities = [...communitiesList]; - let _community = { ...community }; - _community.province = provinciaId; - _community.canton = cantonId; - _community.district = districtId; + if (saveButtonLabel === 'Registrar') { + 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) + for (let i = 0; i < _community.num_houses; i++) { + _community.houses.push({ + number_house: codeHouses + (i + 1), + }); + } + fetch('http://localhost:4000/community/createCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((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(''); + getCommunites(); + setCommunity(emptyCommunity); + }) + .catch((err) => console.log('Ocurrió un error con el fetch', err)); + } else { + let _community = { ...community }; + _community.province = provinciaId; + _community.canton = cantonId; + _community.district = districtId; + console.log(`Actualizando comunidad: ${_community}`); + fetch(`http://localhost:4000/community/updateCommunity/${community._id}`, { + method: 'PUT', + cache: 'no-cache', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + } + }).then((response) => { + getCommunites(); + if (response.status != 200) 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)); + }).catch((err) => console.log('Ocurrió un error con el fetch', err)); + setSaveButtonLabel('Registrar'); + setCommunity(emptyCommunity); + } } else { setSubmitted(true); } }; - - function findNameTenant(tenant_id) { let name = ''; if (tenant_id == '') { name = 'Sin inquilino'; } else { - let tenant = tenants.find(t => t._id == tenant_id) + let tenant = tenants.find((t) => t._id == tenant_id); name = tenant['name'] + ' ' + tenant['last_name']; } return name; @@ -340,7 +348,6 @@ const Communities = () => { if (community.status == '1') { community.status = '0'; community.status_text = 'Inactivo'; - } else if (community.status == '0') { community.status = '1'; community.status_text = 'Activo'; @@ -354,69 +361,67 @@ const Communities = () => { method: 'POST', body: JSON.stringify(data), headers: { - 'Content-Type': 'application/json' - } + '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) { - setEditCommunityDialog(false); - toast.current.show({ - severity: 'success', - summary: 'Éxito', - detail: 'Comunidad de Viviendas Actualizada', - life: 3000, - }); - } - ) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); - } + .then((response) => { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then((_response) => { + setEditCommunityDialog(false); + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Comunidad de Viviendas Actualizada', + life: 3000, + }); + getCommunites(); + }) + .catch((err) => console.log('Ocurrió un error con el fetch', err)); + }; const deleteCommunity = () => { fetch('http://localhost:4000/community/deleteCommunity/' + community._id, { cache: 'no-cache', method: 'DELETE', headers: { - 'Content-Type': 'application/json' - } + '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 = communitiesList.filter(val => val._id !== community._id); - setCommunitiesList(_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 _communities = communitiesList.filter((val) => val._id !== community._id); - _communities = _communities.filter( - (val) => val.status != -1, - ) + .then((response) => { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then((_response) => { + let _community = communitiesList.filter( + (val) => val._id !== community._id, + ); + setCommunitiesList(_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 _communities = communitiesList.filter( + (val) => val._id !== community._id, + ); + _communities = _communities.filter((val) => val.status != -1); setCommunitiesList(_communities); setDeleteCommunityDialog(false); setCommunity(emptyCommunity); @@ -441,9 +446,7 @@ const Communities = () => { } }) })*/ - _communities = _communities.filter( - (val) => val.status != -1, - ) + _communities = _communities.filter((val) => val.status != -1); setCommunitiesList(_communities); setDeleteCommunitiesDialog(false); setSelectedCommunities(null); @@ -455,17 +458,37 @@ const Communities = () => { }); }; - const actionsCommunity = (rowData) => { + const updateCommunity = (community) => { + setCommunity(community); + setSaveButtonLabel('Actualizar'); + setHousesList(community.houses); + }; + const cancelEdit = () => { + setCommunity(emptyCommunity); + setCantonId(''); + setHousesList([]); + setProvinciaId(''); + setDistrictId(''); + setSaveButtonLabel('Registrar'); + } + + const actionsCommunity = (rowData) => { let icono = ''; if (rowData.status == '0') { - icono = "pi pi-eye"; + icono = 'pi pi-eye'; } else if (rowData.status == '1') { - icono = "pi pi-eye-slash"; + icono = 'pi pi-eye-slash'; } return (
+
@@ -1168,12 +1209,22 @@ const Communities = () => { )} -
+
+ /> + {saveButtonLabel === 'Actualizar' && ( +