<commit_before>React = require 'react'
BoardPreview = require './board-preview'
talkClient = require '../api/talk'
authClient = require '../api/auth'
ChangeListener = require '../components/change-listener'
PromiseRenderer = require '../components/promise-renderer'
Moderation = require './lib/moderation'
ChangeListener = require '../components/change-listener'
PromiseRenderer = require '../components/promise-renderer'
ProjectLinker = require './lib/project-linker'
ROLES = require './lib/roles'
auth = require '../api/auth'
{Link} = require 'react-router'

module?.exports = React.createClass
  displayName: 'TalkInit'

  getInitialState: ->
    boards: []

  propTypes:
    section: React.PropTypes.string # 'zooniverse' for main-talk, 'project_id' for projects

  componentWillMount: ->
    @setBoards()

  componentWillReceiveProps: ->
    @setBoards()

  setBoards: ->
    auth.checkCurrent().then =>
      talkClient.type('boards').get(section: @props.section)
        .then (boards) =>
          @setState {boards}

  onSubmitBoard: (e) ->
    e.preventDefault()
    titleInput = @getDOMNode().querySelector('form input')
    descriptionInput = @getDOMNode().querySelector('form textarea')

    # permissions
    read = @getDOMNode().querySelector(".roles-read input[name='role-read']:checked").value
    write = @getDOMNode().querySelector(".roles-write input[name='role-write']:checked").value
    permissions = {read, write}

    title = titleInput.value
    description = descriptionInput.value
    section = @props.section

    board = {title, description, section, permissions}
    return console.log "failed validation" unless title and description and section

    talkClient.type('boards').create(board).save()
      .then (board) =>
        titleInput.value = ''
        descriptionInput.value = ''
        @setBoards()

  boardPreview: (data, i) ->
    <BoardPreview {...@props} key={i} data={data} />

  tag: (t, i) ->
    <span><Link key={i} query={query: t.name} to="talk-search">#{t.name}</Link>{' '}</span>

  roleReadLabel: (data, i) ->
    <label key={i}><input type="radio" name="role-read" defaultChecked={i is ROLES.length-1} value={data}/>{data}</label>

  roleWriteLabel: (data, i) ->
    <label key={i}><input type="radio" name="role-write" defaultChecked={i is ROLES.length-1}value={data}/>{data}</label>

  render: ->
    <div className="talk-home">
      <Moderation section={@props.section}>
        <form onSubmit={@onSubmitBoard}>
          <h2>Moderator Zone:</h2>
          <h3>Add a board:</h3>
          <input type="text" ref="boardTitle" placeholder="Board Title"/>

          <textarea ref="boardDescription" placeholder="Board Description"></textarea><br />

          <h4>Can Read:</h4>
          <div className="roles-read">{ROLES.map(@roleReadLabel)}</div>

          <h4>Can Write:</h4>
          <div className="roles-write">{ROLES.map(@roleWriteLabel)}</div>

          <button type="submit"><i className="fa fa-plus-circle" /> Create Board</button>
        </form>
      </Moderation>

      <div className="talk-list-content">
        <section>
          {if @state.boards.length
            @state.boards.map(@boardPreview)
           else
            <p>There are currently no boards.</p>}
        </section>

        <div className="talk-sidebar">
          <h2>Talk Sidebar</h2>

          <h3>Jump to a project</h3>
          <ProjectLinker />

          <PromiseRenderer promise={talkClient.type('tags').get(section: @props.section)}>{(tags) =>
            if tags.length
              <section>
                <h3>Latest Tags:</h3>
                {tags.map(@tag)}
              </section>
          }</PromiseRenderer>
        </div>
      </div>
    </div>
