<commit_before>// lists of links at the bottom of the sidebar

import '../../../css/browser/side-bar-contextual-lists'

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { FormattedMessage as T, intlShape } from 'react-intl'
import cx from 'classnames'

import {
  fetchMostLinked,
  fetchParents,
  fetchSubs,
  selectContextualList
} from '../../../actions/contextual-lists'

class _List extends React.Component {
  render () {
    const { formatMessage } = this.context.intl
    const { links } = this.props
    return (
      <div className="browser-side-bar-contextual-list">
        <ul>
          { links.map(link =>
            <li key={ link.url }>
              <div className="link-name">
                { formatMessage({ id: 'linked' }) }
                <span className="link-linked">{ link.linked }</span>
                { formatMessage({ id: 'times' }) }
              </div>
              <div className="link-url">{ link.url }</div>
            </li>
          ) }
        </ul>
      </div>
    )
  }
}

_List.contextTypes = {
  intl: intlShape
}

_List.propTypes = {
  links: PropTypes.array
}

const List = connect(({ intl: { locale } }) => ({ locale }))(_List)


class SideBarContextualLists extends React.Component {
  componentDidMount () {
    this.updateCurrentList()
  }

  componentWillReceiveProps (props) {
    // change in nav
    if (props.selected !== this.props.selected)
      this.updateCurrentList(props.selected)
    // change of list content
    if (JSON.stringify(props[props.selected]) !== JSON.stringify(this.props[props.selected]))
      this.updateCurrentList(props.selected)
  }

  updateCurrentList (selected) {
    const { serverUrl, corpusId, webentity,
      fetchMostLinked, fetchParents, fetchSubs } = this.props

    // TODO DRY
    switch (selected) {
    case 'mostLinked':
      fetchMostLinked(serverUrl, corpusId, webentity.id)
      break

    case 'parents':
      fetchParents(serverUrl, corpusId, webentity.id)
      break

    case 'subs':
      fetchSubs(serverUrl, corpusId, webentity.id)
      break
    }
  }

  render () {
    const { selectContextualList, selected } = this.props

    return (
      <div className="browser-side-bar-contextual-lists">
        <nav>
          { ['mostLinked', 'parents', 'subs'].map(l =>
            <button className={ cx('btn', 'btn-default', { selected: l === selected }) }
              key={ l } onClick={ () => selectContextualList(l) }>
              <T id={ `sidebar.contextual.${l}` } />
            </button>
          ) }<commit_msg>fix(SideBarContextualLists): load most linked at creation<commit_after>// lists of links at the bottom of the sidebar

import '../../../css/browser/side-bar-contextual-lists'

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { FormattedMessage as T, intlShape } from 'react-intl'
import cx from 'classnames'

import {
  fetchMostLinked,
  fetchParents,
  fetchSubs,
  selectContextualList
} from '../../../actions/contextual-lists'

class _List extends React.Component {
  render () {
    const { formatMessage } = this.context.intl
    const { links } = this.props
    return (
      <div className="browser-side-bar-contextual-list">
        <ul>
          { links.map(link =>
            <li key={ link.url }>
              <div className="link-name">
                { formatMessage({ id: 'linked' }) }
                <span className="link-linked">{ link.linked }</span>
                { formatMessage({ id: 'times' }) }
              </div>
              <div className="link-url">{ link.url }</div>
            </li>
          ) }
        </ul>
      </div>
    )
  }
}

_List.contextTypes = {
  intl: intlShape
}

_List.propTypes = {
  links: PropTypes.array
}

const List = connect(({ intl: { locale } }) => ({ locale }))(_List)


class SideBarContextualLists extends React.Component {
  componentDidMount () {
    this.updateCurrentList()
  }

  componentWillReceiveProps (props) {
    // change in nav
    if (props.selected !== this.props.selected)
      this.updateCurrentList(props.selected)
    // change of list content
    if (JSON.stringify(props[props.selected]) !== JSON.stringify(this.props[props.selected]))
      this.updateCurrentList(props.selected)
  }

  updateCurrentList (selected) {
    const { serverUrl, corpusId, webentity,
      fetchMostLinked, fetchParents, fetchSubs } = this.props

    // TODO DRY
    switch (selected) {
    case 'parents':
      fetchParents(serverUrl, corpusId, webentity.id)
      break

    case 'subs':
      fetchSubs(serverUrl, corpusId, webentity.id)
      break

    default:
      fetchMostLinked(serverUrl, corpusId, webentity.id)
      break
    }
  }

  render () {
    const { selectContextualList, selected } = this.props

    return (
      <div className="browser-side-bar-contextual-lists">
        <nav>
          { ['mostLinked', 'parents', 'subs'].map(l =>
            <button className={ cx('btn', 'btn-default', { selected: l === selected }) }
              key={ l } onClick={ () => selectContextualList(l) }>
              <T id={ `sidebar.contextual.${l}` } />
            </button>
          ) }
{'fraction_non_alphanumeric': 0.14140053866871874, 'fraction_numerical': 0.0, 'mean_word_length': 2.4002616088947026, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, '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': '10895', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1559, 'n_words': 350}