|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + Form, |
| 4 | + FormGroup, |
| 5 | + TextInput, |
| 6 | + Checkbox, |
| 7 | + ActionGroup, |
| 8 | + Button, |
| 9 | + Title, |
| 10 | +} from '@patternfly/react-core'; |
| 11 | +import { connect } from 'dva'; |
| 12 | +import styles from './index.less'; |
| 13 | +import { validateEmail } from '@/utils/validator'; |
| 14 | + |
| 15 | +const mapStateToProps = state => { |
| 16 | + const { auth } = state; |
| 17 | + return { auth }; |
| 18 | +}; |
| 19 | + |
| 20 | +const LoginForm = props => { |
| 21 | + const [username, setUsername] = useState(''); |
| 22 | + const [password, setPassword] = useState(''); |
| 23 | + const [errors, setErrors] = useState({ |
| 24 | + email: '', |
| 25 | + }); |
| 26 | + const [btnDisabled, setBtnDisabled] = useState(true); |
| 27 | + |
| 28 | + const handleUsernameChange = val => { |
| 29 | + setUsername(val); |
| 30 | + const validEmail = validateEmail(val); |
| 31 | + setErrors({ |
| 32 | + ...errors, |
| 33 | + ...validEmail, |
| 34 | + }); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleLoginSubmit = () => { |
| 38 | + const { dispatch } = props; |
| 39 | + dispatch({ |
| 40 | + type: 'auth/loginUser', |
| 41 | + payload: { |
| 42 | + username, |
| 43 | + password, |
| 44 | + }, |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + /* eslint-disable no-restricted-syntax */ |
| 49 | + const validateForm = () => { |
| 50 | + if (username.trim() === '' || password.trim() === '') { |
| 51 | + return false; |
| 52 | + } |
| 53 | + for (const dep of Object.entries(errors)) { |
| 54 | + if (dep[1].length > 0) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + // if we reach here, it means |
| 59 | + // we have covered all of the edge cases. |
| 60 | + return true; |
| 61 | + }; |
| 62 | + |
| 63 | + useEffect( |
| 64 | + () => { |
| 65 | + if (validateForm()) { |
| 66 | + setBtnDisabled(false); |
| 67 | + } else setBtnDisabled(true); |
| 68 | + }, |
| 69 | + [username, password] |
| 70 | + ); |
| 71 | + |
| 72 | + const form = ( |
| 73 | + <div className={styles.section}> |
| 74 | + <Form className={styles.section}> |
| 75 | + <FormGroup label="Email address" isRequired fieldId="horizontal-form-name"> |
| 76 | + <TextInput |
| 77 | + isRequired |
| 78 | + type="text" |
| 79 | + id="horizontal-form-name" |
| 80 | + aria-describedby="horizontal-form-name-helper" |
| 81 | + name="horizontal-form-name" |
| 82 | + onChange={handleUsernameChange} |
| 83 | + /> |
| 84 | + <p className={styles.error}>{errors.email}</p> |
| 85 | + </FormGroup> |
| 86 | + <FormGroup label="Password" isRequired fieldId="horizontal-form-password"> |
| 87 | + <TextInput |
| 88 | + isRequired |
| 89 | + type="password" |
| 90 | + id="horizontal-form-password" |
| 91 | + name="horizontal-form-password" |
| 92 | + onChange={val => setPassword(val)} |
| 93 | + /> |
| 94 | + </FormGroup> |
| 95 | + <FormGroup fieldId="remember-me"> |
| 96 | + <Checkbox |
| 97 | + label="Keep me logged in" |
| 98 | + id="alt-form-checkbox-1" |
| 99 | + name="alt-form-checkbox-1" |
| 100 | + className={styles.check} |
| 101 | + /> |
| 102 | + </FormGroup> |
| 103 | + <ActionGroup> |
| 104 | + <Button |
| 105 | + isBlock |
| 106 | + onClick={handleLoginSubmit} |
| 107 | + className={styles.btn} |
| 108 | + id="submitBtn" |
| 109 | + isDisabled={btnDisabled} |
| 110 | + > |
| 111 | + <Title |
| 112 | + headingLevel="h4" |
| 113 | + size="xl" |
| 114 | + style={btnDisabled ? { color: 'black' } : { color: 'white' }} |
| 115 | + > |
| 116 | + Submit |
| 117 | + </Title> |
| 118 | + </Button> |
| 119 | + </ActionGroup> |
| 120 | + </Form> |
| 121 | + </div> |
| 122 | + ); |
| 123 | + return <React.Fragment>{form}</React.Fragment>; |
| 124 | +}; |
| 125 | + |
| 126 | +export default connect(mapStateToProps)(LoginForm); |
0 commit comments