All files / components/form/common login-form.jsx

71.88% Statements 23/32
84.21% Branches 32/38
54.55% Functions 6/11
71.88% Lines 23/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219              8x 8x         8x 8x       1x 1x 1x                                     8x 8x   8x 1x               8x           1x 1x 1x                                                                                                                                     1x 1x 1x                       1x 1x 1x                                                           1x                                                     1x                                      
import React from 'react';
import LoadStatus from '../../utilities/load-status.jsx';
import ErrorMessage from '../../utilities/error-message.jsx';
import PropTypes from 'prop-types';
 
class LoginForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      username: '',
      password: '',
      rememberMe: false
    };
    this.fillForm = this.fillForm.bind(this);
    this.submitForm = this.submitForm.bind(this);
  }
 
  submitForm(e) {
    e.preventDefault();
    e.stopPropagation();
    this.props.onSubmit(this.state.username, this.state.password, this.state.rememberMe);
  }
 
  fillForm(e) {
    switch (e.currentTarget.id) {
      case ('username'):
        this.setState({ username: e.currentTarget.value });
        break;
      case ('password'):
        this.setState({ password: e.currentTarget.value });
        break;
      case ('remember-me'):
        this.setState({ rememberMe: e.currentTarget.checked });
        break;
      default: break;
    }
  }
 
  render() {
    let error = '';
    Iif (this.props.error && this.props.error.type) {
      error = this.props.error;
    } else if (this.props.error && this.props.error.message) {
      error = (
        <ErrorMessage
          error={this.props.error}
          theme={this.props.error.theme || this.props.errorTheme}
        />
      );
    }
    
    return (
      <div className="login-box">
        <div className="login-logo">
          <a
            href={this.props.logoLink}
            onClick={this.props.onLogoClick ? e => {
              e.preventDefault();
              e.stopPropagation();
              this.props.onLogoClick();
            } : () => {}}
          >
            {this.props.clientLogo ? <img src={this.props.clientLogo} alt={typeof this.props.clientName === 'string' ? this.props.clientName : 'Brand Logo'} style={{ width: '60%', height: 'auto' }} /> : this.props.clientName}
          </a>
        </div>
        <div className="login-box-body" style={{ position: 'relative' }}>
          <div className="login-box-body-head">
            {error}
            <p className="login-box-msg">
              {this.props.headline}
              {this.props.subheadline ? (<small><br />{this.props.subheadline}</small>) : ''}
            </p>
          </div>
          <form noValidate onSubmit={this.submitForm}>
            <div className="form-group has-feedback">
              <input
                id="username"
                type="text"
                className="form-control"
                placeholder={this.props.userPlaceholder}
                onChange={this.fillForm}
              />
              <span className="glyphicon glyphicon-envelope form-control-feedback" />
            </div>
            <div className="form-group has-feedback">
              <input
                id="password"
                type="password"
                className="form-control"
                placeholder={this.props.passPlaceholder}
                onChange={this.fillForm}
              />
              <span className="glyphicon glyphicon-lock form-control-feedback" />
            </div>
            <div className="row">
              {this.props.remembers ? (
                <div className="col-xs-8">
                  <div className="checkbox icheck">
                    <label htmlFor="remember-me">
                      <input
                        type="checkbox"
                        id="remember-me"
                        defaultChecked={false}
                        onChange={this.fillForm}
                      />
                      {this.props.rememberMeText}
                    </label>
                  </div>
                </div>
              ) : ''}
              <div className="col-xs-offset-8 col-xs-4">
                <button
                  type="submit"
                  className={`btn btn-block btn-flat ${this.props.submitTheme}`}
                  onClick={this.submitForm}
                  id="login-btn"
                >
                  {this.props.submitText}
                </button>
              </div>
            </div>
          </form>
          {this.props.forgets ? (
            <a
              href={this.props.forgetLink}
              onClick={this.props.onForget ? e => {
                e.preventDefault();
                e.stopPropagation();
                this.props.onForget();
              } : () => {}}
            >
              I forgot my password
            </a>
          ) : ''}
          {this.props.forgets && this.props.registers ? <br /> : ''}
          {this.props.registers ? (
            <a
              href={this.props.registerLink}
              className="text-center"
              onClick={this.props.onRegister ? e => {
                e.preventDefault();
                e.stopPropagation();
                this.props.onRegister();
              } : () => {}}
            >
              Register an account
            </a>
          ) : ''}
          <div className="row">
            {this.props.children}
          </div>
          {!this.props.loading ? '' : (
            <div
              className="text-center align-middle overlay over-load"
              style={{
                position: 'absolute',
                backgroundColor: 'rgba(236, 240, 245, 0.25)',
                top: '0', right: '0', bottom: '0', left: '0',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center'
              }}
            >
              <LoadStatus size="5em" color="rgba(51, 51, 51, 0.3)" spins />
            </div>
          )}
        </div>
      </div>
    );
  }
}
 
LoginForm.propTypes = {
  loading: PropTypes.bool,
  errorTheme: PropTypes.string,
  error: PropTypes.oneOfType([
    PropTypes.object,
    PropTypes.element
  ]),
  clientLogo: PropTypes.string,
  onLogoClick: PropTypes.func,
  logoLink: PropTypes.string,
  headline: PropTypes.string,
  subheadline: PropTypes.string,
  remembers: PropTypes.bool,
  rememberMeText: PropTypes.string,
  submitText: PropTypes.string,
  submitTheme: PropTypes.string,
  onSubmit: PropTypes.func,
  userPlaceholder: PropTypes.string,
  passPlaceholder: PropTypes.string,
  forgets: PropTypes.bool,
  registers: PropTypes.bool,
  forgetLink: PropTypes.string,
  onForget: PropTypes.func,
  registerLink: PropTypes.string,
  onRegister: PropTypes.func
};
 
LoginForm.defaultProps = {
  loading: false,
  errorTheme: 'alert-danger',
  clientName: <span><b>Admin</b>LTE</span>,
  clientLogo: '',
  headline: 'Sign in to start your session',
  subheadline: '',
  remembers: false,
  rememberMeText: 'Remember Me',
  submitText: 'Sign In',
  submitTheme: 'btn-primary',
  userPlaceholder: 'Email',
  passPlaceholder: 'Password',
  forgets: false,
  registers: false,
  onSubmit: () => {}
};
 
export default LoginForm;