Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import Button from '@material-ui/core/Button';
  3. import TextField from '@material-ui/core/TextField';
  4. import Dialog from '@material-ui/core/Dialog';
  5. import DialogActions from '@material-ui/core/DialogActions';
  6. import DialogContent from '@material-ui/core/DialogContent';
  7. import DialogTitle from '@material-ui/core/DialogTitle';
  8. import Visibility from '@material-ui/icons/Visibility';
  9. import VisibilityOff from '@material-ui/icons/VisibilityOff';
  10. import IconButton from '@material-ui/core/IconButton';
  11. import InputAdornment from '@material-ui/core/InputAdornment';
  12. import { withStyles } from '@material-ui/core/styles';
  13. import classNames from 'classnames';
  14. import PropTypes from 'prop-types';
  15. import axios from 'axios';
  16. import Snackbar from '@material-ui/core/Snackbar';
  17. import green from '@material-ui/core/colors/green';
  18. import CloseIcon from '@material-ui/icons/Close';
  19. import CheckCircleIcon from '@material-ui/icons/CheckCircle';
  20. import CircularProgress from '@material-ui/core/CircularProgress';
  21.  
  22. const styles = theme => ({
  23.   root: {
  24.     display: 'flex',
  25.     flexWrap: 'wrap',
  26.   },
  27.   margin: {
  28.     margin: theme.spacing.unit,
  29.   },
  30.   textField: {
  31.     flexBasis: 200,
  32.   },
  33.   icon: {
  34.     marginRight: theme.spacing.unit,
  35.   },
  36.   success: {
  37.     display: 'flex',
  38.     alignItems: 'center',
  39.   },
  40.   snackBarColor: {
  41.     background: green[600],
  42.   },
  43.   buttonProgress: {
  44.     marginRight: 20,
  45.   }
  46. });
  47.  
  48. class Register extends Component {
  49.   state = {
  50.     open: false,
  51.     firstName: '',
  52.     lastName: '',
  53.     username: '',
  54.     email: '',
  55.     password: '',
  56.     matchingPassword: '',
  57.     showPassword: false,
  58.     showMatchingPassword: false,
  59.     invalidUsernameText: '',
  60.     invalidPasswordText: '',
  61.     invalidEmailText: '',
  62.     firstNameInvalid: false,
  63.     lastNameInvalid: false,
  64.     usernameInvalid: false,
  65.     matchingPasswordInvalid: false,
  66.     passwordInvalid: false,
  67.     emailInvalid: false,
  68.     registered: false,
  69.     loading: false,
  70.   };
  71.  
  72.   async onRegister() {
  73.     let firstName = this.state.firstName;
  74.     let lastName = this.state.lastName;
  75.     let password = this.state.password;
  76.     let email = this.state.email;
  77.     let username = this.state.username;
  78.     try {
  79.       const response = await axios.post('http://localhost:5000/api/users/register', { firstName, lastName,
  80.         password, email, username });
  81.       if (!response.data.success) {
  82.         if (response.data.errorType === "email") {
  83.           this.setState({ emailInvalid: true, invalidEmailText: response.data.error, loading: false });
  84.         } else {
  85.           this.setState({ usernameInvalid: true, invalidUsernameText: response.data.error, loading: false });
  86.         }
  87.       } else {
  88.         this.setState({ registered: true, open: false, firstName: '', lastName: '', email: '', password: '',
  89.           matchingPassword: '', username: '', loading: false });
  90.       }
  91.     } catch (err) {
  92.       console.log("Error registering user");
  93.     }
  94.   }
  95.  
  96.   handleOpen = () => {
  97.     this.setState({ open: true });
  98.   };
  99.  
  100.   handleClose = () => {
  101.     this.setState({ open: false });
  102.   };
  103.  
  104.   handleRegisteredClose = () => {
  105.     this.setState({ registered: false })
  106.   }
  107.  
  108.   handleRegister = () => {
  109.     let userInvalidResults = this.checkUsernameInvalid(this.state.username);
  110.     let usernameInvalid = userInvalidResults[0];
  111.     let invalidUsernameText = userInvalidResults[1];
  112.     let passwordInvalidResults = this.checkPasswordInvalid(this.state.password);
  113.     let passwordInvalid = passwordInvalidResults[0];
  114.     let invalidPasswordText = passwordInvalidResults[1];
  115.     let matchingPasswordInvalid = this.checkMatchingPasswordInvalid(this.state.password, this.state.matchingPassword);
  116.     let emailInvalidResults = this.checkEmailInvalid(this.state.email);
  117.     let emailInvalid = emailInvalidResults[0];
  118.     let invalidEmailText = emailInvalidResults[1];
  119.     let firstNameInvalid = this.checkNameInvalid(this.state.firstName);
  120.     let lastNameInvalid = this.checkNameInvalid(this.state.lastName);
  121.     let loading = false;
  122.     if (!usernameInvalid && !passwordInvalid && !matchingPasswordInvalid && !emailInvalid) {
  123.       loading = true;
  124.       this.onRegister();
  125.     }
  126.     this.setState({ usernameInvalid, invalidUsernameText, passwordInvalid, invalidPasswordText, matchingPasswordInvalid,
  127.         emailInvalid, invalidEmailText, firstNameInvalid, lastNameInvalid, loading });
  128.   }
  129.  
  130.   handleClickShowPassword = () => {
  131.     this.setState(state => ({ showPassword: !this.state.showPassword }));
  132.   };
  133.  
  134.   handleClickShowMatchingPassword = () => {
  135.     this.setState(state => ({ showMatchingPassword: !this.state.showMatchingPassword }));
  136.   };
  137.  
  138.   handleFirstNameChange = (e) => {
  139.     this.setState({ firstName: e.target.value });
  140.   };
  141.  
  142.   handleLastNameChange = (e) => {
  143.     this.setState({ lastName: e.target.value });
  144.   };
  145.  
  146.   handleUsernameChange = (e) => {
  147.     this.setState({ username: e.target.value });
  148.   };
  149.  
  150.   handleEmailChange = (e) => {
  151.     this.setState({ email: e.target.value });
  152.   };
  153.  
  154.   handlePasswordChange = (e) => {
  155.     this.setState({ password: e.target.value });
  156.   };
  157.  
  158.   handleMatchingPasswordChange = (e) => {
  159.     this.setState({ matchingPassword: e.target.value });
  160.   };
  161.  
  162.   checkNameInvalid(name) {
  163.     return name==='';
  164.   }
  165.  
  166.   checkUsernameInvalid(username) {
  167.     if (username.length < 4) {
  168.       return [true, 'Username must have at least 4 characters'];
  169.     } else if (!/^([a-zA-Z])$/.test(username[0])) {
  170.       return [true, 'Username must begin with a-z'];
  171.     } else if (!/^([a-zA-Z][a-zA-Z0-9]{3,9})$/.test(username)) {
  172.       return [true, 'Username must be alphanumeric and have no more than 10 characters'];
  173.     }
  174.     return [false, ''];
  175.   };
  176.  
  177.   checkEmailInvalid(email) {
  178.     if (!/^([a-zA-Z0-9.]+@[a-zA-Z0-9.]*dcu.ie)$/.test(email)) {
  179.       return [true, "Must use a DCU email"];
  180.     }
  181.     return [false, ''];
  182.   };
  183.  
  184.   checkPasswordInvalid(password) {
  185.     if (password.length < 8) {
  186.       return [true, 'Password must have at least 8 characters'];
  187.     } else if (/^([a-zA-Z0-9]+)$/.test(password)) {
  188.       return [true, 'Password must have at least 1 special character'];
  189.     }
  190.     return [false, ''];
  191.   };
  192.  
  193.   checkMatchingPasswordInvalid(password, matchingPassword) {
  194.     return !(password === matchingPassword);
  195.   };
  196.  
  197.   render() {
  198.     const { classes } = this.props;
  199.     const Icon = CheckCircleIcon;
  200.     if (this.state.registered) {
  201.       return (
  202.         <Snackbar
  203.           className={classes.snackBarColor}
  204.           color="green"
  205.           anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
  206.           open={this.state.registered}
  207.           ContentProps={{
  208.             classes: {
  209.               root: classes.snackBarColor
  210.             }
  211.           }}
  212.           message={
  213.           <span id="success" className={classes.success}>
  214.             <Icon className={classes.icon} />
  215.             Registration complete. Please go to your email to verify your account.
  216.           </span>
  217.           }
  218.           action={[
  219.             <IconButton
  220.               key="close"
  221.               aria-label="Close"
  222.               color="inherit"
  223.               onClick={this.handleRegisteredClose}
  224.             >
  225.               <CloseIcon />
  226.             </IconButton>,
  227.           ]}
  228.         />
  229.       );
  230.     }
  231.     return (
  232.       <div>
  233.         <Button color="secondary" onClick={this.handleOpen}>
  234.           Register
  235.         </Button>
  236.         <Dialog
  237.           open={this.state.open}
  238.           onClose={this.handleClose}
  239.         >
  240.           <DialogTitle>Register</DialogTitle>
  241.           <DialogContent>
  242.             <div className={classes.root}>
  243.               <TextField
  244.                 required
  245.                 disabled={this.state.loading}
  246.                 className={classNames(classes.margin, classes.textField)}
  247.                 error={this.state.firstNameInvalid}
  248.                 helperText={this.state.firstNameInvalid ? "Please enter a first name" : ""}
  249.                 onChange={this.handleFirstNameChange}
  250.                 value={this.state.firstName}
  251.                 margin="normal"
  252.                 id="firstName"
  253.                 label="First name"
  254.                 variant="outlined"
  255.               />
  256.               <TextField
  257.                 required
  258.                 disabled={this.state.loading}
  259.                 className={classNames(classes.margin, classes.textField)}
  260.                 error={this.state.lastNameInvalid}
  261.                 helperText={this.state.lastNameInvalid ? "Please enter a last name" : ""}
  262.                 onChange={this.handleLastNameChange}
  263.                 value={this.state.lastName}
  264.                 margin="normal"
  265.                 id="lastName"
  266.                 label="Last name"
  267.                 variant="outlined"
  268.               />
  269.               <TextField
  270.                 required
  271.                 disabled={this.state.loading}
  272.                 className={classNames(classes.margin, classes.textField)}
  273.                 error={this.state.usernameInvalid}
  274.                 helperText={this.state.usernameInvalid ? this.state.invalidUsernameText : ""}
  275.                 onChange={this.handleUsernameChange}
  276.                 value={this.state.username}
  277.                 margin="normal"
  278.                 id="username"
  279.                 label="Username"
  280.                 variant="outlined"
  281.               />
  282.               <TextField
  283.                 required
  284.                 disabled={this.state.loading}
  285.                 className={classNames(classes.margin, classes.textField)}
  286.                 error={this.state.emailInvalid}
  287.                 helperText={this.state.emailInvalid ? this.state.invalidEmailText : ""}
  288.                 onChange={this.handleEmailChange}
  289.                 value={this.state.email}
  290.                 margin="normal"
  291.                 id="email"
  292.                 label="Email Address"
  293.                 variant="outlined"
  294.               />
  295.               <TextField
  296.                 required
  297.                 disabled={this.state.loading}
  298.                 className={classNames(classes.margin, classes.textField)}
  299.                 error={this.state.passwordInvalid}
  300.                 helperText={this.state.passwordInvalid ? this.state.invalidPasswordText : ""}
  301.                 onChange={this.handlePasswordChange}
  302.                 value={this.state.password}
  303.                 margin="normal"
  304.                 id="password"
  305.                 label="Password"
  306.                 variant="outlined"
  307.                 type={this.state.showPassword ? 'text' : 'password'}
  308.                 InputProps={{
  309.                   endAdornment: (
  310.                     <InputAdornment position="end">
  311.                       <IconButton
  312.                         aria-label="Toggle password visibility"
  313.                         onClick={this.handleClickShowPassword}
  314.                       >
  315.                         {this.state.showPassword ? <VisibilityOff /> : <Visibility />}
  316.                       </IconButton>
  317.                     </InputAdornment>
  318.                   ),
  319.                 }}
  320.               />
  321.               <TextField
  322.                 required
  323.                 disabled={this.state.loading}
  324.                 className={classNames(classes.margin, classes.textField)}
  325.                 error={this.state.matchingPasswordInvalid}
  326.                 helperText={this.state.matchingPasswordInvalid ? "Passwords don't match" : ""}
  327.                 onChange={this.handleMatchingPasswordChange}
  328.                 value={this.state.matchingPassword}
  329.                 margin="normal"
  330.                 id="verify_password"
  331.                 label="Verify Password"
  332.                 variant="outlined"
  333.                 type={this.state.showMatchingPassword ? 'text' : 'password'}
  334.                 InputProps={{
  335.                   endAdornment: (
  336.                     <InputAdornment position="end">
  337.                       <IconButton
  338.                         aria-label="Toggle password visibility"
  339.                         onClick={this.handleClickShowMatchingPassword}
  340.                       >
  341.                         {this.state.showMatchingPassword ? <VisibilityOff /> : <Visibility />}
  342.                       </IconButton>
  343.                     </InputAdornment>
  344.                   ),
  345.                 }}
  346.               />
  347.             </div>
  348.           </DialogContent>
  349.           <DialogActions>
  350.             <Button onClick={this.handleClose} color="primary" disabled={this.state.loading}>
  351.               Cancel
  352.             </Button>
  353.             {!this.state.loading ?
  354.               (<Button onClick={this.handleRegister} color="primary" disabled={this.state.loading}>
  355.                 Register
  356.               </Button>) :
  357.               <CircularProgress size={24} className={classes.buttonProgress} />
  358.             }
  359.           </DialogActions>
  360.         </Dialog>
  361.       </div>
  362.     );
  363.   }
  364. }
  365.  
  366. Register.propTypes = {
  367.   classes: PropTypes.object.isRequired,
  368. };
  369.  
  370. export default withStyles(styles)(Register);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement