#13 Show error alert on invalid response

This commit is contained in:
2023-03-19 02:17:36 -04:00
parent 6e7e3e66f5
commit 8535998d0e

View File

@@ -1,29 +1,39 @@
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 { makeRequest } from "../utils.ts";
const LoginPage = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState(null);
const sendLoginRequest = async () => {
// await makeRequest({
// url: "http://localhost:5000/login",
// method: "POST",
// body: { username, password },
// })
// .then((resp) => resp.json())
// .then((data) => {
// console.log(data);
// });
const sendLoginRequest = async (e) => {
e.preventDefault();
await makeRequest({
url: "http://localhost:5000/login",
method: "POST",
body: { username, password },
})
.then((resp) => resp.json())
.then((data) => {
if (data.error) {
setError(data);
return;
}
console.log(data);
window.location.href = "/";
});
};
return (
<React.Fragment>
<MyNavbar />
{ error && (
<Alert variant="danger" className="m-4 mx-auto w-25">ERROR! {error.message}</Alert>
)}
<Container className="p-5">
<Form>
<Form onSubmit={sendLoginRequest}>
<Form.Group as={Row} className="mb-3" controlId="username">
<Form.Label column sm={2} className="me-2">
Username