<commit_before>import React from 'react';

import { setUIState, replaceUIState } from './actions';

const addReduxUIState = (
  { id, getInitialState, destroyOnUnmount }
) => WrappedComponent => {
  function mapStateToProps(state) {
    return {
      uiState: state.components[id],
    };
  }

  function mapDispatchToProps(dispatch, props) {
    return {
      setUIState: (newState, shouldDeepMerge) => {
        dispatch(setUIState({
          id,
          shouldDeepMerge,
          state: newState,
        }));
      },
      replaceUIState: (newState) => {
        dispatch(replaceUIState({
          id,
          state: newState,
        }));
      },
      resetUIState: () => {
        dispatch(replaceUIState({
          id,
          state: getInitialState(props),
        }));
      },
    };
  }

  class ExportedComponent extends React.Component {
    static propTypes = {
      uiState: React.PropTypes.object,
      setUIState: React.PropTypes.func,
      replaceUIState: React.PropTypes.func,
      resetUIState: React.PropTypes.func,
    };

    componentDidMount() {
      this.props.setUIState(getInitialState(this.props));
    }

    componentWillUnmount() {
      if (destroyOnUnmount) {
        this.props.resetUIState();
      }
    }

    render() {
      return (
        this.props.uiState
        ? <WrappedComponent {...this.props} />
        : null
      );
    }
  }

  const ProxyComponent = (props) => {
    if (!props.uiStateBranch || !props.dispatch) {
      throw new Error(
        'Cannot find state and dispatch in props. This probably means you\'re using ' +
        'addReduxUIState with useConnect set to false but havent passed state and dispatch ' +
        'in your component\'s mapStateToProps and mapDispatchToProps functions.'
      );
    }
    return (
      <ExportedComponent {...{
        ...mapStateToProps(props.uiStateBranch),
        ...mapDispatchToProps(props.dispatch),
      }}
      />
    );
  };
  ProxyComponent.propTypes = {
    uiStateBranch: React.PropTypes.object.isRequired,
    dispatch: React.PropTypes.func.isRequired,
  };

  return ProxyComponent;
};

export default addReduxUIState;

<commit_msg>Refactor addReduxUIState down to single wrapper component<commit_after>import React from 'react';

import { setUIState, replaceUIState } from './actions';

const mapStateToProps = (state, id) => ({
  uiState: state.components[id],
});

const mapDispatchToProps = (dispatch, props, id, getInitialState) => ({
  setUIState: (newState, shouldDeepMerge) => dispatch(setUIState({
    id,
    shouldDeepMerge,
    state: newState,
  })),
  replaceUIState: newState => dispatch(replaceUIState({
    id,
    state: newState,
  })),
  resetUIState: () => dispatch(replaceUIState({
    id,
    state: getInitialState(props),
  })),
});

const addReduxUIState = (
  { id, getInitialState, destroyOnUnmount }
) => WrappedComponent => class ExportedComponent extends React.Component {
  static propTypes = {
    uiStateBranch: React.PropTypes.object.isRequired,
    dispatch: React.PropTypes.func.isRequired,
  }

  constructor(props) {
    super(props);
    if (!props.uiStateBranch || !props.dispatch) {
      throw new Error(
        'Cannot find uiStateBranch and dispatch in props. This probably means you\'re using ' +
        'addReduxUIState rather than addReduxUISTateWithConnect and havent passed the required ' +
        'props through in your component\'s mapStateToProps and mapDispatchToProps functions.'
      );
    }
    this.mappedDispatchProps = mapDispatchToProps(this.props.dispatch, props, id, getInitialState);
  }

  componentDidMount() {
    this.mappedDispatchProps.setUIState(getInitialState(this.props));
  }

  componentWillUnmount() {
    if (destroyOnUnmount) {
      this.mappedDispatchProps.resetUIState();
    }
  }

  render() {
    const mappedStateProps = mapStateToProps(this.props.uiStateBranch, id);
    return (
      mappedStateProps.uiState
      ? (
        <WrappedComponent {...{
          ...mappedStateProps,
          ...this.mappedDispatchProps,
        }}
        />
      )
      : null
    );
  }
};

export default addReduxUIState;

{'fraction_non_alphanumeric': 0.11544950590503736, 'fraction_numerical': 0.0, 'mean_word_length': 2.7119856887298748, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5863886', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1230, 'n_words': 250}