<commit_before>React = require 'react'
BS = require 'react-bootstrap'
LoadableItem = require './loadable-item'
_ = require 'underscore'
camelCase = require 'camelcase'

{CourseActions, CourseStore} = require '../flux/course'
PeriodHelper = require '../helpers/period'

CoursePeriodsNav = React.createClass
  displayName: 'CoursePeriodsNav'

  propTypes:
    courseId: React.PropTypes.string.isRequired
    handleSelect: React.PropTypes.func
    handleKeyUpdate: React.PropTypes.func
    initialActive: React.PropTypes.number.isRequired
    periods: React.PropTypes.array.isRequired

  getDefaultProps: ->
    initialActive: 0
    sortedPeriods: []

  getInitialState: ->
    active: @props.initialActive

  componentWillMount: ->
    @setSortedPeriods(@props.periods)
    CourseStore.on('course.loaded', @selectPeriod)

  componentWillUnmount: ->
    CourseStore.off('course.loaded', @selectPeriod)

  componentWillReceiveProps: (nextProps) ->
    @setSortedPeriods(nextProps.periods)

  setSortedPeriods: (periods) ->
    # make sure that periods are sorted by name
    sortedPeriods = PeriodHelper.sort(periods)
    @setState(sortedPeriods: sortedPeriods)

  selectPeriod: (courseId) ->
    if courseId is @props.courseId
      @onSelect(@state.active)

  onSelect: (key) ->
    {courseId, handleSelect, handleKeyUpdate} = @props
    {sortedPeriods} = @state

    period = sortedPeriods?[key]
    unless period?
      throw new Error("BUG: #{key} period does not exist for course #{courseId}. There are only #{periods.length}.")
      return

    handleSelect?(period)
    handleKeyUpdate?(key)
    @setState(active: key)

  renderPeriod: (period, key) ->
    <BS.NavItem className={'is-trouble' if period.is_trouble} eventKey={key} key="period-nav-#{period.id}">
      {period.name}
    </BS.NavItem>

  render: ->
    {active, sortedPeriods} = @state<commit_msg>shortened line<commit_after>React = require 'react'
BS = require 'react-bootstrap'
LoadableItem = require './loadable-item'
_ = require 'underscore'
camelCase = require 'camelcase'

{CourseActions, CourseStore} = require '../flux/course'
PeriodHelper = require '../helpers/period'

CoursePeriodsNav = React.createClass
  displayName: 'CoursePeriodsNav'

  propTypes:
    courseId: React.PropTypes.string.isRequired
    handleSelect: React.PropTypes.func
    handleKeyUpdate: React.PropTypes.func
    initialActive: React.PropTypes.number.isRequired
    periods: React.PropTypes.array.isRequired

  getDefaultProps: ->
    initialActive: 0
    sortedPeriods: []

  getInitialState: ->
    active: @props.initialActive

  componentWillMount: ->
    @setSortedPeriods(@props.periods)
    CourseStore.on('course.loaded', @selectPeriod)

  componentWillUnmount: ->
    CourseStore.off('course.loaded', @selectPeriod)

  componentWillReceiveProps: (nextProps) ->
    @setSortedPeriods(nextProps.periods)

  setSortedPeriods: (periods) ->
    # make sure that periods are sorted by name
    sortedPeriods = PeriodHelper.sort(periods)
    @setState(sortedPeriods: sortedPeriods)

  selectPeriod: (courseId) ->
    if courseId is @props.courseId
      @onSelect(@state.active)

  onSelect: (key) ->
    {courseId, handleSelect, handleKeyUpdate} = @props
    {sortedPeriods} = @state

    period = sortedPeriods?[key]
    unless period?
      throw new Error("BUG: #{key} period does not exist for course #{courseId}. There are only #{periods.length}.")
      return

    handleSelect?(period)
    handleKeyUpdate?(key)
    @setState(active: key)

  renderPeriod: (period, key) ->
    <BS.NavItem 
      className={'is-trouble' if period.is_trouble} 
      eventKey={key} 
      key="period-nav-#{period.id}">
      {period.name}
    </BS.NavItem>

  render: ->
    {active, sortedPeriods} = @state
{'fraction_non_alphanumeric': 0.12147043153969099, 'fraction_numerical': 0.0005327650506126798, 'mean_word_length': 3.9084967320261437, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '2963385', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1166, 'n_words': 242}