<commit_before>import React from 'react'
import { withApollo } from 'react-apollo'
import { List } from 'immutable'

export const ListableHOC = ({
  // [gql]: Query carregamento dos dados.
  query,
  // [object | function]: Parametros utilizados na query caso exista. Se
  // função recebe `ownProps` e deve retornar um `object`.
  queryParams,
  // [integer]: Linhas por página
  limit,
  // [string]: Nome da query utilizada. É usado para mapear as propriedades
  // do componente envovlido.
  queryName,
  parse,
  handleError
}) => (WrappedComponent) => {
  class PP extends React.Component {
    constructor (props) {
      super(props)
      this.state = {
        loading: false,
        data: [],
        totalCount: 0,
        page: 0
      }
    }

    getPaginationParams () {
      return {
        first: limit,
        offset: this.state.page * limit
      } 
    }

    fetch () {
      this.setState({ loading: true })
      this.props.client.query({
        query,
        variables: {
          ...this.getPaginationParams(),
          ...queryParams(this.props)
        }
      }).then(({ data }) => {
        const all = data[queryName] ? data[queryName].nodes : []
        this.setState({
          loading: data.loading,
          data: all.map(parse),
          totalCount: data[queryName] ? data[queryName].totalCount : 0,
        })
      }).catch((err) => {
        this.setState({ loading: false })
        const handle = handleError || this.props.listableHandleError
        handle(err)
      })
    }

    handleNextPage () {
      this.setState({ page: this.state.page + 1 }, this.fetch)
    }

    handlePreviousPage () {
      if (this.state.page > 0) {
        this.setState({ page: this.state.page - 1 }, this.fetch)
      }
    }

    render () {
      return (
        <WrappedComponent
          {...this.props}
          // Fetching
          fetch={this.fetch.bind(this)}
          loading={this.state.loading}
          data={this.state.data}
          totalCount={this.state.totalCount}
          // Pagination
          indexPage={(this.state.page + 1)}
          lastPage={(parseInt(this.state.totalCount / limit) + 1)}
          onNextPage={this.handleNextPage.bind(this)}
          onPreviousPage={this.handlePreviousPage.bind(this)}
        />
      )
    }
  }
  
  return withApollo(PP)
}

export const SelectableHOC = ({
  // [gql]: Query carregamento dos dados.
  query,<commit_msg>chore(datagrid): fix pagination for when failed fetch #815<commit_after>import React from 'react'
import { withApollo } from 'react-apollo'
import { List } from 'immutable'

export const ListableHOC = ({
  // [gql]: Query carregamento dos dados.
  query,
  // [object | function]: Parametros utilizados na query caso exista. Se
  // função recebe `ownProps` e deve retornar um `object`.
  queryParams,
  // [integer]: Linhas por página
  limit,
  // [string]: Nome da query utilizada. É usado para mapear as propriedades
  // do componente envovlido.
  queryName,
  parse,
  handleError
}) => (WrappedComponent) => {
  class PP extends React.Component {
    constructor (props) {
      super(props)
      this.state = {
        loading: false,
        data: [],
        totalCount: 0,
        page: 0
      }
    }

    getPaginationParams () {
      return {
        first: limit,
        offset: this.state.page * limit
      } 
    }

    fetch () {
      this.setState({ loading: true })
      return this.props.client.query({
        query,
        variables: {
          ...this.getPaginationParams(),
          ...queryParams(this.props)
        }
      }).then(({ data }) => {
        const all = data[queryName] ? data[queryName].nodes : []
        this.setState({
          loading: data.loading,
          data: all.map(parse),
          totalCount: data[queryName] ? data[queryName].totalCount : 0,
        })
        return Promise.resolve()
      }).catch((err) => {
        this.setState({ loading: false })
        const handle = handleError || this.props.listableHandleError
        handle(err)
        return Promise.reject(err)
      })
    }

    handleNextPage () {
      this.setState({ page: this.state.page + 1 }, () => {
        this.fetch()
          .catch(err => {
            this.setState({ page: this.state.page - 1 })
          })
      })
    }

    handlePreviousPage () {
      if (this.state.page > 0) {
        this.setState({ page: this.state.page - 1, action: 'previous' }, () => {
          this.fetch()
            .catch(err => {
              this.setState({ page: this.state.page + 1 })
            })
        })
      }
    }

    render () {
      return (
        <WrappedComponent
          {...this.props}
          // Fetching
          fetch={this.fetch.bind(this)}
          loading={this.state.loading}
          data={this.state.data}
          totalCount={this.state.totalCount}
          // Pagination
          indexPage={(this.state.page + 1)}
          lastPage={(parseInt(this.state.totalCount / limit) + 1)}
          onNextPage={this.handleNextPage.bind(this)}
          onPreviousPage={this.handlePreviousPage.bind(this)}
        />
      )
    }
  }
  
  return withApollo(PP)
}

export const SelectableHOC = ({
  // [gql]: Query carregamento dos dados.
  query,
{'fraction_non_alphanumeric': 0.13427158615443135, 'fraction_numerical': 0.00399391403575504, 'mean_word_length': 1.931438127090301, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3020239', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1603, 'n_words': 343}