<commit_before>import React, {Component, PropTypes} from 'react'
import ReactDOM from 'react-dom'
import Card from '../containers/Card'
import smoothscroll from 'smoothscroll'
import {connect} from 'react-redux'
import {openCard, closeCard} from '../../actions/cards'
import * as ImdiPropTypes from '../proptypes/ImdiPropTypes'

function last(arr) {
  return arr[arr.length - 1]
}

class CardList extends Component {
  static propTypes = {
    dispatch: PropTypes.func,
    cards: PropTypes.arrayOf(ImdiPropTypes.card),
    cardsPage: ImdiPropTypes.cardsPage,
    region: ImdiPropTypes.region,
    openCards: PropTypes.arrayOf(PropTypes.string)
  };

  static contextTypes = {
    linkTo: PropTypes.func,
    goTo: PropTypes.func,
    navigate: PropTypes.func
  };

  componentDidMount() {
    this.maybeScrollToOpenCard()
  }

  componentDidUpdate(prevProps) {
    if (last(prevProps.openCards) !== last(this.props.openCards)) {
      this.maybeScrollToOpenCard()
    }
  }

  maybeScrollToOpenCard() {
    // Make sure the last opened card is always scrolled into view
    const {openCards} = this.props

    const lastCard = last(openCards)
    if (!lastCard) {
      return
    }

    const container = ReactDOM.findDOMNode(this.refs[`card-${lastCard}`])
    smoothscroll(container, 500)
    container.focus()
  }

  renderToggleCardLink(card) {
    const {openCards, dispatch} = this.props
    const {navigate, linkTo} = this.context

    const isOpen = openCards.includes(card.name)
    const prevCard = openCards[openCards.length - 2]

    const handleClick = event => {
      event.preventDefault()
      event.nativeEvent.stopImmediatePropagation()
      const link = isOpen
        ? linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: prevCard || ''})
        : linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: card.name})

      navigate(link, {keepScrollPosition: true})

      if (isOpen) {
        dispatch(closeCard(card.name))
      } else {
        this.props.dispatch(openCard(card.name))
      }
    }
    return (
      <a href={this.context.linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: card.name})}
        onClick={handleClick}
        className={`toggle-list__button ${card.noValues ? 'toggle-list__button--disablet' : ''} ${isOpen ? 'toggle-list__button--expanded' : ''}`}
        aria-expanded={isOpen ? 'true' : 'false'}<commit_msg>Let router handle open card action<commit_after>import React, {Component, PropTypes} from 'react'
import ReactDOM from 'react-dom'
import Card from '../containers/Card'
import smoothscroll from 'smoothscroll'
import {connect} from 'react-redux'
import {openCard, closeCard} from '../../actions/cards'
import * as ImdiPropTypes from '../proptypes/ImdiPropTypes'

function last(arr) {
  return arr[arr.length - 1]
}

class CardList extends Component {
  static propTypes = {
    dispatch: PropTypes.func,
    cards: PropTypes.arrayOf(ImdiPropTypes.card),
    cardsPage: ImdiPropTypes.cardsPage,
    region: ImdiPropTypes.region,
    openCards: PropTypes.arrayOf(PropTypes.string)
  };

  static contextTypes = {
    linkTo: PropTypes.func,
    goTo: PropTypes.func,
    navigate: PropTypes.func
  };

  componentDidMount() {
    this.maybeScrollToOpenCard()
  }

  componentDidUpdate(prevProps) {
    if (last(prevProps.openCards) !== last(this.props.openCards)) {
      this.maybeScrollToOpenCard()
    }
  }

  maybeScrollToOpenCard() {
    // Make sure the last opened card is always scrolled into view
    const {openCards} = this.props

    const lastCard = last(openCards)
    if (!lastCard) {
      return
    }

    const container = ReactDOM.findDOMNode(this.refs[`card-${lastCard}`])
    smoothscroll(container, 500)
    container.focus()
  }

  renderToggleCardLink(card) {
    const {openCards, dispatch} = this.props
    const {navigate, linkTo} = this.context

    const isOpen = openCards.includes(card.name)
    const prevCard = openCards[openCards.length - 2]

    const handleClick = event => {
      event.preventDefault()
      event.nativeEvent.stopImmediatePropagation()
      const link = isOpen
        ? linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: prevCard || ''})
        : linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: card.name})

      navigate(link, {keepScrollPosition: true})

      if (isOpen) {
        dispatch(closeCard(card.name))
      } else {
        // This is taken care of by the router which will dispatch an open card action
      }
    }
    return (
      <a href={this.context.linkTo('/tall-og-statistikk/steder/:region/:cardsPageName/:cardName', {cardName: card.name})}
        onClick={handleClick}
        className={`toggle-list__button ${card.noValues ? 'toggle-list__button--disablet' : ''} ${isOpen ? 'toggle-list__button--expanded' : ''}`}
        aria-expanded={isOpen ? 'true' : 'false'}
{'fraction_non_alphanumeric': 0.13055723194528263, 'fraction_numerical': 0.0020116676725005027, 'mean_word_length': 3.4792792792792793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2117439', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1580, 'n_words': 312}