Skip to content

Commit 87b2a98

Browse files
committed
ref:#187 13 login form change to continue the line of 14 15
1 parent 2495840 commit 87b2a98

6 files changed

Lines changed: 120 additions & 123 deletions

File tree

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import {LoginEntity} from '../model/login';
1+
import { LoginEntity } from "../model/login";
22

33
// Just a fake loginAPI
4-
export const isValidLogin = (loginInfo : LoginEntity) : boolean =>
5-
(loginInfo.login === 'admin' && loginInfo.password === 'test');
4+
export const isValidLogin = (loginInfo: LoginEntity): Promise<boolean> =>
5+
new Promise((resolve) => {
6+
setTimeout(() => {
7+
// mock call
8+
resolve(loginInfo.login === "admin" && loginInfo.password === "test");
9+
}, 500);
10+
});

hooks/13_LoginForm/src/app.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from "react";
22
import { HashRouter, Switch, Route } from "react-router-dom";
3-
import { LoginPage } from "./pages/loginPage";
3+
import { LoginContainer } from "./pages/login.container";
44
import { PageB } from "./pages/pageB";
55

66
export const App = () => {
77
return (
88
<>
99
<HashRouter>
1010
<Switch>
11-
<Route exact={true} path="/" component={LoginPage} />
11+
<Route exact={true} path="/" component={LoginContainer} />
1212
<Route path="/pageB" component={PageB} />
1313
</Switch>
1414
</HashRouter>

hooks/13_LoginForm/src/common/notification.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export const NotificationComponent: React.FC<Props> = (props) => {
2626
return (
2727
<Snackbar
2828
anchorOrigin={{
29-
vertical: "bottom",
30-
horizontal: "left",
29+
vertical: "top",
30+
horizontal: "right",
3131
}}
3232
open={show}
3333
autoHideDuration={3000}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as React from "react";
2+
import makeStyles from "@material-ui/styles/makeStyles";
3+
import createStyles from "@material-ui/styles/createStyles";
4+
import TextField from "@material-ui/core/TextField";
5+
import Button from "@material-ui/core/Button";
6+
import Card from "@material-ui/core/Card";
7+
import CardHeader from "@material-ui/core/CardHeader";
8+
import CardContent from "@material-ui/core/CardContent";
9+
import { LoginEntity, createEmptyLogin } from "../model/login";
10+
11+
interface PropsForm {
12+
onLogin: (login: LoginEntity) => void;
13+
}
14+
15+
// https://material-ui.com/styles/api/#makestyles-styles-options-hook
16+
const useFormStyles = makeStyles((theme) =>
17+
createStyles({
18+
formContainer: {
19+
display: "flex",
20+
flexDirection: "column",
21+
justifyContent: "center",
22+
},
23+
card: {
24+
maxWidth: 400,
25+
margin: "0 auto",
26+
},
27+
})
28+
);
29+
30+
export const LoginComponent: React.FC<PropsForm> = (props) => {
31+
const { onLogin } = props;
32+
const [loginInfo, setLoginInfo] = React.useState<LoginEntity>(
33+
createEmptyLogin()
34+
);
35+
const classes = useFormStyles();
36+
const onTexFieldChange = (fieldId) => (e) => {
37+
setLoginInfo({
38+
...loginInfo,
39+
[fieldId]: e.target.value,
40+
});
41+
};
42+
43+
return (
44+
<Card className={classes.card}>
45+
<CardHeader title="Login" />
46+
<CardContent>
47+
<div className={classes.formContainer}>
48+
<TextField
49+
label="Name"
50+
margin="normal"
51+
value={loginInfo.login}
52+
onChange={onTexFieldChange("login")}
53+
/>
54+
<TextField
55+
label="Password"
56+
type="password"
57+
margin="normal"
58+
value={loginInfo.password}
59+
onChange={onTexFieldChange("password")}
60+
/>
61+
<Button
62+
variant="contained"
63+
color="primary"
64+
onClick={() => onLogin(loginInfo)}
65+
>
66+
Login
67+
</Button>
68+
</div>
69+
</CardContent>
70+
</Card>
71+
);
72+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as React from "react";
2+
import { useHistory } from "react-router-dom";
3+
import { LoginEntity } from "../model/login";
4+
import { isValidLogin } from "../api/login";
5+
import { LoginComponent } from "./login.component";
6+
import { NotificationComponent } from "../common";
7+
8+
interface Props {}
9+
10+
export const LoginContainer: React.FC<Props> = (props) => {
11+
const history = useHistory();
12+
const [isShowAlert, setShowAlert] = React.useState(false);
13+
14+
const loginSucceeded = (isValid: boolean) => {
15+
if (isValid) {
16+
history.push("/pageB");
17+
} else {
18+
setShowAlert(true);
19+
}
20+
};
21+
22+
const handleLogin = (login: LoginEntity) => {
23+
isValidLogin(login).then(loginSucceeded);
24+
};
25+
26+
return (
27+
<>
28+
<LoginComponent onLogin={handleLogin} />
29+
<NotificationComponent
30+
show={isShowAlert}
31+
message="Invalid Form"
32+
onClose={() => setShowAlert(false)}
33+
/>
34+
</>
35+
);
36+
};

hooks/13_LoginForm/src/pages/loginPage.tsx

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)