import React, { PropTypes, Component } from 'react';
import { AccountsClient, PasswordSignupFields } from '@accounts/accounts';
import { Form, connectForm } from 'immutable-form';
import FormTypes from './FormTypes';

const loginForm = () => new Form('login', {
  fields: {
    user: {

    },
    password: {

    },
  },
});

const signupForm = () => new Form('signup', {
  fields: ({
    password: {

    },
    passwordConfirm: {

    },
    // eslint-disable-next-line consistent-return
    ...(() => {
      const {
      EMAIL_ONLY,
      USERNAME_ONLY,
      USERNAME_AND_EMAIL,
      USERNAME_AND_OPTIONAL_EMAIL,
    } = PasswordSignupFields;
      switch (AccountsClient.options().passwordSignupFields) {
        case EMAIL_ONLY:
          return { email: {} };
        case USERNAME_ONLY:
          return { username: {} };
        case USERNAME_AND_EMAIL:
        case USERNAME_AND_OPTIONAL_EMAIL:
          return { username: {}, email: {} };
        default:
          break;
      }
    })(),
  }),
});

class Accounts extends Component {
  static propTypes = {
    formType: PropTypes.string,
  }
  static defaultProps = {
    formType: FormTypes.LOGIN,
  }
  constructor(props) {
    super(props);
    this.state = {
      formType: this.props.formType,
    };
  }
  setFormType = (formType) => {
    this.setState({
      formType,
    });
  }
  render() {
    let ConnectedForm;
    switch (this.state.formType) {
      case FormTypes.LOGIN:
        ConnectedForm = connectForm(loginForm)(<AccountsClient.ui.LoginForm />);
        break;
      case FormTypes.SIGNUP:
        ConnectedForm = connectForm(signupForm)(<AccountsClient.ui.SignupForm />);
        break;
      default:
        break;
    }
    return <ConnectedForm setFormType={this.setFormType} {...this.props} />;
  }
}

export default Accounts;

@@ -91,16 +91,25 @@
 upFields
+, isEmail
  %7D from 
@@ -230,736 +230,1980 @@
 nst 
-loginForm = () =%3E new Form('login', %7B%0A  fields: %7B%0A    user: %7B%0A%0A    %7D,%0A    password: %7B%0A%0A    %7D,%0A  %7D,%0A%7D);%0A%0Aconst signupForm = () =%3E new Form('signup', %7B%0A  fields: (%7B%0A    password: %7B%0A%0A    %7D,%0A    passwordConfirm: %7B%0A%0A    %7D,%0A    // eslint-disable-next-line consistent-return%0A    ...(() =%3E %7B%0A      const %7B%0A      EMAIL_ONLY,%0A      USERNAME_ONLY,%0A      USERNAME_AND_EMAIL,%0A      USERNAME_AND_OPTIONAL_EMAIL,%0A    %7D = PasswordSignupFields;%0A      switch (AccountsClient.options().passwordSignupFields) %7B%0A        case EMAIL_ONLY:%0A          return %7B email: %7B%7D %7D;%0A        case USERNAME_ONLY:%0A          return %7B username: %7B%7D %7D;%0A        case USERNAME_AND_EMAIL:%0A        case USERNAME_AND_OPTIONAL_EMAIL:%0A          return %7B username: %7B%7D, email: %7B%7D
+validateUser = user =%3E user.trim().length === 0 && 'Username or email is required';%0Aconst validateUsername = username =%3E username.trim().length === 0 && 'Username is required';%0Aconst validateEmail = email =%3E !isEmail(email) && 'Not a valid email address';%0A%0Aconst loginForm = () =%3E new Form('login', %7B%0A  fields: %7B%0A    user: %7B%0A      validate: %5B%0A        validateUser,%0A      %5D,%0A    %7D,%0A    password: %7B%0A      validate: %5B%0A        password =%3E password.length === 0 && 'Password is required',%0A      %5D,%0A    %7D,%0A  %7D,%0A%7D);%0A%0Aconst signupForm = () =%3E new Form('signup', %7B%0A  fields: (%7B%0A    password: %7B%0A      validate: %5B%0A        password =%3E%0A          password.length %3C AccountsClient.options().minimumPasswordLength%0A            && %60Password must be at least $%7BAccountsClient.options().minimumPasswordLength%7D characters%60,%0A      %5D,%0A    %7D,%0A    passwordConfirm: %7B%0A      validate: %5B%0A        (confirmPassword, %7B form %7D) =%3E confirmPassword.length !== form.getFields().getIn(%5B'password', 'value'%5D)%0A          && 'Passwords do not match',%0A      %5D,%0A    %7D,%0A    // eslint-disable-next-line consistent-return%0A    ...(() =%3E %7B%0A      const %7B%0A      EMAIL_ONLY,%0A      USERNAME_ONLY,%0A      USERNAME_AND_EMAIL,%0A      USERNAME_AND_OPTIONAL_EMAIL,%0A    %7D = PasswordSignupFields;%0A      switch (AccountsClient.options().passwordSignupFields) %7B%0A        case EMAIL_ONLY:%0A          return %7B%0A            email: %7B%0A              validate: %5B%0A                validateEmail,%0A              %5D,%0A            %7D,%0A          %7D;%0A        case USERNAME_ONLY:%0A          return %7B%0A            username: %7B%0A              validate: %5B%0A                validateUsername,%0A              %5D,%0A            %7D,%0A          %7D;%0A        case USERNAME_AND_EMAIL:%0A        case USERNAME_AND_OPTIONAL_EMAIL:%0A          return %7B%0A            username: %7B%0A              validate: %5B%0A                validateUsername,%0A              %5D,%0A            %7D,%0A            email: %7B%0A              validate: %5B%0A                validateEmail,%0A              %5D,%0A            %7D,%0A         
  %7D;%0A
@@ -2258,24 +2258,69 @@
 (),%0A  %7D),%0A%7D)
+.setSubmit(() =%3E AccountsClient.createUser())
 ;%0A%0Aclass Acc

Add validation to login and signup form