<commit_msg>Add loading / empty state to talk boards page<commit_after>React = require 'react'
BoardPreview = require './board-preview'
talkClient = require '../api/talk'
authClient = require '../api/auth'
ChangeListener = require '../components/change-listener'
PromiseRenderer = require '../components/promise-renderer'
Moderation = require './lib/moderation'
ChangeListener = require '../components/change-listener'
PromiseRenderer = require '../components/promise-renderer'
ProjectLinker = require './lib/project-linker'
ROLES = require './lib/roles'
auth = require '../api/auth'
{Link} = require 'react-router'
Loading = require '../components/loading-indicator'

module?.exports = React.createClass
  displayName: 'TalkInit'

  getInitialState: ->
    boards: []
    loading: true

  propTypes:
    section: React.PropTypes.string # 'zooniverse' for main-talk, 'project_id' for projects

  componentWillMount: ->
    @setBoards()

  componentWillReceiveProps: ->
    @setBoards()

  setBoards: ->
    auth.checkCurrent().then =>
      talkClient.type('boards').get(section: @props.section)
        .then (boards) =>
          @setState {boards, loading: false}

  onSubmitBoard: (e) ->
    e.preventDefault()
    titleInput = @getDOMNode().querySelector('form input')
    descriptionInput = @getDOMNode().querySelector('form textarea')

    # permissions
    read = @getDOMNode().querySelector(".roles-read input[name='role-read']:checked").value
    write = @getDOMNode().querySelector(".roles-write input[name='role-write']:checked").value
    permissions = {read, write}

    title = titleInput.value
    description = descriptionInput.value
    section = @props.section

    board = {title, description, section, permissions}
    return console.log "failed validation" unless title and description and section

    talkClient.type('boards').create(board).save()
      .then (board) =>
        titleInput.value = ''
        descriptionInput.value = ''
        @setBoards()

  boardPreview: (data, i) ->
    <BoardPreview {...@props} key={i} data={data} />

  tag: (t, i) ->
    <span><Link key={i} query={query: t.name} to="talk-search">#{t.name}</Link>{' '}</span>

  roleReadLabel: (data, i) ->
    <label key={i}><input type="radio" name="role-read" defaultChecked={i is ROLES.length-1} value={data}/>{data}</label>

  roleWriteLabel: (data, i) ->
    <label key={i}><input type="radio" name="role-write" defaultChecked={i is ROLES.length-1}value={data}/>{data}</label>

  render: ->
    <div className="talk-home">
      <Moderation section={@props.section}>
        <form onSubmit={@onSubmitBoard}>
          <h2>Moderator Zone:</h2>
          <h3>Add a board:</h3>
          <input type="text" ref="boardTitle" placeholder="Board Title"/>

          <textarea ref="boardDescription" placeholder="Board Description"></textarea><br />

          <h4>Can Read:</h4>
          <div className="roles-read">{ROLES.map(@roleReadLabel)}</div>

          <h4>Can Write:</h4>
          <div className="roles-write">{ROLES.map(@roleWriteLabel)}</div>

          <button type="submit"><i className="fa fa-plus-circle" /> Create Board</button>
        </form>
      </Moderation>

      <div className="talk-list-content">
        <section>
          {if @state.loading
            <Loading />
           else if @state.boards?.length is 0
            <p>There are currently no boards.</p>
           else if @state.boards?.length
             @state.boards.map(@boardPreview)}
        </section>

        <div className="talk-sidebar">
          <h2>Talk Sidebar</h2>

          <h3>Jump to a project</h3>
          <ProjectLinker />

          <PromiseRenderer promise={talkClient.type('tags').get(section: @props.section)}>{(tags) =>
            if tags.length
              <section>
                <h3>Latest Tags:</h3>
                {tags.map(@tag)}
              </section>
          }</PromiseRenderer>
        </div>
      </div>
    </div>

{'fraction_non_alphanumeric': 0.16306329773378483, 'fraction_numerical': 0.004297994269340974, 'mean_word_length': 2.9059003051881995, 'pattern_counts': {'":': 0, '<': 114, '<?xml version=': 0, '>': 142, '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': '3467864', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2268, 'n_words': 466}