From 0ed28237aac522013ffee35d657509102eff5a92 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Tue, 30 Aug 2022 05:37:53 -0600 Subject: [PATCH] add function to update post --- .../src/components/RegistroComunicado.js | 70 +++++++++++++------ 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/web-ui/web-react/src/components/RegistroComunicado.js b/web-ui/web-react/src/components/RegistroComunicado.js index b98a7c5f..cca51f78 100644 --- a/web-ui/web-react/src/components/RegistroComunicado.js +++ b/web-ui/web-react/src/components/RegistroComunicado.js @@ -49,31 +49,57 @@ const RegistroComunicado = () => { } const saveComunicado = () => { - var data = { - post: document.getElementById('txt_comunicado').value, - user_id: cookies.id, - community_id: cookies.community_id - }; + if (comunicado._id === null) { + var data = { + post: document.getElementById('txt_comunicado').value, + user_id: cookies.id, + community_id: cookies.community_id + }; - fetch('http://localhost:4000/post/createPost', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(data), - 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((_response) => { + fetch('http://localhost:4000/post/createPost', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(data), + 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((_response) => { - }).catch( - err => console.log('Ocurrió un error con el fetch', err) - ); + }).catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } else { + let data = { + _id: comunicado._id, + post: document.getElementById('txt_comunicado').value, + user_id: cookies.id, + community_id: cookies.community_id + }; + + fetch(`http://localhost:4000/post/updatePost/${comunicado._id}`, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(data), + 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((_response) => { + + }).catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } } - const header = (