Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component, PropTypes} from 'react';
  2. import Modal from 'react-bootstrap/lib/Modal';
  3. import {reduxForm} from 'redux-form';
  4. import signupValidation from './signupValidation';
  5. import * as api from 'redux/modules/api';
  6. import {connect} from 'react-redux';
  7.  
  8. @connect(
  9.   state => {
  10.     let props = {};
  11.  
  12.     switch (api.getStatus(api.SIGNUP_CALL, state.api)) {
  13.         case api.SUCCESS:
  14.             props.success = true;
  15.         break;
  16.  
  17.         case api.ERROR:
  18.             props.apiError = api.getErrorMessage(api.SIGNUP_CALL, state.api);
  19.         break;
  20.     }
  21.        
  22.     //
  23.  
  24.     return props;
  25.   },
  26.   {
  27.       signup: api.signup
  28.   })
  29. @reduxForm({
  30.   form: 'signup',
  31.   fields: ['username', 'email', 'password', 'confirm'],
  32.   validate: signupValidation,
  33.   asyncValidate,
  34.   asyncBlurFields: ['email']
  35. })
  36.  
  37. export default class ModalSignup extends Component {
  38.   static propTypes = {
  39.     show: PropTypes.bool.isRequired,
  40.     onHide: PropTypes.func.isRequired,
  41.     active: PropTypes.string,
  42.     asyncValidating: PropTypes.bool.isRequired,
  43.     fields: PropTypes.object.isRequired,
  44.     dirty: PropTypes.bool.isRequired,
  45.     handleSubmit: PropTypes.func.isRequired,
  46.     invalid: PropTypes.bool.isRequired,
  47.     pristine: PropTypes.bool.isRequired,
  48.     valid: PropTypes.bool.isRequired,
  49.     success: PropTypes.bool,
  50.     apiError: PropTypes.string
  51.   }
  52.  
  53.   state = {
  54.     showModal: true,
  55.     appstate: this.props.appstate
  56.   }
  57.  
  58.   componentWillReceiveProps(nextProps) {
  59.         if (nextProps.success) {
  60.         this.setState({success: true});
  61.     } else if (nextProps.apiError) {
  62.         this.setState({errorText: nextProps.apiError});
  63.     }
  64.   }
  65.  
  66.   componentDidMount() {}
  67.  
  68.   handleSubmit = (event) => {
  69.     event.preventDefault();
  70.     const email = this.props.values.email;
  71.     const username = this.props.values.username;
  72.     const password = this.props.values.password;
  73.    
  74.     this.props.signup(username, email, password);
  75.   }
  76.  
  77.   onHide = () => {
  78.       this.setState({success: false});
  79.       this.setState({errorText: null});
  80.       this.state.appstate.setState({showSignup: false});
  81.   }
  82.  
  83.   render() {
  84.     const {
  85.       asyncValidating,
  86.       fields: {username, email, password, confirm},
  87.       handleSubmit
  88.       } = this.props;
  89.  
  90.     const renderInput = (field, label, placeholder, showAsyncValidating) =>
  91.       <div className={'form-group col-xs-12' + (field.error && field.touched ? ' has-error' : '')}>
  92.         <label htmlFor={field.name} className="control-label">{label}</label>
  93.           {showAsyncValidating && asyncValidating && <i className={'fa fa-cog fa-spin '}/>}
  94.           <input type={(field.name === 'password' || field.name === 'confirm') ? 'password' : 'text'}
  95.             className="form-control" placeholder={placeholder} id={field.name} {...field}/>
  96.           {field.error && field.touched && <div className="text-danger">{field.error}</div>}
  97.       </div>;
  98.  
  99.     const renderText = (text) =>
  100.         <p className="text-center message-container">{text}</p>;
  101.    
  102.     const renderError = (err) =>
  103.         <p className="text-center message-container error">{error}</p>;
  104.  
  105.     return (
  106.       <Modal show={this.props.show} onHide={this.props.onHide} id="signupModal" aria-labelledby="signupModalLabel" className="signup-modal">
  107.           <Modal.Header closeButton>
  108.             <Modal.Title>SignUp</Modal.Title>
  109.           </Modal.Header>
  110.           <Modal.Body>
  111.             <div className="row">
  112.                 {this.state.success && renderText("You have successfuly signed up. \
  113.                    You'll see a message asking you to verify your account.")}
  114.                        
  115.                 {this.state.errorText && renderError(this.state.errorText)}
  116.                
  117.                 {!(this.state.success || this.state.errorText) && (
  118.                     <form id="signupForm" method="post" onSubmit={this.handleSubmit}>
  119.                         {renderInput(email, 'Email*', 'Your Email', true)}
  120.                         {renderInput(username, 'Username*', 'Your Username')}
  121.                         {renderInput(password, 'Password*', 'From 6 to 12 characters')}
  122.                         {renderInput(confirm, 'Repeat Password*', 'Confirm')}
  123.                         <div className="form-group col-xs-12">
  124.                             <button type="submit" className="btn btn-warning btn-lg col-xs-12" onClick={this.handleSubmit}>
  125.                                 Signup
  126.                             </button>
  127.                         </div>
  128.                     </form>)}
  129.             </div>
  130.           </Modal.Body>
  131.         </Modal>
  132.     );
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement