<commit_before>import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { push } from 'react-router-redux'

import ComicViewer from './ComicViewer'

import * as Actions from '../../actions'
import { App } from '../../constants'

class ComicViewerContainer extends React.Component {

  static propTypes = {
    comicId: PropTypes.number.isRequired,
    comic: PropTypes.object,
    episodeId: PropTypes.number.isRequired,
    episodes: PropTypes.object.isRequired,
    pages: PropTypes.object.isRequired,
    comicViewer: PropTypes.object.isRequired,
    dispatch: PropTypes.func.isRequired,
  }

  componentDidMount() {
    this.handleDataFetch()
  }

  componentWillUpdate(nextProps) {
    this.handleDataFetch(nextProps)
  }

  onPrevEpisodeClick = () => this.goNextEpisodeByOffset(-1)

  onNextEpisodeClick = () => this.goNextEpisodeByOffset(+1)

  onBackClick = () => {
    this.props.dispatch(push('/'))
  }

  onComicDrawerClick = () => {
    this.props.dispatch(Actions.updateComicDrawer(true, this.props.comicId))
  }

  handleDataFetch = (nextProps) => {
    const { comicId, comic, episodeId, episodes, pages, dispatch } =
          nextProps || this.props

    if (!comicId || !episodeId) return

    if (!nextProps && !comic) {
      this.props.dispatch(Actions.fetchComic(comicId))
    }

    if (comicId !== episodes.comicId) {
      dispatch(Actions.fetchEpisodes(comicId))
    }

    if (comicId !== pages.comicId || episodeId !== pages.episodeId) {
      dispatch(Actions.fetchPages(comicId, episodeId))
    }

    const episode = episodes.entries[episodeId]
    document.title = (comic && episode) ?
      `${comic.title} - ${episode.title} - ${App.title}` : App.title
  }

  goNextEpisodeByOffset = (offset) => {
    const { comicId, episodeId, episodes, dispatch } = this.props
    if (episodes.entries[episodeId + offset]) {
      dispatch(push(`/viewer?cid=${comicId}&eid=${episodeId + offset}`))
    }
  }

  render() {
    const { episodeId, episodes, pages } = this.props

    return (
      <ComicViewer
        pages={ pages.entries }
        episode={ episodes.entries[episodeId] }
        isFetching={ pages.isFetching }
        fetchError={ pages.fetchError }
        prevEpisode={ episodes.entries[episodeId - 1] }
        nextEpisode={ episodes.entries[episodeId + 1] }<commit_msg>Fixed wrong prev / next episode click navigation<commit_after>import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { push } from 'react-router-redux'

import ComicViewer from './ComicViewer'

import * as Actions from '../../actions'
import { App } from '../../constants'

class ComicViewerContainer extends React.Component {

  static propTypes = {
    comicId: PropTypes.number.isRequired,
    comic: PropTypes.object,
    episodeId: PropTypes.number.isRequired,
    episodes: PropTypes.object.isRequired,
    pages: PropTypes.object.isRequired,
    comicViewer: PropTypes.object.isRequired,
    dispatch: PropTypes.func.isRequired,
  }

  componentDidMount() {
    this.handleDataFetch()
  }

  componentWillUpdate(nextProps) {
    this.handleDataFetch(nextProps)
  }

  onBackClick = () => {
    this.props.dispatch(push('/'))
  }

  onComicDrawerClick = () => {
    this.props.dispatch(Actions.updateComicDrawer(true, this.props.comicId))
  }

  onPrevEpisodeClick = () => this.handleEpisodeNavigation(-1)

  onNextEpisodeClick = () => this.handleEpisodeNavigation(+1)

  getNextEpisodeIdByOffset = (offset) => {
    const { episodeId, episodes } = this.props
    const episodeKeys = Object.keys(episodes.entries)
    return episodeKeys[episodeKeys.indexOf(`${episodeId}`) + offset]
  }

  handleEpisodeNavigation = (offset) => {
    const { comicId, dispatch } = this.props
    const nextEpisodeId = this.getNextEpisodeIdByOffset(offset)
    if (nextEpisodeId) {
      dispatch(push(`/viewer?cid=${comicId}&eid=${nextEpisodeId}`))
    }
  }

  handleDataFetch = (nextProps) => {
    const { comicId, comic, episodeId, episodes, pages, dispatch } =
          nextProps || this.props

    if (!comicId || !episodeId) return

    if (!nextProps && !comic) {
      this.props.dispatch(Actions.fetchComic(comicId))
    }

    if (comicId !== episodes.comicId) {
      dispatch(Actions.fetchEpisodes(comicId))
    }

    if (comicId !== pages.comicId || episodeId !== pages.episodeId) {
      dispatch(Actions.fetchPages(comicId, episodeId))
    }

    const episode = episodes.entries[episodeId]
    document.title = (comic && episode) ?
      `${comic.title} - ${episode.title} - ${App.title}` : App.title
  }

  render() {
    const { episodeId, episodes: { entries: episodes }, pages } = this.props

    return (
      <ComicViewer
        pages={ pages.entries }
        episode={ episodes[episodeId] }
        isFetching={ pages.isFetching }
        fetchError={ pages.fetchError }
        prevEpisode={ episodes[this.getNextEpisodeIdByOffset(-1)] }
        nextEpisode={ episodes[this.getNextEpisodeIdByOffset(+1)] }
{'fraction_non_alphanumeric': 0.13588152891735042, 'fraction_numerical': 0.0016009605763458075, 'mean_word_length': 3.4151943462897525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2269441', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1593, 'n_words': 285}