All files / src/containers/Impersonate component.jsx

0% Statements 0/94
0% Branches 0/47
0% Functions 0/24
0% Lines 0/48
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                                                                                                                                                                                                                                                             
import styles from './style.postcss';
 
import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import classnames from 'classnames';
import { postImpersonate, clearImpersonateData, getTokenEndPoint } from './actions';
import TextInput from 'components/TextInput';
import Button from 'components/Button';
import testClass from 'domain/testClass';
import AlertDialog from 'components/AlertDialog';
import ApiCall from 'containers/ApiCalls';
import PropTypes from 'prop-types';
 
const suffix = '@ef.com';
 
class Impersonate extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      impersonateEmail: '',
      emailChanged: false,
    };
 
    this._handleSwitchClick.bind(this);
    this._handleEmailChange.bind(this);
  }
 
  componentWillUnmount() {
    this.props.clearImpersonateData();
  }
 
  _handleSwitchClick() {
    this.props.postedImpersonate(this.state.impersonateEmail + suffix, this.props.token);
    this.setState({ emailChanged: false });
  }
 
  _handleEmailChange(e) {
    this.setState({ impersonateEmail: e.target.value, emailChanged: true });
  }
 
  _renderErrorPopup() {
    const error = this.props.error;
    if (! error) return;
    const { clean, apiKey } = this.props;
    const cleanError = () => clean(apiKey);
 
    return <AlertDialog isWarning
        content1={error.response.error}
        okButtonContent="OK"
        onButtonClick={cleanError} />;
  }
 
  render() {
    const { postImpersonateState } = this.props;
    const errorCode = postImpersonateState ? postImpersonateState.get('error') : undefined;
    const data = postImpersonateState ? postImpersonateState.get('data') : undefined;
 
    if (data) {
      this.props.success();
    }
 
    const inputClasses = classnames({ [styles.error]: ! this.state.emailChanged && errorCode },
        testClass('impersonate-dialog-input'));
 
    return <div className={styles.Impersonate}>
      <p className={styles.Impersonate_title}>Switch User</p>
      <div>
        <TextInput labelName="Email"
            className={styles.Impersonate_field}
            inputClassName={inputClasses}
            suffix={suffix}
            onChange={(e) => this._handleEmailChange(e)} />
      </div>
      <Button.Container className={styles.Impersonate_buttonContainer}
          align="center">
        <Button type={Button.Type.primary}
            className={classnames(styles.button, testClass('impersonate-dialog-switch'))}
            disabled={! this.state.impersonateEmail}
            onClick={() => this._handleSwitchClick()}>
          SWITCH
        </Button>
        <Button className={styles.button}
            onClick={() => this.props.close()}>
          CANCEL
        </Button>
      </Button.Container>
      {this._renderErrorPopup()}
    </div>;
  }
}
 
Impersonate.propTypes = {
  postImpersonateState: PropTypes.object,
  close: PropTypes.func,
  success: PropTypes.func,
  postedImpersonate: PropTypes.func,
  clearImpersonateData: PropTypes.func,
  token: PropTypes.string,
  apiKey: PropTypes.string,
  error: PropTypes.object,
  clean: PropTypes.func.isRequired,
};
 
function mapStateToProps(state) {
  const apiKey = `POST ${getTokenEndPoint()}`;
  const error = state.get('apiCalls').get(apiKey) && state.get('apiCalls').get(apiKey).error;
  return {
    postImpersonateState: state.get('impersonate')
      .get('postedImpersonate')
      .get('impersonate'),
    token: state.get('singleSignOn').get('user').get('id_token'),
    error,
    apiKey,
  };
}
 
function mapDispatchToProps(dispatch) {
  return {
    postedImpersonate: (email, token) =>
      dispatch(postImpersonate(email, token)),
    clearImpersonateData: () => dispatch(clearImpersonateData()),
    clean: (key) => dispatch(ApiCall.clean(key)),
  };
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Impersonate);