source: frontend/src/screens/SigninScreen.js@ 16237c4

Last change on this file since 16237c4 was 16237c4, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 22 months ago

Added Order Functionality

  • Property mode set to 100644
File size: 2.6 KB
Line 
1import Axios from "axios";
2import Container from "react-bootstrap/Container";
3import Form from "react-bootstrap/Form";
4import { Helmet } from "react-helmet-async";
5import Button from "react-bootstrap/Button";
6import "../styles/SigninScreen.css";
7import React, { useContext, useEffect, useState } from "react";
8import { Link, useLocation, useNavigate } from "react-router-dom";
9import { Store } from "../Store";
10import { toast } from "react-toastify";
11import { getError } from "../components/utils";
12
13function SigninScreen() {
14 const navigate = useNavigate();
15 const { search } = useLocation();
16 const redirectInUrl = new URLSearchParams(search).get("redirect");
17 const redirect = redirectInUrl ? redirectInUrl : "/";
18
19 const [email, setEmail] = useState("");
20 const [password, setPassword] = useState("");
21
22 const { state, dispatch: ctxDispatch } = useContext(Store);
23 const { userInfo } = state;
24
25 const submitHandler = async (e) => {
26 e.preventDefault();
27 try {
28 const { data } = await Axios.post("/api/users/signin", {
29 email,
30 password,
31 });
32 ctxDispatch({ type: "USER_SIGNIN", payload: data });
33 localStorage.setItem("userInfo", JSON.stringify(data));
34 navigate(redirect || "/");
35 } catch (err) {
36 toast.error(getError(err));
37 }
38 };
39
40 useEffect(() => {
41 if (userInfo) {
42 navigate(redirect);
43 }
44 }, [navigate, redirect, userInfo]);
45
46 return (
47 <div className="pageContainer">
48 <Container className="main">
49 <Helmet>
50 <title>Најави се</title>
51 </Helmet>
52 <h1>Најави се</h1>
53 <Form className="formCointainer" onSubmit={submitHandler}>
54 <Form.Group controlId="email">
55 <Form.Label>Email</Form.Label>
56 <Form.Control
57 type="email"
58 required
59 onChange={(e) => setEmail(e.target.value)}
60 />
61 </Form.Group>
62 <Form.Group controlId="password">
63 <Form.Label>Лозинка</Form.Label>
64 <Form.Control
65 type="password"
66 required
67 onChange={(e) => setPassword(e.target.value)}
68 />
69 </Form.Group>
70 <div className="submitBtnContainer">
71 <Button variant="danger" size="lg" type="submit">
72 Најави се
73 </Button>
74 </div>
75 <div className="registerParagraph">
76 Нов корисник?{" "}
77 <Link to={`/signup?redirect=${redirect}`}>Регистрирај се</Link>
78 </div>
79 </Form>
80 </Container>
81 </div>
82 );
83}
84
85export default SigninScreen;
Note: See TracBrowser for help on using the repository browser.