1 | import React from 'react';
|
---|
2 | import { useNavigate } from 'react-router-dom';
|
---|
3 | import RebuMKService from "../../repository/rebuRepository";
|
---|
4 | import CenteredContainer from '../UtilComponents/CenteredContainer';
|
---|
5 | import '../UtilComponents/App.css'
|
---|
6 |
|
---|
7 | const Login = (props) => {
|
---|
8 |
|
---|
9 | const navigate = useNavigate();
|
---|
10 | const [formData, updateFormData] = React.useState({
|
---|
11 | username: "",
|
---|
12 | password: ""
|
---|
13 | })
|
---|
14 |
|
---|
15 | const handleChange = (e) => {
|
---|
16 | updateFormData({
|
---|
17 | ...formData,
|
---|
18 | [e.target.name]: e.target.value.trim()
|
---|
19 | })
|
---|
20 | }
|
---|
21 |
|
---|
22 | const onFormSubmit = (e) => {
|
---|
23 | e.preventDefault();
|
---|
24 | RebuMKService.login(formData.username, formData.password).then(resp => {
|
---|
25 | if(resp.data.typeOfLoggedUser === "Driver"){
|
---|
26 | localStorage.setItem("driverId", resp.data.idOfLoggedUser)
|
---|
27 | props.onLogin();
|
---|
28 | props.onLoadRequests(resp.data.idOfLoggedUser)
|
---|
29 | navigate("/home");
|
---|
30 | } else if(resp.data.typeOfLoggedUser === "Passenger"){
|
---|
31 | localStorage.setItem("passengerId", resp.data.idOfLoggedUser)
|
---|
32 | props.onLogin();
|
---|
33 | navigate("/home");
|
---|
34 | } else if(resp.data.typeOfLoggedUser === "Admin"){
|
---|
35 | localStorage.setItem("adminId", resp.data.idOfLoggedUser)
|
---|
36 | props.onLogin();
|
---|
37 | navigate("/home");
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | alert("Login unsucessfull, wrong password or email, try again.")
|
---|
41 | }
|
---|
42 | })
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | return (
|
---|
47 | <CenteredContainer>
|
---|
48 | <h3 style={{textAlign: "center", color: "darkcyan"}}>RebuMK</h3>
|
---|
49 | <h5 style={{textAlign: "center", color: "darkcyan"}}>Login to your account</h5>
|
---|
50 | <hr></hr>
|
---|
51 | <form onSubmit={onFormSubmit}>
|
---|
52 | <div className="form-group">
|
---|
53 | <label htmlFor="username">Username</label>
|
---|
54 | <input type="text"
|
---|
55 | className="form-control"
|
---|
56 | name="username"
|
---|
57 | id="username"
|
---|
58 | required
|
---|
59 | placeholder="Enter username"
|
---|
60 | onChange={handleChange}
|
---|
61 | />
|
---|
62 | </div>
|
---|
63 | <br></br>
|
---|
64 | <div className="form-group">
|
---|
65 | <label htmlFor="price">Password</label>
|
---|
66 | <input type="password"
|
---|
67 | className="form-control"
|
---|
68 | name="password"
|
---|
69 | id="password"
|
---|
70 | placeholder="Enter password"
|
---|
71 | required
|
---|
72 | onChange={handleChange}
|
---|
73 | />
|
---|
74 | </div>
|
---|
75 | <br></br>
|
---|
76 | <div className="d-grid gap-2 col-6 mx-auto">
|
---|
77 | <button id="submit" type="submit" className="myButton btn btn-primary" style={{backgroundColor: "darkcyan", borderColor: "black"}}>Login</button>
|
---|
78 | </div>
|
---|
79 | </form>
|
---|
80 | </CenteredContainer>
|
---|
81 | )
|
---|
82 | }
|
---|
83 |
|
---|
84 | export default Login; |
---|