11import * as React from "react" ;
2- import { Link } from "react-router-dom" ;
32import { useHistory } from "react-router-dom" ;
43import makeStyles from "@material-ui/styles/makeStyles" ;
54import createStyles from "@material-ui/styles/createStyles" ;
65import Card from "@material-ui/core/Card" ;
76import CardHeader from "@material-ui/core/CardHeader" ;
87import CardContent from "@material-ui/core/CardContent" ;
9- import TextField from "@material-ui/core/TextField" ;
10- import Button from "@material-ui/core/Button" ;
11- import { FormHelperText } from "@material-ui/core" ;
128import { LoginEntity , createEmptyLogin } from "../model/login" ;
139import { isValidLogin } from "../api/login" ;
14- import { NotificationComponent } from "../common" ;
15- import {
16- LoginFormErrors ,
17- createDefaultLoginFormErrors ,
18- } from "./loginPage.viewmodel" ;
1910import { loginFormValidation } from "./loginPage.validation" ;
20- import { TextFieldForm } from "../common" ;
11+ import { Formik } from "formik" ;
12+ import { LoginForm } from "./loginFormComponent" ;
2113
2214// https://material-ui.com/styles/api/#makestyles-styles-options-hook
2315const useStyles = makeStyles ( ( theme ) =>
@@ -35,117 +27,42 @@ export const LoginPage: React.FC<Props> = (props) => {
3527 const [ loginInfo , setLoginInfo ] = React . useState < LoginEntity > (
3628 createEmptyLogin ( )
3729 ) ;
38- const [ loginFormErrors , setLoginFormErrors ] = React . useState < LoginFormErrors > (
39- createDefaultLoginFormErrors ( )
40- ) ;
41- const [ showLoginFailedMsg , setShowLoginFailedMsg ] = React . useState ( false ) ;
4230 const classes = useStyles ( ) ;
4331 const history = useHistory ( ) ;
4432
4533 const onLogin = ( ) => {
46- loginFormValidation . validateForm ( loginInfo ) . then ( ( formValidationResult ) => {
47- if ( formValidationResult . succeeded ) {
48- if ( isValidLogin ( loginInfo ) ) {
49- history . push ( "/pageB" ) ;
50- } else {
51- setShowLoginFailedMsg ( true ) ;
52- }
53- } else {
54- alert ( "error, review the fields" ) ;
55- const updatedLoginFormErrors = {
56- ...loginFormErrors ,
57- ...formValidationResult . fieldErrors ,
58- } ;
59- setLoginFormErrors ( updatedLoginFormErrors ) ;
60- }
61- } ) ;
34+ if ( isValidLogin ( loginInfo ) ) {
35+ history . push ( "/pageB" ) ;
36+ }
6237 } ;
6338
6439 const onUpdateLoginField = ( name , value ) => {
6540 setLoginInfo ( {
6641 ...loginInfo ,
6742 [ name ] : value ,
6843 } ) ;
69-
70- loginFormValidation
71- . validateField ( loginInfo , name , value )
72- . then ( ( fieldValidationResult ) => {
73- setLoginFormErrors ( {
74- ...loginFormErrors ,
75- [ name ] : fieldValidationResult ,
76- } ) ;
77- } ) ;
7844 } ;
7945
8046 return (
8147 < >
8248 < Card className = { classes . card } >
8349 < CardHeader title = "Login" />
8450 < CardContent >
85- < LoginForm
86- onLogin = { onLogin }
87- onUpdateField = { onUpdateLoginField }
88- loginInfo = { loginInfo }
89- loginFormErrors = { loginFormErrors }
90- />
51+ < Formik
52+ onSubmit = { onLogin }
53+ initialValues = { createEmptyLogin ( ) }
54+ validate = { loginFormValidation . validateForm }
55+ >
56+ { ( ) => (
57+ < LoginForm
58+ onLogin = { onLogin }
59+ onUpdateField = { onUpdateLoginField }
60+ loginInfo = { loginInfo }
61+ />
62+ ) }
63+ </ Formik >
9164 </ CardContent >
9265 </ Card >
93- < NotificationComponent
94- message = "Invalid login or password, please type again"
95- show = { showLoginFailedMsg }
96- onClose = { ( ) => setShowLoginFailedMsg ( false ) }
97- />
9866 </ >
9967 ) ;
10068} ;
101-
102- interface PropsForm {
103- onLogin : ( ) => void ;
104- onUpdateField : ( string , any ) => void ;
105- loginInfo : LoginEntity ;
106- loginFormErrors : LoginFormErrors ;
107- }
108-
109- // https://material-ui.com/styles/api/#makestyles-styles-options-hook
110- const useFormStyles = makeStyles ( ( theme ) =>
111- createStyles ( {
112- formContainer : {
113- display : "flex" ,
114- flexDirection : "column" ,
115- justifyContent : "center" ,
116- } ,
117- } )
118- ) ;
119-
120- const LoginForm : React . FC < PropsForm > = ( props ) => {
121- const classes = useFormStyles ( ) ;
122- const { onLogin, onUpdateField, loginInfo, loginFormErrors } = props ;
123-
124- // TODO: Enhacement move this outside the stateless component discuss why is a good idea
125- const onTexFieldChange = ( fieldId ) => ( e ) => {
126- onUpdateField ( fieldId , e . target . value ) ;
127- } ;
128-
129- return (
130- < div className = { classes . formContainer } >
131- < TextFieldForm
132- label = "Name"
133- name = "login"
134- value = { loginInfo . login }
135- onChange = { onUpdateField }
136- error = { loginFormErrors . login . errorMessage }
137- />
138- < TextFieldForm
139- label = "Password"
140- type = "password"
141- name = "password"
142- value = { loginInfo . password }
143- onChange = { onUpdateField }
144- error = { loginFormErrors . password . errorMessage }
145- />
146- < Button variant = "contained" color = "primary" onClick = { onLogin } >
147- Login
148- </ Button >
149- </ div >
150- ) ;
151- } ;
0 commit comments