Ignore:
Timestamp:
09/08/22 12:38:24 (22 months ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Children:
ee05663
Parents:
717ceae
Message:

Added Order Functionality

Location:
frontend/src/screens
Files:
7 added
1 edited

Legend:

Unmodified
Added
Removed
  • frontend/src/screens/SigninScreen.js

    r717ceae r16237c4  
     1import Axios from "axios";
    12import Container from "react-bootstrap/Container";
    23import Form from "react-bootstrap/Form";
     
    45import Button from "react-bootstrap/Button";
    56import "../styles/SigninScreen.css";
    6 import React from "react";
    7 import { Link, useLocation } from "react-router-dom";
     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";
    812
    913function SigninScreen() {
     14  const navigate = useNavigate();
    1015  const { search } = useLocation();
    1116  const redirectInUrl = new URLSearchParams(search).get("redirect");
    1217  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
    1346  return (
    1447    <div className="pageContainer">
     
    1851        </Helmet>
    1952        <h1>Најави се</h1>
    20         <Form className="formCointainer">
     53        <Form className="formCointainer" onSubmit={submitHandler}>
    2154          <Form.Group controlId="email">
    2255            <Form.Label>Email</Form.Label>
    23             <Form.Control type="email" required />
     56            <Form.Control
     57              type="email"
     58              required
     59              onChange={(e) => setEmail(e.target.value)}
     60            />
    2461          </Form.Group>
    2562          <Form.Group controlId="password">
    2663            <Form.Label>Лозинка</Form.Label>
    27             <Form.Control type="email" required />
     64            <Form.Control
     65              type="password"
     66              required
     67              onChange={(e) => setPassword(e.target.value)}
     68            />
    2869          </Form.Group>
    2970          <div className="submitBtnContainer">
Note: See TracChangeset for help on using the changeset viewer.