diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 56c1252c..dbcd8b85 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -425,6 +425,23 @@ export class AppController { return this.appService.changeStatusCommonArea(pId, pStatus); } + @Post('commonArea/updateCommonArea') + updateCommonArea( + @Body('_id') id: string, + @Body('name') name: string, + @Body('hourMin') hourMin: string, + @Body('hourMax') hourMax: string, + @Body('bookable') bookable: number, + @Body('community_id') community_id: string, + ) { + return this.appService.updateCommonArea( + id, + name, + hourMin, + hourMax, + bookable, + community_id,); + } // #==== API GUEST //#API userService - create user @Post('guest/createGuest') diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 7677f6ba..56eb67e6 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -522,7 +522,27 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } - + updateCommonArea( + id: string, + name: string, + hourMin: string, + hourMax: string, + bookable: number, + community_id: string, + ) { + const pattern = { cmd: 'updateCommonArea' }; + const payload = { + id: id, + name: name, + hourMin: hourMin, + hourMax: hourMax, + bookable: bookable, + community_id: community_id, + }; + return this.clientCommonAreaApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } // ====================== GUESTS =============================== //POST parameter from API diff --git a/servicio-areas-comunes/src/common_areas/common_areas.service.ts b/servicio-areas-comunes/src/common_areas/common_areas.service.ts index 877bd011..b25a9d5f 100644 --- a/servicio-areas-comunes/src/common_areas/common_areas.service.ts +++ b/servicio-areas-comunes/src/common_areas/common_areas.service.ts @@ -26,6 +26,7 @@ export class CommonAreasService { } update(id: string, commonArea: CommonAreaDocument) { + console.log(commonArea); return this.commonAreaModel.findOneAndUpdate({ _id: id }, commonArea, { new: true, }); diff --git a/web-ui/web-react/src/components/AreasComunes.js b/web-ui/web-react/src/components/AreasComunes.js index 7b0b6fd8..b8c2b0f9 100644 --- a/web-ui/web-react/src/components/AreasComunes.js +++ b/web-ui/web-react/src/components/AreasComunes.js @@ -8,7 +8,7 @@ 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 { faClock, faHome, faHomeAlt, faUserAlt } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; import { faAt } from '@fortawesome/free-solid-svg-icons'; import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons'; @@ -41,11 +41,10 @@ const AreasComunes = () => { const [submitted, setSubmitted] = useState(false); const toast = useRef(null); const dt = useRef(null); - const [cookies, setCookie] = useCookies(); const [changeStatusAreaDialog, setChangeStatusAreaDialog] = useState(false); - - + const [areaDialog, setAreaDialog] = useState(false); + const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar") async function getCommonAreas() { await fetch(`http://localhost:4000/commonArea/findByCommunity/${cookies.community_id}`, { method: 'GET' }) @@ -57,7 +56,7 @@ const AreasComunes = () => { if (item.bookable == '1') { item.bookable_text = 'Necesaria'; } else { - item.bookable_text = 'No es necesarioa'; + item.bookable_text = 'No es necesaria'; } if (item.status == '1') { @@ -69,7 +68,6 @@ const AreasComunes = () => { } }) } - data = data.filter( (val) => val.status != -1, ) @@ -81,75 +79,147 @@ const AreasComunes = () => { getCommonAreas(); }, []); + const findIndexById = (id) => { + let index = -1; + for (let i = 0; i < commonAreaList.length; i++) { + if (commonAreaList[i]._id === id) { + index = i; + break; + } + } + return index; + } + const saveCommonArea = () => { + let _common_areas = [...commonAreaList]; + let _common_area = { ...commonArea }; + _common_area.community_id = cookies.community_id; if ( commonArea.name && commonArea.hourMin < commonArea.hourMax ) { - let _common_areas = [...commonAreaList]; - let _common_area = { ...commonArea }; - _common_area.community_id = cookies.community_id; + if (commonArea._id) { + fetch('http://localhost:4000/commonArea/updateCommonArea', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_common_area), + 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 (data) { + return data.message; + }) + .then((data) => { + const index = findIndexById(commonArea._id); - // console.log(houses) - fetch('http://localhost:4000/commonArea/createCommonArea', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(_common_area), - 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 (data) { - return data.message; - }) - .then((data) => { - if (data) { - if (data.bookable == '1') { - data.bookable_text = 'Necesaria'; - } else { - data.bookable_text = 'No es necesaria'; + if (data) { + if (data.bookable == '1') { + data.bookable_text = 'Necesaria'; + } else { + data.bookable_text = 'No es necesaria'; + } + + if (data.status == '1') { + data.status_text = 'Activo'; + } else if (data.status == '0') { + data.status_text = 'Inactivo'; + } else { + data.status_text = 'Eliminado'; + } } + _common_areas[index] = _common_area; - if (data.status == '1') { - data.status_text = 'Activo'; - } else if (data.status == '0') { - data.status_text = 'Inactivo'; - } else { - data.status_text = 'Eliminado'; - } - } - _common_areas.push(data); + toast.current.show({ + severity: 'success', + summary: 'Actualización exitosa', + detail: 'Área Común Actualizada', + life: 3000, + }); + setCommonAreaList(_common_areas); + setAreaDialog(false); + setCommonArea(emptyCommonArea); + }) + .catch((err) => { + console.log('Ocurrió un error con el fetch', err); + toast.current.show({ + severity: 'danger', + summary: 'Error', + detail: 'No se pudo registrar el área común', + life: 3000 + }); - toast.current.show({ - severity: 'success', - summary: 'Registro exitoso', - detail: 'Área Común Creada', - life: 3000, }); - setCommonAreaList(_common_areas); - setCommonArea(emptyCommonArea); + } else { + // console.log(houses) + fetch('http://localhost:4000/commonArea/createCommonArea', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_common_area), + headers: { + 'Content-Type': 'application/json', + }, }) - .catch((err) => { - console.log('Ocurrió un error con el fetch', err); - toast.current.show({ - severity: 'danger', - summary: 'Error', - detail: 'No se pudo registrar el área común', - life: 3000 - }); + .then(function (response) { + if (response.status != 201 && response.status != 200) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then(function (data) { + return data.message; + }) + .then((data) => { - }); + if (data) { + if (data.bookable == '1') { + data.bookable_text = 'Necesaria'; + } else { + data.bookable_text = 'No es necesaria'; + } + + if (data.status == '1') { + data.status_text = 'Activo'; + } else if (data.status == '0') { + data.status_text = 'Inactivo'; + } else { + data.status_text = 'Eliminado'; + } + } + _common_areas.push(_common_area); + + toast.current.show({ + severity: 'success', + summary: 'Registro exitoso', + detail: 'Área Común Registrado', + life: 3000, + }); + setCommonAreaList(_common_areas); + setAreaDialog(false); + + setCommonArea(emptyCommonArea); + + }) + .catch((err) => { + console.log('Ocurrió un error con el fetch', err); + toast.current.show({ + severity: 'danger', + summary: 'Error', + detail: 'No se pudo registrar el área común', + life: 3000 + }); + + }); + } } else { setSubmitted(true); } }; - const deleteCommonArea = () => { fetch('http://localhost:4000/commonArea/deleteCommonArea/' + commonArea._id, { cache: 'no-cache', @@ -283,8 +353,6 @@ const AreasComunes = () => { setDeleteCommonAreasDialog(true); }; - - const hideChangeStatusAreaDialog = () => { setChangeStatusAreaDialog(false); }; @@ -294,6 +362,27 @@ const AreasComunes = () => { setChangeStatusAreaDialog(true); }; + const hideAreaDialog = () => { + setSubmitted(false); + setAreaDialog(false); + setCommonArea(emptyCommonArea); + setSaveButtonTitle('Registrar'); + + }; + + const openNewArea = () => { + setCommonArea(emptyCommonArea); + setAreaDialog(true); + setSubmitted(false); + }; + + const editArea = (area) => { + setCommonArea({ ...area }); + setAreaDialog(true); + setSaveButtonTitle('Actualizar'); + + }; + const actionsCommonArea = (rowData) => { let icono = ''; let text = ''; @@ -307,15 +396,21 @@ const AreasComunes = () => { return (
+
@@ -335,6 +430,32 @@ const AreasComunes = () => { setCommonArea(_commonArea); }; + const onTimeChange = (e, name) => { + e.target.value.split(':')[1] = "00"; + const val = (e.target && e.target.value.split(':')[0]) || ''; + let _area = { ...commonArea }; + document.getElementById(`${name}`).value = val + ":00"; + _area[`${name}`] = val + ":00"; + setCommonArea(_area); + }; + + const areaDialogFooter = ( + <> + - - - );