Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import logo from './../assets/images/GigLogoOrange.png';
  3. import Notifications, { notify } from 'react-notify-toast';
  4. import api from "../api";
  5. import {Redirect} from 'react-router-dom';
  6. import { timingSafeEqual } from 'crypto';
  7.  
  8. export default class Register extends Component {
  9.     constructor(props) {
  10.         super(props);
  11.         this.state = {
  12.             username: '',
  13.             email: '',
  14.             password: '',
  15.             gender: null,
  16.             created: false,
  17.             firstName: '',
  18.             lastName: '',
  19.             city: '',
  20.             age: '16',
  21.             errors: [],
  22.             redirect: false
  23.         };
  24.         this.onChange = this.onChange.bind(this)
  25.         this.onSubmit = this.onSubmit.bind(this)
  26.     }
  27.  
  28.     onChange(e) {
  29.         this.setState({ [e.target.name]: e.target.value })
  30.     }
  31.  
  32.     validateEmail(email) {
  33.         const re = /^(([^<>()\[\]\\.,;:\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,}))$/;
  34.         return re.test(String(email).toLowerCase());
  35.     }
  36.  
  37.     validateForm(params){
  38.         let errors  = [];
  39.         Object.keys(params.user).map(key => {
  40.                 if(!params.user[key]){
  41.                     errors.push(`invalid ${key}`);
  42.                 }else if(key === "email")
  43.                 {
  44.                     if(!this.validateEmail(params.user[key]))
  45.                     {
  46.                         errors.push(`invalid ${key}`);
  47.                     }
  48.                 }else if(key === "password" || key === "username")
  49.                 {
  50.                     if(params.user[key].length < 8)
  51.                     {
  52.                         errors.push(`${key} is too short(minimum 8 signs)`);
  53.                     }
  54.                 }
  55.         });
  56.        
  57.         if(errors.length > 0){
  58.             this.setState({ errors })
  59.             notify.show(errors + ', ','error');
  60.             return false;
  61.         }else {return true;}
  62.     }
  63.    
  64.     onSubmit(e) {
  65.         e.preventDefault();
  66.         const { email, password, username } = this.state;
  67.         const params = {
  68.             "user":{
  69.                 "email":email,
  70.                 "password":password,
  71.                 "login":username
  72.             }
  73.         }
  74.         if(this.validateForm(params)){
  75.             // const url = 'http://e3a5e7a8.ngrok.io/users';
  76.             this.setState({ errors: [] });
  77.             notify.show("success",'success');
  78.             api.createUser(params)
  79.             .then( () => this.setState({ created: true }))
  80.             .catch( err => notify.show(err,'error'))
  81.             //this.setState({created:true});
  82.  
  83.         }
  84.     }
  85.  
  86.     onLoginClick=()=>
  87.     {
  88.         console.log("click")
  89.         this.setState({redirect:true})
  90.     }
  91.    
  92.     render() {
  93.         const options = [];
  94.         const {redirect} = this.state;
  95.  
  96.         for (let i = 16; i <= 100; i++) {
  97.             options.push(<option key={i}>{i}</option>)
  98.         }
  99.  
  100.         if(redirect)
  101.             {
  102.             return <Redirect to='/Login' />;
  103.             }
  104.        
  105.         return (
  106.             <div className="whole-screen">
  107.                 <div className="uk-container uk-container-expand uk-margin-bottom">
  108.                     <img className="uk-align-left logo-img" src={logo} alt="" />
  109.                 </div>
  110.                 {this.state.created?
  111.                 <div className="uk-container uk-container-expand uk-vertical-align-middle">
  112.                     <div className="uk-margin">
  113.                         <span>Login succesful!</span>
  114.                         <br />
  115.                         <button className="uk-button uk-button-default" onClick={this.onLoginClick}>Go to Login screen</button>
  116.                         <br />
  117.                     </div>
  118.                 </div>
  119.              :
  120.                 <form className="uk-panel uk-panel-box uk-form" onSubmit={this.onSubmit}>
  121.                     <h1 className="uk-container uk-container-expand uk-vertical-align-middle uk-heading">Register</h1>
  122.                     <hr></hr>
  123.                     <div className="uk-container uk-container-expand uk-vertical-align-middle ">
  124.                         <div className="uk-margin uk-margin-bottom uk-margin-top">
  125.                             <div className="uk-inline">
  126.                                 <input className="uk-input" type="text" placeholder="Username" name="username" value={this.state.username} onChange={e => this.onChange(e)} />
  127.                             </div>
  128.                             <div className="uk-inline">
  129.                                 <input className="uk-input" type="text" placeholder="E-mail" name="email" value={this.state.email} onChange={e => this.onChange(e)} />
  130.                             </div>
  131.                             <div className="uk-inline">
  132.                                 <input className="uk-input" type="password" placeholder="Password" name="password" value={this.state.password} onChange={e => this.onChange(e)} />
  133.                             </div>
  134.                         </div>
  135.                     </div>
  136.                     <hr></hr>
  137.                     <div className="uk-container uk-container-expand uk-vertical-align-middle ">
  138.                         <div className="uk-margin uk-margin-top">
  139.                             <div className="uk-inline">
  140.                                 <input className="uk-input" type="text" placeholder="First Name" name="firstName" value={this.state.firstName} onChange={e => this.onChange(e)} />
  141.                             </div>
  142.                             <div className="uk-inline">
  143.                                 <input className="uk-input" type="text" placeholder="Last Name" name="lastName" value={this.state.lastName} onChange={e => this.onChange(e)} />
  144.                             </div>
  145.                             <div className="uk-inline">
  146.                                 <input className="uk-input" type="text" placeholder="City" name="city" value={this.state.city} onChange={e => this.onChange(e)} />
  147.                             </div>
  148.                         </div>
  149.                         <div className="uk-container uk-container-expand uk-vertical-align-middle">
  150.                             <div className="uk-margin" onChange={this.onChange.bind(this)}>
  151.                                 <label><input className="uk-radio" type="radio" name="gender" value="Male" /> Male</label>
  152.                                 <label><input className="uk-radio" type="radio" name="gender" value="Female" /> Female</label>
  153.                             </div>
  154.                             <div className="uk-margin">
  155.                                 <div className="uk-inline">
  156.                                     <div className="uk-button-mini uk-form-select">
  157.                                     Age:
  158.                                     {
  159.                                     }
  160.                                     <select>
  161.                                         {options}
  162.                                     </select>
  163.                                     </div>
  164.                                 </div>
  165.                             </div>
  166.                             <div className="uk-margin">
  167.                                 <button className="uk-button uk-button-default" type="submit">REGISTER</button>
  168.                                 <br />
  169.                             </div>
  170.                         </div>
  171.                     </div>
  172.                 </form>
  173.                 }
  174.             </div>
  175.         )
  176.     }
  177. }
  178.  
  179.  
  180. ------------------------------------ api.js---------------------
  181.  
  182. import axios from "axios";
  183. import Notifications, { notify } from "react-notify-toast";
  184. import { networkInterfaces } from "os";
  185.  
  186. const url = "https://gigapi.herokuapp.com/";
  187.  
  188. class Api {
  189.   constructor() {
  190.     this.api = axios.create({
  191.       baseURL: url,
  192.       created:false
  193.     });
  194.   }
  195.  
  196.   setHeaders() {
  197.     this.api.defaults.headers = {
  198.       "Content-Type": "application/json"
  199.     };
  200.   }
  201.  
  202.   setToken(userData) {
  203.     this.setHeaders();
  204.     this.api.post("users/login", userData).then(res => console.log);
  205.   }
  206.  
  207.   createUser(userData) {
  208.     let created = this.state;
  209.     this.setHeaders();
  210.     this.api.post("users/create", userData)
  211.     .then(res => {created = true; console.log(created)})
  212.     .catch((error) =>{
  213.       if(error.res)
  214.       {
  215.         notify.show(error.response,'error');
  216.       }else if(error.request)
  217.       {
  218.         notify.show(error.request,'error');
  219.       }
  220.     });
  221.   }
  222. }
  223.  
  224. export default Api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement