<commit_before>import {
  DEFAULT_PAGE_PROPS,
  DEFAULT_SEARCH_PROPS,
  setPageProps,
  setPagination,
  setSearchProps
} from "actions"
import autobind from "autobind-decorator"
import { setMessages } from "components/Messages"
import NotFound from "components/NotFound"
import _get from "lodash/get"
import _isEmpty from "lodash/isEmpty"
import _isEqualWith from "lodash/isEqualWith"
import PropTypes from "prop-types"
import React, { Component } from "react"
import { hideLoading, showLoading } from "react-redux-loading-bar"
import { animateScroll, Link } from "react-scroll"
import utils from "utils"

export const mapDispatchToProps = (dispatch, ownProps) => ({
  showLoading: () => dispatch(showLoading()),
  hideLoading: () => dispatch(hideLoading()),
  setPageProps: pageProps => dispatch(setPageProps(pageProps)),
  setSearchProps: searchProps => dispatch(setSearchProps(searchProps)),
  setPagination: (pageKey, pageNum) => dispatch(setPagination(pageKey, pageNum))
})

export const routerRelatedPropTypes = {
  location: PropTypes.object,
  history: PropTypes.object.isRequired
}

export const propTypes = {
  showLoading: PropTypes.func.isRequired,
  hideLoading: PropTypes.func.isRequired,
  setPageProps: PropTypes.func.isRequired,
  setSearchProps: PropTypes.func.isRequired,
  searchQuery: PropTypes.shape({
    text: PropTypes.string,
    filters: PropTypes.any,
    objectType: PropTypes.string
  }),
  /* eslint-disable react/no-unused-prop-types */
  /* FIXME: refactor setting the pagination, maybe use a container component */
  setPagination: PropTypes.func.isRequired,
  /* eslint-enable react/no-unused-prop-types */
  ...routerRelatedPropTypes
}

export const AnchorLink = function(props) {
  const { to, children, ...remainingProps } = props
  return (
    <Link
      to={to}
      smooth
      duration={500}
      containerId="main-viewport"
      {...remainingProps}
    >
      {props.children}
    </Link>
  )
}
AnchorLink.propTypes = {
  to: PropTypes.string,
  children: PropTypes.node
}

export function jumpToTop() {
  animateScroll.scrollToTop({
    duration: 500,
    delay: 100,
    smooth: "easeInOutQuint",
    containerId: "main-viewport"
  })
}

export default class Page extends Component {
  constructor(props, pageProps, searchProps) {
    super(props)
    const pp = _isEmpty(pageProps) ? DEFAULT_PAGE_PROPS : pageProps
    const sp = _isEmpty(searchProps) ? DEFAULT_SEARCH_PROPS : searchProps
    if (typeof props.setPageProps === "function") {
      props.setPageProps(Object.assign({}, pp))
    }
    if (typeof props.setSearchProps === "function") {
      props.setSearchProps(Object.assign({}, sp))
    }
    this.state = {
      notFound: false,
      invalidRequest: false,
      loadCount: null
    }

    this.renderPage = this.render
    this.render = Page.prototype.render
  }

  incrementRefCount = refCount => (refCount ? refCount + 1 : 1)

  decrementRefCount = refCount => (refCount ? refCount - 1 : 0)

  @autobind
  loadData() {
    this.setState({
      notFound: false,
      invalidRequest: false,
      loadCount: this.incrementRefCount(this.state.loadCount)
    })

    if (this.fetchData) {
      document.body.classList.add("loading")
      if (typeof this.props.showLoading === "function") {
        this.props.showLoading()
      }

      const promise = this.fetchData(this.props)

      if (promise && promise.then instanceof Function) {
        promise.then(this.doneLoading, this.doneLoading)
      } else {
        this.doneLoading()
      }

      return promise
    } else {
      this.doneLoading()
    }
  }

  @autobind
  doneLoading(response) {
    if (typeof this.props.hideLoading === "function") {
      this.props.hideLoading()
    }
    document.body.classList.remove("loading")

    if (response) {
      if (
        response.status === 404 ||
        (response.status === 500 &&
          _get(response, ["errors", 0]) === "Invalid Syntax")
      ) {
        this.setState({
          notFound: true,
          loadCount: this.decrementRefCount(this.state.loadCount)
        })
      } else if (response.status === 500) {
        this.setState({
          invalidRequest: true,
          loadCount: this.decrementRefCount(this.state.loadCount)
        })
      }
    } else {
      this.setState({ loadCount: this.decrementRefCount(this.state.loadCount) })
    }

    return response
  }

