From 3dce9337216bc0890f047ae95364a35ae9ed365f Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Thu, 29 Aug 2024 03:30:07 +0000 Subject: [PATCH] Handle NetworkErrors when submitting --- src/App.tsx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 989c4ce..19c0539 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -33,7 +33,7 @@ function App() { } const showThenHide = async () => { - const response = await fetch("http://localhost:8080/post", { + await fetch("http://localhost:8080/post", { method: "POST", body: JSON.stringify({ rating: rating, @@ -41,19 +41,23 @@ function App() { title: fields[1].dynamicState[0], content: fields[2].dynamicState[0] }) - }).then((response) => response.json()); + }) + .then(async ({ json }) => { + const result = await json(); - response.error ? changeAlertText(`${response.error.type}: ${response.error.message}`) : changeAlertText(`Success: ${response.message}`); + changeAlertText(result.error ? `${result.error.type}: ${result.error.message}` : `Success: ${result.message}`); + }) + .catch((err) => changeAlertText(err.toString())); - if (showAlert) { - return; - } + if (showAlert) { + return; + } - changeAlert(true); + changeAlert(true); - setTimeout(() => { - changeAlert(false); - }, 3000); + setTimeout(() => { + changeAlert(false); + }, 2500); } const buttons: ActionProps[] = [