<commit_before>React = require 'react'
classnames = require 'classnames'
{SmartOverflow} = require 'openstax-react-components'

{Task} = require '../task'
UserStatus = require '../user/status'
UserLoginButton = require '../user/login-button'
UserLogin = require '../user/login'

{ExerciseStep} = require '../exercise'

User = require '../user/model'

{channel} = require './model'

ConceptCoach = React.createClass
  displayName: 'ConceptCoach'

  propTypes:
    close: React.PropTypes.func

  getInitialState: ->
    isLoggedIn: User.isLoggedIn()
    displayLogin: false
    isLoaded: false

  onAttemptLogin: ->
    @setState(displayLogin: true)

  onLoginComplete: ->
    @setState(displayLogin: false)

  componentWillMount: ->
    User.ensureStatusLoaded()

  componentDidMount: ->
    mountData = coach: {el: @getDOMNode(), action: 'mount'}
    channel.emit('coach.mount.success', mountData)
    User.channel.on('change', @update)

  componentWillUnmount: ->
    mountData = coach: {el: @getDOMNode(), action: 'unmount'}
    channel.emit('coach.unmount.success', mountData)
    User.channel.off('change', @update)

  update: ->
    @setState(isLoggedIn: User.isLoggedIn(), isLoaded: User.loaded)

  render: ->
    {isLoaded, isLoggedIn, displayLogin} = @state

    if isLoggedIn
      coach = <Task {...@props} key='task'/>
    else if displayLogin
      coach = <UserLogin onComplete={@onLoginComplete} />
    else if isLoaded
      coach = <UserLoginButton onAttemptLogin={@onAttemptLogin} key='user-login-button'/>
    else
      coach = 'Loading...'

    className = classnames 'concept-coach-view',
      loading: not (isLoggedIn or isLoaded)

    <div className='concept-coach'>
      <UserStatus key='user-status' close={@props.close}/>
      <div className={className}>
        {coach}
      </div>
    </div>

module.exports = {ConceptCoach, channel}<commit_msg>Add course registration, simplfy logic<commit_after>React = require 'react'
classnames = require 'classnames'
{SmartOverflow} = require 'openstax-react-components'

{Task} = require '../task'
UserStatus = require '../user/status'
UserLoginButton = require '../user/login-button'
UserLogin = require '../user/login'

{ExerciseStep} = require '../exercise'

CourseRegistration = require '../course/registration'
User = require '../user/model'

{channel} = require './model'

ConceptCoach = React.createClass
  displayName: 'ConceptCoach'

  propTypes:
    close:          React.PropTypes.func
    moduleUUID:     React.PropTypes.string.isRequired
    collectionUUID: React.PropTypes.string.isRequired

  getInitialState: ->
    isLoggedIn: User.isLoggedIn()
    displayLogin: false
    isLoaded: User.isLoaded

  onAttemptLogin: ->
    @setState(displayLogin: true)

  onLoginComplete: ->
    @setState(displayLogin: false)

  componentWillMount: ->
    User.ensureStatusLoaded()

  componentDidMount: ->
    mountData = coach: {el: @getDOMNode(), action: 'mount'}
    channel.emit('coach.mount.success', mountData)
    User.channel.on('change', @update)

  componentWillUnmount: ->
    mountData = coach: {el: @getDOMNode(), action: 'unmount'}
    channel.emit('coach.unmount.success', mountData)
    User.channel.off('change', @update)

  update: ->
    course = User.getCourse(@props.collectionUUID)
    @setState(
      isLoggedIn: User.isLoggedIn(),
      isLoaded: User.isLoaded,
      isRegistered: course?.isRegistered()
    )

  childComponent: ->
    {isLoaded, isRegistered, isLoggedIn, displayLogin} = @state
    if not isLoaded
      <span><i className='fa fa-spinner fa-spin'/> Loading ...</span>
    else if not isLoggedIn
      <UserLogin onComplete={@onLoginComplete} />
    else if not isRegistered
      <CourseRegistration {...@props} onComplete={@update} />
    else
      <Task {...@props} key='task'/>

  render: ->
    {isLoaded, isLoggedIn} = @state

    className = classnames 'concept-coach-view',
      loading: not (isLoggedIn or isLoaded)

    <div className='concept-coach'>
      <UserStatus key='user-status' close={@props.close}/>
      <div className={className}>
        {@childComponent()}
      </div>
    </div>

module.exports = {ConceptCoach, channel}
{'fraction_non_alphanumeric': 0.1437485001199904, 'fraction_numerical': 0.0, 'mean_word_length': 3.5353645266594125, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 39, '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': '93521', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1296, 'n_words': 278}