import React, { PureComponent } from 'react'; import { Link } from 'react-router-dom'; import { connect } from 'react-redux'; import { registerAsync, isAuthenticatedAsync } from '../../actions/auth.actions'; import { Button, Icon, Input, Form, Col, Row, Spin } from 'antd'; import Title from 'antd/lib/typography/Title'; import './register-page.scss'; class RegisterForm extends PureComponent { constructor(props: any) { super(props); this.state = { confirmDirty: false, loading: false }; } componentDidMount() { this.setState({ loading: true }); this.props.dispatch(isAuthenticatedAsync()).then( (isAuthenticated: boolean) => { this.setState({ loading: false }); if (this.props.isAuthenticated) { this.props.history.replace('/tasks'); } } ); } render() { const { getFieldDecorator } = this.props.form; return (
Register {getFieldDecorator('username', { rules: [{ required: true, message: 'Please enter your username!' }], })( } type="text" name="username" placeholder="Username" />, )} {getFieldDecorator('firstName', { rules: [], })( } type="text" name="first-name" placeholder="First name" />, )} {getFieldDecorator('lastName', { rules: [], })( } type="text" name="last-name" placeholder="Last name" />, )} {getFieldDecorator('email', { rules: [ { type: 'email', message: 'The input is not valid email!', }, { required: true, message: 'Please input your email!', }, ], })( } type="text" name="email" placeholder="Email" />, )} {getFieldDecorator('password', { rules: [ { required: true, message: 'Please input your password!', }, { validator: this.validateToNextPassword, }, ], })( } name="password" placeholder="Password" />, )} {getFieldDecorator('passwordConfirmation', { rules: [ { required: true, message: 'Please confirm your password!', }, { validator: this.compareToFirstPassword, }, ], })( } name="password-confirmation" placeholder="Password confirmation" />, )} Already have an account? Login here.
); } private handleConfirmBlur = (event: any) => { const { value } = event.target; this.setState({ confirmDirty: this.state.confirmDirty || !!value }); }; private compareToFirstPassword = (rule: any, value: string, callback: Function) => { const { form } = this.props; if (value && value !== form.getFieldValue('password')) { callback('Two passwords that you enter are inconsistent!'); } else { callback(); } }; private validateToNextPassword = (rule: any, value: string, callback: Function) => { const { form } = this.props; if (value && this.state.confirmDirty) { form.validateFields(['passwordConfirmation'], { force: true }); } callback(); }; private onSubmit = (event: React.FormEvent) => { event.preventDefault(); this.props.form.validateFields((error: any, values: any) => { if (!error) { this.props.dispatch( registerAsync( values.username, values.firstName, values.lastName, values.email, values.password, values.passwordConfirmation, this.props.history, ), ); } }); } } const mapStateToProps = (state: any) => { return state.authContext; }; export default Form.create()(connect(mapStateToProps)(RegisterForm));