<commit_before>/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */
import SwipeableViews from 'react-swipeable-views';
import Markdown from 'react-remarkable';
import React, {PropTypes} from 'react';
import Helmet from 'react-helmet';
import withStyles from 'isomorphic-style-loader/lib/withStyles';

// internal
import Ballot, {BallotPropType, PartiesPropTypes} from '../../../components/Ballot';
import Layout from '../../../components/Layout';
import BallotChart from '../../../components/BallotChart';
import AssemblyChart from '../../../components/AssemblyChart';
import s from './Ballots.css';
import { ViewPropTypes } from '../../../components/View';
import * as _ from 'lodash';
import * as d3 from 'd3';

const BallotsPropTypes = Object.assign({}, {
  activeBallot: PropTypes.string,
  ballots: PropTypes.arrayOf(PropTypes.shape(BallotPropType)),
  parties: PartiesPropTypes
}, ViewPropTypes);

class Ballots extends React.Component {
  static propTypes = BallotsPropTypes;

  componentDidMount() {
    this.props.onRef(this)
  }
  componentWillUnmount() {
    this.props.onRef(undefined)
  }

  hasSwipeableViews(){ return true; }

  constructor(props, context, updater) {
    super(props, context, updater);

    let activeId  = this.props.activeBallot;
    const ballots = this.props.ballots;
    const activeBallot = ballots.find((b)=>b.id == activeId);
    const activeIndex  = ballots.indexOf(activeBallot);
    this.state = {
      index: activeIndex
    };
  }

  setIndex(index){
    this.setState({index:index});
  }

  atLastTab(){
    return this.state.index == this.props.ballots.length - 1;
  }

  atFirstTab(){
    return this.state.index == 0;
  }

  goNext(){
    // console.log('goNext');
    const { index } = this.state;
    this.setState({
      index: index + 1
    });
  }

  goPrevious(){
    // console.log('goPrevious');
    const { index } = this.state;
    this.setState({
      index: index - 1
    });
  }

  onNextClicked() {
    this.goNext();
  }

  onPreviousClicked() {
    this.goPrevious();
  }

  onChangeIndex(i){
    // console.log('onChangeIndex !');
    if(i != this.state.index){
      this.setState({index:i});
    }
  }

  isActive(index){
    return this.state.index == index;
  }

  paginationItemClass(index){
    let classes = [ s.paginationItem ];
    if(this.isActive(index)){
      classes.push(s['paginationItem--active']);
    }
    return classes.join(' ');
  }

  currentBallotData(){
    const {index,} = this.state;
    const results  = this.props.ballots[index].results.slice(0);
    const parties  = this.props.parties.slice(0);
    return { results, parties };
  }

  render() {
    const {index,} = this.state;
    const {
      ballots, parties, isActive
    } = this.props;
    const ballotData = this.currentBallotData();
    const results = _.cloneDeep(ballotData.results);
    const firstResult = results.sort((a, b)=>b.seats - a.seats)[0];
    const firstParty = parties.find((p)=> p.id == firstResult.party);
    const allocatedSeats = d3.sum(results, (r)=>r.seats);
    const totalSeats = 577;
    const absoluteMajority = firstResult.seats > Math.ceil(totalSeats/2);
    console.log('firstParty: ', firstParty);
    return (
      <div>
        <div className={s.pagination}>
          <div className={s.container}>
            {ballots.map((ballot, key) => {
              return <div key={key} className={ this.paginationItemClass(key) } onClick={ this.setIndex.bind(this, key) }>{key+1}</div>;
            })}
          </div>
        </div>
        <div className={s.container + ' ' + s.content}>
          <div className={s['content--left']}>
            <BallotChart data={ _.cloneDeep(ballotData) }/><commit_msg>Remove debug info from routes/study/ballots<commit_after>/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */
import SwipeableViews from 'react-swipeable-views';
import Markdown from 'react-remarkable';
import React, {PropTypes} from 'react';
import Helmet from 'react-helmet';
import withStyles from 'isomorphic-style-loader/lib/withStyles';

// internal
import Ballot, {BallotPropType, PartiesPropTypes} from '../../../components/Ballot';
import Layout from '../../../components/Layout';
import BallotChart from '../../../components/BallotChart';
import AssemblyChart from '../../../components/AssemblyChart';
import s from './Ballots.css';
import { ViewPropTypes } from '../../../components/View';
import * as _ from 'lodash';
import * as d3 from 'd3';

const BallotsPropTypes = Object.assign({}, {
  activeBallot: PropTypes.string,
  ballots: PropTypes.arrayOf(PropTypes.shape(BallotPropType)),
  parties: PartiesPropTypes
}, ViewPropTypes);

class Ballots extends React.Component {
  static propTypes = BallotsPropTypes;

  componentDidMount() {
    this.props.onRef(this)
  }
  componentWillUnmount() {
    this.props.onRef(undefined)
  }

  hasSwipeableViews(){ return true; }

  constructor(props, context, updater) {
    super(props, context, updater);

    let activeId  = this.props.activeBallot;
    const ballots = this.props.ballots;
    const activeBallot = ballots.find((b)=>b.id == activeId);
    const activeIndex  = ballots.indexOf(activeBallot);
    this.state = {
      index: activeIndex
    };
  }

  setIndex(index){
    this.setState({index:index});
  }

  atLastTab(){
    return this.state.index == this.props.ballots.length - 1;
  }

  atFirstTab(){
    return this.state.index == 0;
  }

  goNext(){
    // console.log('goNext');
    const { index } = this.state;
    this.setState({
      index: index + 1
    });
  }

  goPrevious(){
    // console.log('goPrevious');
    const { index } = this.state;
    this.setState({
      index: index - 1
    });
  }

  onNextClicked() {
    this.goNext();
  }

  onPreviousClicked() {
    this.goPrevious();
  }

  onChangeIndex(i){
    // console.log('onChangeIndex !');
    if(i != this.state.index){
      this.setState({index:i});
    }
  }

  isActive(index){
    return this.state.index == index;
  }

  paginationItemClass(index){
    let classes = [ s.paginationItem ];
    if(this.isActive(index)){
      classes.push(s['paginationItem--active']);
    }
    return classes.join(' ');
  }

  currentBallotData(){
    const {index,} = this.state;
    const results  = this.props.ballots[index].results.slice(0);
    const parties  = this.props.parties.slice(0);
    return { results, parties };
  }

  render() {
    const {index,} = this.state;
    const {
      ballots, parties, isActive
    } = this.props;
    const ballotData = this.currentBallotData();
    const results = _.cloneDeep(ballotData.results);
    const firstResult = results.sort((a, b)=>b.seats - a.seats)[0];
    const firstParty = parties.find((p)=> p.id == firstResult.party);
    const allocatedSeats = d3.sum(results, (r)=>r.seats);
    const totalSeats = 577;
    const absoluteMajority = firstResult.seats > Math.ceil(totalSeats/2);
    return (
      <div>
        <div className={s.pagination}>
          <div className={s.container}>
            {ballots.map((ballot, key) => {
              return <div key={key} className={ this.paginationItemClass(key) } onClick={ this.setIndex.bind(this, key) }>{key+1}</div>;
            })}
          </div>
        </div>
        <div className={s.container + ' ' + s.content}>
          <div className={s['content--left']}>
            <BallotChart data={ _.cloneDeep(ballotData) }/>
{'fraction_non_alphanumeric': 0.15433918728368157, 'fraction_numerical': 0.005896679912831688, 'mean_word_length': 3.048780487804878, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2959397', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2452, 'n_words': 534}