  render() {
    if (this.state.loadCount !== 0) {
      return null
    }
    if (this.state.notFound) {
      let modelName = this.constructor.modelName
      let text = modelName
        ? `${modelName} #${this.props.match.params.uuid}`<commit_msg>Remove obsolete 500 error handling<commit_after>import {
  DEFAULT_PAGE_PROPS,
  DEFAULT_SEARCH_PROPS,
  setPageProps,
  setPagination,
  setSearchProps
} from "actions"
import autobind from "autobind-decorator"
import { setMessages } from "components/Messages"
import NotFound from "components/NotFound"
import _isEmpty from "lodash/isEmpty"
import _isEqualWith from "lodash/isEqualWith"
import PropTypes from "prop-types"
import React, { Component } from "react"
import { hideLoading, showLoading } from "react-redux-loading-bar"
import { animateScroll, Link } from "react-scroll"
import utils from "utils"

export const mapDispatchToProps = (dispatch, ownProps) => ({
  showLoading: () => dispatch(showLoading()),
  hideLoading: () => dispatch(hideLoading()),
  setPageProps: pageProps => dispatch(setPageProps(pageProps)),
  setSearchProps: searchProps => dispatch(setSearchProps(searchProps)),
  setPagination: (pageKey, pageNum) => dispatch(setPagination(pageKey, pageNum))
})

export const routerRelatedPropTypes = {
  location: PropTypes.object,
  history: PropTypes.object.isRequired
}

export const propTypes = {
  showLoading: PropTypes.func.isRequired,
  hideLoading: PropTypes.func.isRequired,
  setPageProps: PropTypes.func.isRequired,
  setSearchProps: PropTypes.func.isRequired,
  searchQuery: PropTypes.shape({
    text: PropTypes.string,
    filters: PropTypes.any,
    objectType: PropTypes.string
  }),
  /* eslint-disable react/no-unused-prop-types */
  /* FIXME: refactor setting the pagination, maybe use a container component */
  setPagination: PropTypes.func.isRequired,
  /* eslint-enable react/no-unused-prop-types */
  ...routerRelatedPropTypes
}

export const AnchorLink = function(props) {
  const { to, children, ...remainingProps } = props
  return (
    <Link
      to={to}
      smooth
      duration={500}
      containerId="main-viewport"
      {...remainingProps}
    >
      {props.children}
    </Link>
  )
}
AnchorLink.propTypes = {
  to: PropTypes.string,
  children: PropTypes.node
}

export function jumpToTop() {
  animateScroll.scrollToTop({
    duration: 500,
    delay: 100,
    smooth: "easeInOutQuint",
    containerId: "main-viewport"
  })
}

export default class Page extends Component {
  constructor(props, pageProps, searchProps) {
    super(props)
    const pp = _isEmpty(pageProps) ? DEFAULT_PAGE_PROPS : pageProps
    const sp = _isEmpty(searchProps) ? DEFAULT_SEARCH_PROPS : searchProps
    if (typeof props.setPageProps === "function") {
      props.setPageProps(Object.assign({}, pp))
    }
    if (typeof props.setSearchProps === "function") {
      props.setSearchProps(Object.assign({}, sp))
    }
    this.state = {
      notFound: false,
      invalidRequest: false,
      loadCount: null
    }

    this.renderPage = this.render
    this.render = Page.prototype.render
  }

  incrementRefCount = refCount => (refCount ? refCount + 1 : 1)

  decrementRefCount = refCount => (refCount ? refCount - 1 : 0)

  @autobind
  loadData() {
    this.setState({
      notFound: false,
      invalidRequest: false,
      loadCount: this.incrementRefCount(this.state.loadCount)
    })

    if (this.fetchData) {
      document.body.classList.add("loading")
      if (typeof this.props.showLoading === "function") {
        this.props.showLoading()
      }

      const promise = this.fetchData(this.props)

      if (promise && promise.then instanceof Function) {
        promise.then(this.doneLoading, this.doneLoading)
      } else {
        this.doneLoading()
      }

      return promise
    } else {
      this.doneLoading()
    }
  }

  @autobind
  doneLoading(response) {
    if (typeof this.props.hideLoading === "function") {
      this.props.hideLoading()
    }
    document.body.classList.remove("loading")

    if (response) {
      if (response.status === 404) {
        this.setState({
          notFound: true,
          loadCount: this.decrementRefCount(this.state.loadCount)
        })
      } else if (response.status === 500) {
        this.setState({
          invalidRequest: true,
          loadCount: this.decrementRefCount(this.state.loadCount)
        })
      }
    } else {
      this.setState({ loadCount: this.decrementRefCount(this.state.loadCount) })
    }

    return response
  }

  render() {
    if (this.state.loadCount !== 0) {
      return null
    }
    if (this.state.notFound) {
      let modelName = this.constructor.modelName
      let text = modelName
        ? `${modelName} #${this.props.match.params.uuid}`
{'fraction_non_alphanumeric': 0.12010500984467294, 'fraction_numerical': 0.00514110697877926, 'mean_word_length': 3.186355311355311, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '493110', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2791, 'n_words': 586}