diff --git a/web-ui/web-react/src/components/AreasComunes.js b/web-ui/web-react/src/components/AreasComunes.js index 7b5ae8bc..6da1739d 100644 --- a/web-ui/web-react/src/components/AreasComunes.js +++ b/web-ui/web-react/src/components/AreasComunes.js @@ -44,8 +44,7 @@ const AreasComunes = () => { 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) + 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', @@ -292,11 +362,12 @@ const AreasComunes = () => { setChangeStatusAreaDialog(true); }; - - const hideNewAreaDialog = () => { + const hideAreaDialog = () => { setSubmitted(false); setAreaDialog(false); setCommonArea(emptyCommonArea); + setSaveButtonTitle('Registrar'); + }; const openNewArea = () => { @@ -305,6 +376,12 @@ const AreasComunes = () => { setSubmitted(false); }; + const editArea = (area) => { + setCommonArea({ ...area }); + setAreaDialog(true); + setSaveButtonTitle('Actualizar'); + + }; const actionsCommonArea = (rowData) => { let icono = ''; @@ -319,15 +396,21 @@ const AreasComunes = () => { return (
+
@@ -347,10 +430,19 @@ 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 = ( <> - - - );