<commit_before>import React from 'react';
import R from 'ramda';

var ReactPromisedComponent = (
  promiseProp, LoadingComponent, ErrorComponent, SuccessComponent
) => class extends React.Component
{
  constructor(props)
  {
    super(props);
    this.state = {loading: true, error: null, value: null};
  }

  executePromise(params)
  {
    this.setState({loading: true, error: null, value: null});

    this.props[promiseProp](params).then(
      value => this.setState({loading: false, value: value}),
      error => this.setState({loading: false, error: error})
    );
  }

  componentWillUpdate(nextProps, nextState)
  {
    if (nextProps[promiseProp + '_params'] !== this.props[promiseProp + '_params'])
    {
      // We are detecting a params change, we need to recall the promise
      this.executePromise(nextProps[promiseProp + '_params']());
    }
  }

  retryPromise()
  {
    this.executePromise(this.props[promiseProp + '_params']());
  }

  componentWillMount()
  {
    this.executePromise(this.props[promiseProp + '_params']());
  }

  render()
  {
    if (this.state.loading)
    {
      return (
        <LoadingComponent />
      );
    }
    else if (this.state.error !== null)
    {
      return (
        <ErrorComponent error={this.state.error} retry={this.retryPromise.bind(this)} />
      );
    }
    else
    {
      var propsWithoutThePromise = R.dissoc(promiseProp, this.props);
      var result = { 'result': Object.assign({}, this.state.value) };
      return (
        <SuccessComponent {...propsWithoutThePromise} {...result} />
      );
    }
  }
};

export default ReactPromisedComponent;
<commit_msg>Passes the result directly to the component rather using ... operator<commit_after>import React from 'react';
import R from 'ramda';

var ReactPromisedComponent = (
  promiseProp, LoadingComponent, ErrorComponent, SuccessComponent
) => class extends React.Component
{
  constructor(props)
  {
    super(props);
    this.state = {loading: true, error: null, value: null};
  }

  executePromise(params)
  {
    this.setState({loading: true, error: null, value: null});

    this.props[promiseProp](params).then(
      value => this.setState({loading: false, value: value}),
      error => this.setState({loading: false, error: error})
    );
  }

  componentWillUpdate(nextProps, nextState)
  {
    if (nextProps[promiseProp + '_params'] !== this.props[promiseProp + '_params'])
    {
      // We are detecting a params change, we need to recall the promise
      this.executePromise(nextProps[promiseProp + '_params']());
    }
  }

  retryPromise()
  {
    this.executePromise(this.props[promiseProp + '_params']());
  }

  componentWillMount()
  {
    this.executePromise(this.props[promiseProp + '_params']());
  }

  render()
  {
    if (this.state.loading)
    {
      return (
        <LoadingComponent />
      );
    }
    else if (this.state.error !== null)
    {
      return (
        <ErrorComponent error={this.state.error} retry={this.retryPromise.bind(this)} />
      );
    }
    else
    {
      var propsWithoutThePromise = R.dissoc(promiseProp, this.props);
      return (
        <SuccessComponent {...propsWithoutThePromise} result={this.state.value} />
      );
    }
  }
};

export default ReactPromisedComponent;

{'fraction_non_alphanumeric': 0.14058679706601468, 'fraction_numerical': 0.0, 'mean_word_length': 3.0407407407407407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '5765236', 'n_tokens_mistral': 999, 'n_tokens_neox': 998, 'n_words': 206}