Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Register User
  3. export const registerUser = (userData, history) => dispatch => {
  4.   return axios
  5.     .post("/api/users/register", userData)
  6.     .then(res => history.push("/login")) // re-direct to login on successful register
  7.     .catch(err =>
  8.       dispatch({
  9.         type: GET_ERRORS,
  10.         payload: err.response.data
  11.       })
  12.     );
  13. };
  14.  
  15.  
  16. import React, { Component } from "react";
  17. import { Link, withRouter } from "react-router-dom";
  18. import PropTypes from "prop-types";
  19. import { connect } from "react-redux";
  20. import { registerUser } from "../../actions/index";
  21. import classnames from "classnames";
  22. import styled from 'styled-components';
  23. import Button from '@material-ui/core/Button';
  24. import quikLogo from '../../components/quikLogo.png';
  25. import TextField from '@material-ui/core/TextField';
  26.  
  27. const LoginForm = styled.div`
  28.     margin: 0 auto;
  29.     max-width: 320px;
  30.     outline: 1px solid #CCC;
  31.     box-shadow: 5px #CCC;
  32.     background-color: white;
  33.     padding:1.5em;
  34.     box-shadow: inset 0 1.5px 2px rgba(190, 190, 190, .4), 0 0 0 5px #f5f7f8;
  35. `
  36. const Center = styled.div`
  37.     text-align: center;
  38. `
  39.  
  40. export class Register extends Component {
  41.     constructor() {
  42.         super();
  43.         this.state = {
  44.             name: "",
  45.             email: "",
  46.             password: "",
  47.             password2: "",
  48.             errors: {}
  49.         };
  50.     }
  51.  
  52.     componentDidMount() {
  53.         // If logged in and user navigates to Register page, should redirect them to dashboard
  54.         if (this.props.auth.isAuthenticated) {
  55.           this.props.history.push("/dashboard");
  56.         }
  57.     }
  58.  
  59.     componentWillReceiveProps(nextProps) {
  60.         if (nextProps.errors) {
  61.             this.setState({
  62.                 errors: nextProps.errors
  63.             });
  64.         }
  65.     }
  66.     onChange = e => {
  67.         this.setState({ [e.target.id]: e.target.value });
  68.     };
  69.     onSubmit = e => {
  70.         e.preventDefault();
  71.         const newUser = {
  72.             name: this.state.name,
  73.             email: this.state.email,
  74.             password: this.state.password,
  75.             password2: this.state.password2
  76.         };
  77.         this.props.registerUser(newUser, this.props.history);
  78.     };
  79.     getErrors = e => {
  80.         const { errors } = this.state;
  81.         if(errors.errors === undefined || errors.errors.find(x => x.param === e) === undefined)
  82.         {
  83.             return true;
  84.         }
  85.         else
  86.         {
  87.             // return errors.errors.find(x => x.param === e).msg;
  88.             return false;
  89.         }
  90.     };
  91.     render() {
  92.        
  93.         return (
  94.             <div>
  95.                 <Link to="/"> Back to home</Link><br/>
  96.                 <Center>
  97.                     <img src={quikLogo} width="275" height= "180" alt = "logo"/>
  98.                 </Center>
  99.                 <LoginForm>
  100.                     <h4>New User? Register below:</h4>
  101.                     <p>Already have an account? <Link to="/login">Log in</Link></p>
  102.                 <form noValidate onSubmit={this.onSubmit}>
  103.                     <TextField
  104.                         onChange={this.onChange}
  105.                         value={this.state.name}
  106.                         error={this.getErrors('name')}
  107.                         id="name"
  108.                         label="Name"
  109.                         type="text"
  110.                         className={classnames("", {
  111.                             invalid: this.getErrors('name')
  112.                         })}
  113.                     />
  114.                     <span>{this.getErrors('name')}</span>
  115.                     <br/>
  116.                     <TextField
  117.                         onChange={this.onChange}
  118.                         value={this.state.email}
  119.                         error={this.getErrors('email')}
  120.                         id="email"
  121.                         type="email"
  122.                         label="Email"
  123.                         className={classnames("", {
  124.                             invalid: this.getErrors('email')
  125.                         })}
  126.                     />
  127.                     <span>{this.getErrors('email')}</span>
  128.                     <br/>
  129.                     <TextField
  130.                         onChange={this.onChange}
  131.                         value={this.state.password}
  132.                         error={this.getErrors('password')}
  133.                         id="password"
  134.                         type="password"
  135.                         label="Password"
  136.                         className={classnames("", {
  137.                             invalid: this.getErrors('password')
  138.                         })}
  139.                     />
  140.                     <span>{this.getErrors('password')}</span>
  141.                     <br/>
  142.                     <TextField
  143.                         onChange={this.onChange}
  144.                         value={this.state.password2}
  145.                         error={this.getErrors('password')}
  146.                         id="password2"
  147.                         type="password"
  148.                         label="Confirm Password"
  149.                         className={classnames("", {
  150.                             invalid: this.getErrors('password')
  151.                         })}
  152.                     />
  153.                     <br/>
  154.                     <br/>
  155.                     <div>
  156.                     <Button variant="contained" color="primary" type="submit">
  157.                     Register
  158.                     </Button>
  159.                     </div>
  160.                 </form>
  161.                 </LoginForm>
  162.             </div>
  163.         );
  164.     }
  165. }
  166.  
  167. Register.propTypes = {
  168.   registerUser: PropTypes.func.isRequired,
  169.   auth: PropTypes.object.isRequired,
  170.   errors: PropTypes.object.isRequired
  171. };
  172.  
  173. const mapStateToProps = state => ({
  174.   auth: state.auth,
  175.   errors: state.errors
  176. });
  177.  
  178. export default connect(
  179.   mapStateToProps,
  180.   { registerUser }
  181. )(withRouter(Register));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement