Show error if server returns an error
This commit is contained in:
parent
c4c3316e33
commit
b1675f3229
28
src/App.tsx
28
src/App.tsx
|
@ -9,6 +9,7 @@ function App() {
|
||||||
const [rating, setNewRating] = useState<number | null>(0);
|
const [rating, setNewRating] = useState<number | null>(0);
|
||||||
const [showAlert, changeAlert] = useState(false);
|
const [showAlert, changeAlert] = useState(false);
|
||||||
const [alertText, changeAlertText] = useState("");
|
const [alertText, changeAlertText] = useState("");
|
||||||
|
const [secure, setSecure] = useState(false);
|
||||||
|
|
||||||
const [showInfo, changeInfo] = useState(false);
|
const [showInfo, changeInfo] = useState(false);
|
||||||
const [infoText, changeInfoText] = useState("");
|
const [infoText, changeInfoText] = useState("");
|
||||||
|
@ -47,7 +48,7 @@ function App() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const showThenHide = async () => {
|
const showThenHide = async () => {
|
||||||
await fetch(endpoint ? `${endpoint}/post` : "http://localhost:8080/post", {
|
await fetch(endpoint ? `${secure ? "https" : "http"}://${endpoint}/post` : "http://localhost:8080/post", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
rating: rating,
|
rating: rating,
|
||||||
|
@ -59,17 +60,22 @@ function App() {
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
changeInfoText(
|
if (result.error) {
|
||||||
result.error
|
changeAlertText(`${result.error.type}: ${result.error.message}`);
|
||||||
? `${result.error.type}: ${result.error.message}`
|
changeAlert(true);
|
||||||
: `Success: ${result.message}`,
|
|
||||||
);
|
setTimeout(() => {
|
||||||
|
changeAlert(false);
|
||||||
|
}, 2000);
|
||||||
|
} else {
|
||||||
|
changeInfoText(`Success: ${result.message}`);
|
||||||
|
changeInfo(true);
|
||||||
|
|
||||||
changeInfo(true);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
changeInfo(false);
|
changeInfo(false);
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
changeAlertText(err.toString());
|
changeAlertText(err.toString());
|
||||||
|
@ -103,7 +109,7 @@ function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div id="settings">
|
<div id="settings">
|
||||||
<EndpointDialog endpoint={[endpoint, setEndpoint]} />
|
<EndpointDialog endpoint={[endpoint, setEndpoint]} secure={[secure, setSecure]}/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="app">
|
<div id="app">
|
||||||
|
|
Loading…
Reference in a new issue