<commit_before>React = require 'react'
BS = require 'react-bootstrap'
_ = require 'underscore'

RefreshButton = require './refresh-button'

module.exports = React.createClass
  displayName: 'AsyncButton'

  propTypes:
    isWaiting: React.PropTypes.bool.isRequired
    isDone: React.PropTypes.bool
    isFailed: React.PropTypes.bool
    waitingText: React.PropTypes.node # TODO: This should be a Component or array
    failedState: React.PropTypes.func
    failedProps: React.PropTypes.object
    doneText: React.PropTypes.node
    isJob: React.PropTypes.bool
    timeoutLength: React.PropTypes.number

  getInitialState: ->
    isTimedout: false

  componentDidUpdate: ->
    {isWaiting, isJob} = @props
    {isTimedout} = @state

    timeout = @props.timeoutLength or if isJob then 600000 else 30000

    if isWaiting and not isTimedout
      _.delay @checkForTimeout, timeout

  checkForTimeout: ->
    {isWaiting} = @props
    @setState(isTimedout: true) if isWaiting and @isMounted()

  getDefaultProps: ->
    isDone: false
    isFailed: false
    waitingText: 'Loading…'
    failedState: RefreshButton
    failedProps:
      beforeText: 'There was a problem.  '
    doneText: ''
    isJob: false

  render: ->
    {className, disabled} = @props
    {isWaiting, isDone, isFailed} = @props
    {children, waitingText, failedState, failedProps, doneText} = @props
    {isTimedout} = @state

    buttonTypeClass = 'async-button'

    if isFailed or isTimedout
      stateClass = 'is-failed'
      return React.createElement(failedState, failedProps)
    else if isWaiting
      stateClass = 'is-waiting'
      text = waitingText
      disabled = true
      spinner = <i className='fa fa-spinner fa-spin'/>
    else if isDone
      stateClass = 'is-done'
      text = doneText
    else
      stateClass = null
      text = children

    <BS.Button {...@props}
      className={[buttonTypeClass, stateClass, className]}
      disabled={disabled}
      >
        {spinner}
        {text}
    </BS.Button><commit_msg>Use JSX for rendering FailedState<commit_after>React = require 'react'
BS = require 'react-bootstrap'
_ = require 'underscore'

RefreshButton = require './refresh-button'

module.exports = React.createClass
  displayName: 'AsyncButton'

  propTypes:
    isWaiting: React.PropTypes.bool.isRequired
    isDone: React.PropTypes.bool
    isFailed: React.PropTypes.bool
    waitingText: React.PropTypes.node # TODO: This should be a Component or array
    failedState: React.PropTypes.func
    failedProps: React.PropTypes.object
    doneText: React.PropTypes.node
    isJob: React.PropTypes.bool
    timeoutLength: React.PropTypes.number

  getInitialState: ->
    isTimedout: false

  componentDidUpdate: ->
    {isWaiting, isJob} = @props
    {isTimedout} = @state

    timeout = @props.timeoutLength or if isJob then 600000 else 30000

    if isWaiting and not isTimedout
      _.delay @checkForTimeout, timeout

  checkForTimeout: ->
    {isWaiting} = @props
    @setState(isTimedout: true) if isWaiting and @isMounted()

  getDefaultProps: ->
    isDone: false
    isFailed: false
    waitingText: 'Loading…'
    failedState: RefreshButton
    failedProps:
      beforeText: 'There was a problem.  '
    doneText: ''
    isJob: false

  render: ->
    {className, disabled} = @props
    {isWaiting, isDone, isFailed} = @props
    {children, waitingText, failedProps, doneText} = @props
    {isTimedout} = @state
    # needs to be capitalized so JSX will transpile as a variable, not element
    FailedState = @props.failedState

    buttonTypeClass = 'async-button'

    if isFailed or isTimedout
      stateClass = 'is-failed'
      return <FailedState {...failedProps} />
    else if isWaiting
      stateClass = 'is-waiting'
      text = waitingText
      disabled = true
      spinner = <i className='fa fa-spinner fa-spin'/>
    else if isDone
      stateClass = 'is-done'
      text = doneText
    else
      stateClass = null
      text = children

    <BS.Button {...@props}
      className={[buttonTypeClass, stateClass, className]}
      disabled={disabled}
      >
        {spinner}
        {text}
    </BS.Button>
{'fraction_non_alphanumeric': 0.0975374215354901, 'fraction_numerical': 0.005311443746982134, 'mean_word_length': 2.9608030592734225, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, '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': '660272', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1265, 'n_words': 331}