client/src/components/Reset.js
author ymh <ymh.work@gmail.com>
Tue, 18 Dec 2018 02:27:22 +0100
changeset 199 c78d579f4b55
permissions -rw-r--r--
Correct the registration screen. First version of a password reset screen.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
199
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import React, { Component } from 'react';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import { connect } from 'react-redux';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import { bindActionCreators } from 'redux';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import * as authActions from '../actions/authActions';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import { Trans } from 'react-i18next';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import * as R from 'ramda';
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
class Reset extends Component {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
  constructor(props) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    super(props);
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    this.state = {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
      email: ''
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
  reset = () => {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    const { email } = this.state;
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    this.props.authActions.resetSubmit(email);
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
  handleChange = (event) => {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    const newState = {};
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    newState[event.target.name] = event.target.value;
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    this.setState(newState);
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
  submit = (e) => {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    e.preventDefault();
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    this.reset();
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
  onClickLogin = (e) => {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    e.preventDefault();
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    this.props.history.push('/login');
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
  renderErrorMessage(errorMessages, fieldname) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    if (errorMessages && fieldname in errorMessages) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
      return errorMessages[fieldname].map((message, key) =>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        <p key={ key } className="form-text alert alert-danger mt-4" role="alert" >{ message }</p>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
      );
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
  renderNonFieldErrors(errorMessages) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    if (errorMessages && errorMessages.error) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
      return (
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        <div className="alert alert-danger mt-4" role="alert">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
          <Trans i18nKey="login.login_error">Unable to log in.</Trans>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      )
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    const errors = R.reduce(
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
      (acc, p) => R.concat(acc, R.ifElse(Array.isArray, R.identity, v => [v,])(R.pathOr([], ['data', p], errorMessages))),
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
      [],
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
      ['non_field_errors', 'detail']
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    );
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    if (errors && errors.length > 0 ) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
      return (
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        <div className="alert alert-danger mt-4" role="alert">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        { errors.map((message, key) =>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
          <p key={ key }><Trans i18nKey="reset.reset_error">{ message }</Trans></p>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        ) }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
      )
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
  render() {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    const errorMessages = this.props.register.error ? this.props.register.errorMessages : false ;
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    return (
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
      <div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        {/* <Navbar history={this.props.history} /> */}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        <div className="container-fluid">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
          <div className="row">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            <div className="col-lg-6 offset-md-3">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
              <div className="panel-login panel panel-default">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                <div className="card-header bg-secondary border-0 mt-5 pt-5">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                  <h4 className="text-center card-title font-weight-bold text-lg" onClick={this.onClickHome}>IRI Notes</h4>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                  <form className="pt-3 ml-5 pl-5" onSubmit={this.submit}>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                    <div className="form-group mb-2 ml-3 w-75">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
                      <label className="col-form-label text-primary font-weight-bold mt-2" htmlFor="email"><Trans i18nKey="common.email">Email</Trans></label>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                      <input className="form-control bg-irinotes-form border-0 text-muted" type="email" onChange={this.handleChange} value={this.state.email} name="email" />
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                      { errorMessages && this.renderErrorMessage(errorMessages.data, 'email') }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                    </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    { this.renderNonFieldErrors(errorMessages) }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                    <div className="text-center mr-5 pr-5">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                    <button type="submit" onClick={this.submit} className="btn btn-primary btn-lg text-secondary font-weight-bold mt-3"><Trans i18nKey="reset.reset">Reset passord</Trans></button>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
                    </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                  </form>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
              </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
              <p className="text-center">
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                <a className="text-muted" href="/login" onClick={ this.onClickLogin }><Trans i18nKey="register.already_registered">Déjà inscrit ? Se connecter</Trans>.</a>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
              </p>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
            </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
          </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
      </div>
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    );
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
function mapStateToProps(state, props) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
  return {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    register: state.register
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
  };
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
function mapDispatchToProps(dispatch) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
  return {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    authActions: bindActionCreators(authActions, dispatch)
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
export default connect(mapStateToProps, mapDispatchToProps)(Reset);