<commit_before>/** @jsx React.DOM */
"use strict";
var React = require("react/addons");
var Question = require("./Question");

module.exports = React.createClass({
  displayName: "QuestionList",

  getInitialState () {
    return {
      selectedIndex: -1,
      items: this.props.items
    };
  },

  componentWillReceiveProps (nextProps) {
    this.setState({
      items: nextProps.items
    });
  },

  render () {
    return this._render(this.props, this.state);
  },

  _handleClick (i, event) {
    if (i !== this.state.selectedIndex) {
      this.setState({ selectedIndex: i });
    } else {
      this.setState({ selectedIndex: -1 });
    }
  },

  _handleSort (event) {
    var {items} = this.state;
    switch(event.target.id) {
      case 'sortBySign':
        items.sort(function (a, b) {
          return b.signatures.length - a.signatures.length;
        });
        break;
      case 'sortByTime':
        items.sort(function (a, b) {
          return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
        });
        break;
      default:
    }
    this.setState({items});
  },

  _render (props, state) {
    var {items} = state;
    var result = items.map((item, index) => {
      var boundClick = this._handleClick.bind(this, item.id);
      var selected = (item.id === state.selectedIndex)? true: false;
      if(props.selected === item.id && -1 === state.selectedIndex) {
        selected = true;
      }
      return <Question key={item.id}
                       selected={selected}
                       index={index}
                       question={item}
                       _handleClick={boundClick}
                       cid={props.candidate.id}
                       pid={item.pid}
                       qid={item.id}
                       loggedIn={props.loggedIn}
                       status={props.status} />;
    });
    var sortingMenu = <div className="sorting_menu" >
            <label className="radio" >
               <input id="sortBySign" type="radio" name="radios" defaultChecked={true} onClick={this._handleSort} />
               <span className="outer"><span className="inner"></span></span>
               <span className="radio_text">票數排序</span>
            </label>
            <label className="radio">
            <input id="sortByTime" type="radio" name="radios" onClick={this._handleSort} />
               <span className="outer"><span className="inner"></span></span>
               <span className="radio_text">時間排序</span>
            </label>
        </div>;

    return <div >
      <div className="question_menu">
        <div className="question_menu_meta">{'共有 ' + items.length + ' 題'}</div>
        {sortingMenu}
      </div>
      {result}
    </div>;
  }
});<commit_msg>fix(sort): sorting by signatures when showing all questions<commit_after>/** @jsx React.DOM */
"use strict";
var React = require("react/addons");
var Question = require("./Question");

module.exports = React.createClass({
  displayName: "QuestionList",

  getInitialState () {
    return {
      selectedIndex: -1,
      items: this.props.items,
      sortBy: 'sign'
    };
  },

  componentWillReceiveProps (nextProps) {
    this.setState({
      items: nextProps.items
    });
  },

  render () {
    return this._render(this.props, this.state);
  },

  _handleClick (i, event) {
    if (i !== this.state.selectedIndex) {
      this.setState({ selectedIndex: i });
    } else {
      this.setState({ selectedIndex: -1 });
    }
  },

  _handleSort (event) {
    var {items, sortBy} = this.state;
    switch(event.target.id) {
      case 'sortBySign':
        items.sort(function (a, b) {
          return b.signatures.length - a.signatures.length;
        });
        sortBy = 'sign';
        break;
      case 'sortByTime':
        items.sort(function (a, b) {
          return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
        });
        sortBy = 'time';
        break;
      default:
    }
    this.setState({items, sortBy});
  },

  _render (props, state) {
    var {items} = state;
    if('sign' === state.sortBy) {
      items = items.sort(function (a, b) { return b.signatures.length - a.signatures.length; });
    }
    var result = items.map((item, index) => {
      var boundClick = this._handleClick.bind(this, item.id);
      var selected = (item.id === state.selectedIndex)? true: false;
      if(props.selected === item.id && -1 === state.selectedIndex) {
        selected = true;
      }
      return <Question key={item.id}
                       selected={selected}
                       index={index}
                       question={item}
                       _handleClick={boundClick}
                       cid={props.candidate.id}
                       pid={item.pid}
                       qid={item.id}
                       loggedIn={props.loggedIn}
                       status={props.status} />;
    });
    var sortingMenu = <div className="sorting_menu" >
            <label className="radio" >
               <input id="sortBySign" type="radio" name="radios" defaultChecked={true} onClick={this._handleSort} />
               <span className="outer"><span className="inner"></span></span>
               <span className="radio_text">票數排序</span>
            </label>
            <label className="radio">
            <input id="sortByTime" type="radio" name="radios" onClick={this._handleSort} />
               <span className="outer"><span className="inner"></span></span>
               <span className="radio_text">時間排序</span>
            </label>
        </div>;

    return <div >
      <div className="question_menu">
        <div className="question_menu_meta">{'共有 ' + items.length + ' 題'}</div>
        {sortingMenu}
      </div>
      {result}
    </div>;
  }
});
{'fraction_non_alphanumeric': 0.1529046716083434, 'fraction_numerical': 0.001034304430270643, 'mean_word_length': 1.9922640536358949, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, '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': '7125437', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1700, 'n_words': 330}