<commit_before># @cjsx React.DOM

React                         = require 'react'
{Router, Routes, Route, Link} = require 'react-router'
LoadingIndicator              = require './loading-indicator'
ForumConnectors               = require './forum-connectors'

module.exports = React.createClass
  displayName: 'ForumSubjectWidget'
  resizing: false

  getDefaultProps: ->
    project:        null
    subject_set:    null
    subject:        null

  propTypes:
    project: React.PropTypes.object

  getInitialState: ->
    connector:      null
    posts:          {}

  # componentDidMount: ->
  componentWillReceiveProps: (new_props) ->

    project = new_props.project # result[0]

    if project.forum?.type?
      if ! ForumConnectors[project.forum?.type]?
        console.warn "Unsupported forum type. No connector defined for #{project.forum.type}"
      else
        connector = new ForumConnectors[project.forum.type] project.forum, project

    if connector?
      @setState connector: connector, () =>
        @fetchPosts 'subject', new_props.subject.id

  fetchPosts: (type, id) ->
    @setState loading: true, () =>
      @state.connector.fetchPosts type, id, (posts) =>
        @setState loading: false
        console.log 'FETCHED posts: ', posts
        @setState
          posts: posts

  handleSearchFormSubmit: (e) ->
    e.preventDefault()
    term = @refs.search_term?.getDOMNode().value.trim()
    url = @state.connector.search_url(term)
    window.open url, '_blank'

render: ->
  return null if ! @state.connector?
  create_url = @state.connector.create_url @props.subject
  search_enabled = @state.connector.search_url()?

  subject_posts = @state.posts.subject ? ( @state.posts.subject_set ? [] )

  <div className="forum-subject-widget">
    { if search_enabled
      <form onSubmit={@handleSearchFormSubmit} method='get' action='javascript:void(0);'><input type="text" ref="search_term" placeholder="Search forum"/></form>
    }

    { if @state.loading and search_enabled
        <span>Searching for discussions about this {@props.project.term('subject')}...</span>
      else if subject_posts.length > 0
        <span>
          Discussion about this {@props.project.term('subject')}:
          <ul>
          { for post,i in subject_posts
            <li key={i}>
              "<a target="_blank" href={post.search_url}>{post.excerpt.truncate 70}</a>"
              <br />&ndash; {post.author}, {moment(post.updated_at).fromNow()}
            </li>
          }
          </ul>
        </span>
    }

    { if create_url?
        <a target="_blank" href={create_url}>Start a { if subject_posts.length > 0 then 'new' else '' } discussion about this {@props.project.term('subject set')}</a>
      else
        <a>Oops! Disscussions have not been set up for this {@props.project.term('subject set')}.</a>
    }

  </div>
<commit_msg>fix indentation issue<commit_after># @cjsx React.DOM

React                         = require 'react'
{Router, Routes, Route, Link} = require 'react-router'
LoadingIndicator              = require './loading-indicator'
ForumConnectors               = require './forum-connectors'

module.exports = React.createClass
  displayName: 'ForumSubjectWidget'
  resizing: false

  getDefaultProps: ->
    project:        null
    subject_set:    null
    subject:        null

  propTypes:
    project: React.PropTypes.object

  getInitialState: ->
    connector:      null
    posts:          {}

  # componentDidMount: ->
  componentWillReceiveProps: (new_props) ->

    project = new_props.project # result[0]

    if project.forum?.type?
      if ! ForumConnectors[project.forum?.type]?
        console.warn "Unsupported forum type. No connector defined for #{project.forum.type}"
      else
        connector = new ForumConnectors[project.forum.type] project.forum, project

    if connector?
      @setState connector: connector, () =>
        @fetchPosts 'subject', new_props.subject.id

  fetchPosts: (type, id) ->
    @setState loading: true, () =>
      @state.connector.fetchPosts type, id, (posts) =>
        @setState loading: false
        console.log 'FETCHED posts: ', posts
        @setState
          posts: posts

  handleSearchFormSubmit: (e) ->
    e.preventDefault()
    term = @refs.search_term?.getDOMNode().value.trim()
    url = @state.connector.search_url(term)
    window.open url, '_blank'

  render: ->
    return null if ! @state.connector?
    create_url = @state.connector.create_url @props.subject
    search_enabled = @state.connector.search_url()?

    subject_posts = @state.posts.subject ? ( @state.posts.subject_set ? [] )

    <div className="forum-subject-widget">
      { if search_enabled
        <form onSubmit={@handleSearchFormSubmit} method='get' action='javascript:void(0);'><input type="text" ref="search_term" placeholder="Search forum"/></form>
      }

      { if @state.loading and search_enabled
          <span>Searching for discussions about this {@props.project.term('subject')}...</span>
        else if subject_posts.length > 0
          <span>
            Discussion about this {@props.project.term('subject')}:
            <ul>
            { for post,i in subject_posts
              <li key={i}>
                "<a target="_blank" href={post.search_url}>{post.excerpt.truncate 70}</a>"
                <br />&ndash; {post.author}, {moment(post.updated_at).fromNow()}
              </li>
            }
            </ul>
          </span>
      }

      { if create_url?
          <a target="_blank" href={create_url}>Start a { if subject_posts.length > 0 then 'new' else '' } discussion about this {@props.project.term('subject set')}</a>
        else
          <a>Oops! Disscussions have not been set up for this {@props.project.term('subject set')}.</a>
      }

    </div>

{'fraction_non_alphanumeric': 0.13156077348066297, 'fraction_numerical': 0.0020718232044198894, 'mean_word_length': 2.569316081330869, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 67, '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': '2012110', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1685, 'n_words': 388}