Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from 'react';
- import { connect } from 'react-redux';
- import { Redirect } from 'react-router-dom';
- import classes from './Auth.css';
- import Input from '../../components/UI/Input/Input';
- import Button from '../../components/UI/Button/Button';
- import Spinner from '../../components/UI/Spinner/Spinner';
- import * as actions from '../../store/actions/';
- class Auth extends Component {
- state = {
- controls: {
- email: {
- elementType: 'input',
- elementConfig: {
- type: 'email',
- placeholder: 'your email'
- },
- value: '',
- validation: {
- required: true,
- isEmail: true
- },
- valid: false,
- touched: false
- },
- password: {
- elementType: 'input',
- elementConfig: {
- type: 'password',
- placeholder: 'Password'
- },
- value: '',
- validation: {
- required: true,
- minLength: 7
- },
- valid: false,
- touched: false
- }
- },
- isSignUp: true,
- };
- inputChangedHandler = (event, controlName) => {
- const updateControls = {
- ...this.state.controls,
- [controlName]: {
- ...this.state.controls[controlName],
- value: event.target.value,
- valid: this.checkValidity(event.target.value, this.state.controls[controlName].validation),
- touched: true
- }
- };
- this.setState({ controls: updateControls });
- };
- switchAuthModeHandler = () => {
- this.setState(prevState => {
- return { isSignUp: !prevState.isSignUp }
- })
- };
- componentDidMount() {
- // When we want to redirect to checkout when not creating a burger
- if (!this.props.buildingBurger && this.props.authRedirectPath !== '/') {
- this.props.onSetAuthRedirectPath();
- }
- }
- checkValidity(value, rules) {
- let isValid = true;
- if (rules.required) {
- isValid = value.trim() !== '' && isValid;
- }
- if (rules.minLength) {
- isValid = value.length >= rules.minLength && isValid;
- }
- if (rules.maxLength) {
- isValid = value.length <= rules.maxLength && isValid;
- }
- if (rules.isEmail) {
- const emailRule = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
- isValid = emailRule.test(value) && isValid;
- }
- return isValid;
- }
- submitHandler = (event) => {
- event.preventDefault();
- this.props.onAuth(
- this.state.controls.email.value,
- this.state.controls.password.value,
- this.state.isSignUp
- );
- };
- render() {
- const formElementArray = [];
- for (let key in this.state.controls) {
- formElementArray.push({
- id: key,
- config: this.state.controls[key]
- })
- }
- let form = formElementArray.map(formElement => (
- <Input
- changed={(event) => this.inputChangedHandler(event, formElement.id)}
- key={formElement.id}
- value={formElement.config.value}
- elementType={formElement.config.elementType}
- invalid={!formElement.config.valid}
- shouldValidate={formElement.config.validation}
- touched={formElement.config.touched}
- errorMessage={form}
- elementConfig={formElement.config.elementConfig}/>
- ));
- if (this.props.loading) {
- form = <Spinner/>
- }
- let errorMessage = null;
- // Use message coming from Firebase
- if (this.props.error) {
- errorMessage = (
- <p>{this.props.error.message}</p>
- );
- }
- // Redirect away from the form when user is signed in
- let authRedirect = null;
- if (this.props.isAuthenticated) {
- authRedirect = <Redirect to={this.props.authRedirectPath}/>
- }
- return (
- <div className={classes.Auth}>
- {authRedirect}
- {errorMessage}
- <form onSubmit={this.submitHandler}>
- {form}
- <Button btnType="Success">Submit</Button>
- </form>
- <Button
- clicked={this.switchAuthModeHandler}
- btnType="Danger">
- {this.state.isSignUp ? 'Let\'s SIGN IN' : 'Let\'s SIGN UP'}
- </Button>
- </div>
- );
- }
- }
- // data props from Reducers
- const mapStateToProps = state => {
- return {
- loading: state.auth.loading,
- error: state.auth.error,
- isAuthenticated: state.auth.token !== null,
- buildingBurger: state.burgerBuilder.building,
- authRedirectPath: state.auth.authRedirectPath
- }
- };
- const mapDispatchToProps = dispatch => {
- return {
- onAuth: (email, password, isSignup) => dispatch(actions.auth(email, password, isSignup)),
- onSetAuthRedirectPath: () => dispatch(actions.setAuthRedirectPath('/'))
- }
- };
- export default connect(mapStateToProps, mapDispatchToProps)(Auth);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement