#13 Show error alert on invalid response
This commit is contained in:
@@ -1,29 +1,39 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { Button, Col, Container, Form, Row } from "react-bootstrap";
|
import { Button, Col, Container, Form, Row, Alert} from "react-bootstrap";
|
||||||
import MyNavbar from "../components/MyNavbar";
|
import MyNavbar from "../components/MyNavbar";
|
||||||
import { makeRequest } from "../utils.ts";
|
import { makeRequest } from "../utils.ts";
|
||||||
|
|
||||||
const LoginPage = () => {
|
const LoginPage = () => {
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const sendLoginRequest = async () => {
|
const sendLoginRequest = async (e) => {
|
||||||
// await makeRequest({
|
e.preventDefault();
|
||||||
// url: "http://localhost:5000/login",
|
await makeRequest({
|
||||||
// method: "POST",
|
url: "http://localhost:5000/login",
|
||||||
// body: { username, password },
|
method: "POST",
|
||||||
// })
|
body: { username, password },
|
||||||
// .then((resp) => resp.json())
|
})
|
||||||
// .then((data) => {
|
.then((resp) => resp.json())
|
||||||
// console.log(data);
|
.then((data) => {
|
||||||
// });
|
if (data.error) {
|
||||||
|
setError(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(data);
|
||||||
|
window.location.href = "/";
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<MyNavbar />
|
<MyNavbar />
|
||||||
|
{ error && (
|
||||||
|
<Alert variant="danger" className="m-4 mx-auto w-25">ERROR! {error.message}</Alert>
|
||||||
|
)}
|
||||||
<Container className="p-5">
|
<Container className="p-5">
|
||||||
<Form>
|
<Form onSubmit={sendLoginRequest}>
|
||||||
<Form.Group as={Row} className="mb-3" controlId="username">
|
<Form.Group as={Row} className="mb-3" controlId="username">
|
||||||
<Form.Label column sm={2} className="me-2">
|
<Form.Label column sm={2} className="me-2">
|
||||||
Username
|
Username
|
||||||
|
|||||||
11
frontend/src/utils.ts
Normal file
11
frontend/src/utils.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const makeRequest = ({ url, method, body }): Promise<Response> => {
|
||||||
|
return fetch(url, {
|
||||||
|
method: method,
|
||||||
|
credentials: "include",
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
mode: "cors",
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { makeRequest };
|
||||||
Reference in New Issue
Block a user