EnterLayout.js 4.88 KB
import _                                  from 'lodash';
import { Meteor }                         from 'meteor/meteor';
import { SimpleSchema }                   from 'meteor/aldeed:simple-schema';
import { Bert }                           from 'meteor/themeteorchef:bert';

import React                              from 'react';
import { Container, Row, Col }            from 'reactstrap';
import { Link }                           from 'react-router';
import { If, Case }                       from '/imports/client/components/Logic';

import { LoginPane }                      from '/imports/client/views/org/enter/LoginPane';
import { ForgotPane }                     from '/imports/client/views/org/enter/ForgotPane';
import { ResetPane }                      from '/imports/client/views/org/enter/ResetPane';
import  Validation                        from '/imports/validation/validationMethods';

// const signupSchemaValidator = new SimpleSchema({
//   email:      { type: String, regEx: SimpleSchema.RegEx.Email},
//   firstName:  { type: String, },
//   lastName:   { type: String, },
//   password:   { type: String, min: 6},
// }).validator();

let validation = new Validation();

export class EnterLayout extends React.Component {


  constructor(props) {
    super(props);
    this.state = {
      email:        '',
      password:     '',
      firstName:    '',
      lastName:     '',
      orgName:      '',
      loading:      false,
      error:        '',
      message:      '',
    };
  };

  componentWillReceiveProps(nextProps) {
    console.log(this.props.pane);
    console.log("this.props.pane");
    if(this.props.pane !== nextProps.pane) {
      this.onClearState();
    }
  };


  onClearState(loading) {
    this.setState({
      loading: loading || false,
      error:   '',
      message: '',
    });
  };

  onUpdate(key, value) {
    this.setState({[key]: value});
  };

  onForgot(e) {
    e.preventDefault();
    this.onClearState(true);

    Accounts.forgotPassword({email: this.state.email || '...'}, (error) => {
      this.onClearState();
      if(error) {
        this.setState({error: 'An error occured.'});
      } else {
        this.setState({message: 'Reset email has been sent. Check your inbox.'});
      }
    });
  };

  onReset(e) {
    e.preventDefault();
    this.onClearState(true);
    if(!validation.passwordValidation(this.state.password)){
      Bert.alert('Password must be a minimum of 6 characters in length.', 'danger');
      this.onClearState();
      return false;
    }else if(!validation.noQwertysAllowed(this.state.password)){
      Bert.alert('No qwertys allowed!', 'danger');
      this.onClearState();
      return false;
    } else{
      Accounts.resetPassword(
        this.props.location.query.token,
        this.state.password,
        (error) => {
          this.onClearState();
          if(error) {
            this.setState({error: 'An error occured.'});
          }
        }
      );
    }
  };

  onLogin(e) {
    e.preventDefault();
    this.onClearState(true);
    if(this.state.email.trim() == '' || !validation.validateEmail(this.state.email)){
      this.onClearState();
      Bert.alert('Please enter a valid email address!', 'danger');
      return false;
    }
    if(this.state.password.trim() == ''){
      Bert.alert('Please enter your password!', 'danger');
      this.onClearState();
      return false;
    }
    Meteor.loginWithPassword(
      this.state.email,
      this.state.password,
      (e, r) => {
        this.onClearState();
        if(e) {
          this.setState({error: 'Wrong email or password.'})
        }
      }
    );
  };

  render() {
    return (
      <div className = "enterLayout__content--bg">
        <Container>
            <Row>
            <Case
                switch = {this.props.pane}
                case0 = "forgot"
                then0 = {() => (
                <ForgotPane
                    data          = {this.state}
                    location      = {this.props.location}
                    onUpdate      = {(...a) => this.onUpdate(...a)}
                    onForgot      = {(...a) => this.onForgot(...a)}
                />
                )}
                case1 = "reset"
                then1 = {() => (
                <ResetPane
                    data          = {this.state}
                    location      = {this.props.location}
                    onUpdate      = {(...a) => this.onUpdate(...a)}
                    onReset       = {(...a) => this.onReset(...a)}
                />
                )}
                else = {() => (
                <LoginPane
                    data          = {this.state}
                    location      = {this.props.location}
                    onUpdate      = {(...a) => this.onUpdate(...a)}
                    onLogin       = {(...a) => this.onLogin(...a)}
                />
                )}
            />
            </Row>
        </Container>
      </div>
    );
  };
};