|
1 |
| -import React, {FormEvent, ReactElement, useState} from 'react'; |
| 1 | +import React, {Dispatch, FormEvent, ReactElement, SetStateAction, useState} from 'react'; |
2 | 2 | import {Button, Col, Container, Form, Image, Row, Spinner} from "react-bootstrap";
|
3 | 3 | import {loginWithUsernameAndPassword} from "../../background/api/auth";
|
4 | 4 |
|
5 | 5 | import logo from "../../assets/images/logos/logoWithWhiteBorder.png";
|
6 | 6 |
|
| 7 | +export interface LoginInputInterface { |
| 8 | + handleSubmit: (event: FormEvent) => void, |
| 9 | + username: string|number|string[]|undefined, |
| 10 | + setUsername: Dispatch<SetStateAction<string>>, |
| 11 | + password: string|number|string[]|undefined, |
| 12 | + setPassword: Dispatch<SetStateAction<string>>, |
| 13 | + isLoading: boolean, |
| 14 | + setIsLoading: Dispatch<SetStateAction<boolean>>, |
| 15 | + stayLoggedIn: boolean, |
| 16 | + setStayLoggedIn: Dispatch<SetStateAction<boolean>>, |
| 17 | + errorMessage: string|null |
| 18 | +} |
| 19 | + |
7 | 20 | function Login(): ReactElement {
|
8 | 21 | const [userName, setUsername] = useState<string>("");
|
9 | 22 | const [password, setPassword] = useState<string>("");
|
10 | 23 | const [stayLoggedIn, setStayLoggedIn] = useState<boolean>(true);
|
11 | 24 | const [errorMessage, setErrorMessage] = useState<string>("");
|
12 |
| - const [loading, setLoading] = useState<boolean>(false); |
| 25 | + const [isLoading, setIsLoading] = useState<boolean>(false); |
13 | 26 |
|
14 | 27 |
|
15 | 28 | const handleSubmit = (event: FormEvent) => {
|
16 | 29 | event.preventDefault();
|
17 |
| - setLoading(true) |
| 30 | + setIsLoading(true) |
18 | 31 | loginWithUsernameAndPassword(userName, password, stayLoggedIn)
|
19 | 32 | .then(() => {
|
20 | 33 | //nothing to do here :)
|
21 | 34 | setErrorMessage("");
|
22 |
| - setLoading(false); |
| 35 | + setIsLoading(false); |
23 | 36 | })
|
24 | 37 | .catch(((error) => {
|
25 | 38 | console.log(error);
|
26 |
| - setLoading(false) |
| 39 | + setIsLoading(false) |
27 | 40 | setErrorMessage(error.response?.data.message);
|
28 | 41 | }))
|
29 | 42 |
|
30 | 43 |
|
31 | 44 | };
|
32 | 45 |
|
33 |
| - |
34 | 46 | return (
|
35 | 47 | <Container fluid className="h-100 ml-0 mr-0 login-page">
|
36 |
| - <Container fluid className="login-container"> |
37 |
| - <Container className="login-intro"> |
38 |
| - <Row className="justify-content-md-center"> |
39 |
| - <Image rounded src={logo} height="200px" width="auto"/> |
40 |
| - </Row> |
41 |
| - <Row className="justify-content-md-center"> |
42 |
| - <h1>Greetings Traveller!</h1> |
43 |
| - </Row> |
44 |
| - <Row className="justify-content-md-center"> |
45 |
| - <h2>Be welcome at FileFighter</h2> |
46 |
| - </Row> |
47 |
| - </Container> |
48 |
| - <Container className="login-input"> |
49 |
| - <Row className="mt-4"> |
50 |
| - <Col> |
51 |
| - <Form onSubmit={handleSubmit}> |
52 |
| - <Form.Group controlId="formBasicUsername"> |
53 |
| - <Form.Control placeholder="Username" value={userName} |
54 |
| - onChange={event => setUsername(event.target.value)}/> |
55 |
| - </Form.Group> |
| 48 | + <LoginInteractionArea |
| 49 | + handleSubmit={handleSubmit} |
| 50 | + username={userName} |
| 51 | + setUsername={setUsername} |
| 52 | + password={password} |
| 53 | + setPassword={setPassword} |
| 54 | + isLoading={isLoading} |
| 55 | + setIsLoading={setIsLoading} |
| 56 | + stayLoggedIn={stayLoggedIn} |
| 57 | + setStayLoggedIn={setStayLoggedIn} |
| 58 | + errorMessage={errorMessage} |
| 59 | + /> |
| 60 | + </Container>); |
| 61 | +} |
56 | 62 |
|
57 |
| - <Form.Group controlId="formBasicPassword"> |
58 |
| - <Form.Control type="password" placeholder="Password" value={password} |
59 |
| - onChange={event => setPassword(event.target.value)}/> |
60 |
| - <Form.Text className="text-muted"> |
61 |
| - Contact your administrator if you have forgotten your login details. |
62 |
| - </Form.Text> |
63 |
| - </Form.Group> |
| 63 | +export function LoginInteractionArea(props: LoginInputInterface) { |
| 64 | + const { |
| 65 | + handleSubmit, |
| 66 | + username, |
| 67 | + setUsername, |
| 68 | + password, |
| 69 | + setPassword, |
| 70 | + isLoading, |
| 71 | + setIsLoading, |
| 72 | + stayLoggedIn, |
| 73 | + setStayLoggedIn, |
| 74 | + errorMessage, |
| 75 | + } = props |
| 76 | + return ( |
| 77 | + <Container fluid className="login-container"> |
| 78 | + <LoginHeader/> |
| 79 | + <LoginInput |
| 80 | + handleSubmit={handleSubmit} |
| 81 | + username={username} |
| 82 | + setUsername={setUsername} |
| 83 | + password={password} |
| 84 | + setPassword={setPassword} |
| 85 | + isLoading={isLoading} |
| 86 | + setIsLoading={setIsLoading} |
| 87 | + stayLoggedIn={stayLoggedIn} |
| 88 | + setStayLoggedIn={setStayLoggedIn} |
| 89 | + errorMessage={errorMessage} |
| 90 | + /> |
| 91 | + </Container> |
| 92 | + ) |
| 93 | +} |
64 | 94 |
|
65 |
| - <Button variant="primary" type="submit" block> |
66 |
| - <Spinner |
67 |
| - as="span" |
68 |
| - animation="grow" |
69 |
| - size="sm" |
70 |
| - role="status" |
71 |
| - aria-hidden="true" |
72 |
| - className={loading ? "" : "d-none"} |
73 |
| - /> <span className={loading ? "sr-only" : "d-none"}>Loading...</span>Sign in |
74 |
| - </Button> |
75 |
| - <Form.Group controlId="formBasicCheckbox" className="mt-3 justify-content-center"> |
76 |
| - <Form.Check checked={stayLoggedIn} type="checkbox" label="Keep me signed in*" |
77 |
| - onChange={() => setStayLoggedIn(!stayLoggedIn)}/> |
78 |
| - <Form.Text className="text-muted"> |
79 |
| - *By clicking this, you accept the usage of cookies. |
80 |
| - </Form.Text> |
81 |
| - </Form.Group> |
82 |
| - <p className="text-danger">{errorMessage}</p> |
83 |
| - </Form> |
84 |
| - </Col> |
85 |
| - </Row> |
86 |
| - </Container> |
87 |
| - </Container> |
88 |
| - </Container>); |
| 95 | +export function LoginHeader() { |
| 96 | + return ( |
| 97 | + <Container className="login-intro"> |
| 98 | + <Row className="justify-content-md-center"> |
| 99 | + <Image rounded src={logo} height="200px" width="auto"/> |
| 100 | + </Row> |
| 101 | + <Row className="justify-content-md-center"> |
| 102 | + <h1>Greetings Traveller!</h1> |
| 103 | + </Row> |
| 104 | + <Row className="justify-content-md-center"> |
| 105 | + <h2>Be welcome at FileFighter</h2> |
| 106 | + </Row> |
| 107 | + </Container> |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +export function LoginInput(props: LoginInputInterface) { |
| 112 | + const { |
| 113 | + handleSubmit, |
| 114 | + username, |
| 115 | + setUsername, |
| 116 | + password, |
| 117 | + setPassword, |
| 118 | + isLoading, |
| 119 | + stayLoggedIn, |
| 120 | + setStayLoggedIn, |
| 121 | + errorMessage, |
| 122 | + } = props |
| 123 | + |
| 124 | + return ( |
| 125 | + <Container className="login-input"> |
| 126 | + <Row className="mt-4"> |
| 127 | + <Col> |
| 128 | + <Form onSubmit={handleSubmit}> |
| 129 | + <Form.Group controlId="formBasicUsername"> |
| 130 | + <Form.Control placeholder="Username" value={username} |
| 131 | + onChange={event => setUsername(event.target.value)}/> |
| 132 | + </Form.Group> |
| 133 | + |
| 134 | + <Form.Group controlId="formBasicPassword"> |
| 135 | + <Form.Control type="password" placeholder="Password" value={password} |
| 136 | + onChange={event => setPassword(event.target.value)}/> |
| 137 | + <Form.Text className="text-muted"> |
| 138 | + Contact your administrator if you have forgotten your login details. |
| 139 | + </Form.Text> |
| 140 | + </Form.Group> |
| 141 | + |
| 142 | + <Button variant="primary" type="submit" block> |
| 143 | + <Spinner |
| 144 | + as="span" |
| 145 | + animation="grow" |
| 146 | + size="sm" |
| 147 | + role="status" |
| 148 | + aria-hidden="true" |
| 149 | + className={isLoading ? "" : "d-none"} |
| 150 | + /> <span className={isLoading ? "sr-only" : "d-none"}>isLoading...</span>Sign in |
| 151 | + </Button> |
| 152 | + <Form.Group controlId="formBasicCheckbox" className="mt-3 justify-content-center"> |
| 153 | + <Form.Check checked={stayLoggedIn} type="checkbox" label="Keep me signed in*" |
| 154 | + onChange={() => setStayLoggedIn(!stayLoggedIn)}/> |
| 155 | + <Form.Text className="text-muted"> |
| 156 | + *By clicking this, you accept the usage of cookies. |
| 157 | + </Form.Text> |
| 158 | + </Form.Group> |
| 159 | + <p className="text-danger">{errorMessage}</p> |
| 160 | + </Form> |
| 161 | + </Col> |
| 162 | + </Row> |
| 163 | + </Container> |
| 164 | + ) |
89 | 165 | }
|
90 | 166 |
|
91 | 167 | export default Login;
|
0 commit comments