<commit_before>/* Copyright (C) 2017 ethbets
 * All rights reserved.
 * 
 * This software may be modified and distributed under the terms
 * of the BSD license. See the LICENSE file for details.
*/

/*global web3:true */
import React, { Component } from 'react';

import EbetsJson from 'build/contracts/Ebets.json';
import Bet from 'components/Bet';
import PropTypes from 'prop-types';
import {getParsedCategories} from 'utils/ebetsCategories';

class Ebets extends Component {
  constructor(props) {
    super(props);
    this.state = {
      bets: [],
      ebetsContractInstance: null
    }
  }

  getBetsByCategory = (category, ebetsContractInstance) => {
    return new Promise( async (resolve, reject) => {
      let betPromises = [];
      if (category === 'all_bets') {
        betPromises = getParsedCategories().map(category => ({
            bets: ebetsContractInstance.getBetsByCategory(category.key),
            category: category.key
        }));
      }
      else {
        betPromises = [{
          bets: ebetsContractInstance.getBetsByCategory(category),
          category: category
        }];
      }
      const bets = (await Promise.all(betPromises.map(betCat => (betCat.bets)))).reduce((before, bet, idx) => {
        return before.concat(bet.map(b => ({bet: b, category: betPromises[idx].category})));
      }, []);
      resolve(bets);
    });
  }
  
  componentWillMount() {
    this.instantiateContract();
  }
  componentWillUnmount () {
    if (this.state.betsEvents)
      this.state.betsEvents.stopWatching();
  }
  componentWillReceiveProps(nextProps) {
    const category = nextProps.routeParams.category;
    if (this.state.ebetsContractInstance !== null && 
        category !== undefined) {
      this.getBetsByCategory(category, this.state.ebetsContractInstance)
      .then(bets => {
        this.setState({bets: bets})
      });
    }
  }

  async instantiateContract() {
    const contract = require('truffle-contract');
    const ebetsContract = contract(EbetsJson);
    ebetsContract.setProvider(web3.currentProvider);
    // Get accounts.
    web3.eth.getAccounts(async (error) => {
      if (error) {
        console.error('Error', error);
        return;
      }
      var ebetsContractInstance;
      try {
        ebetsContractInstance = await ebetsContract.deployed();
      }
      catch(error) {
        console.error('Contract not deployed!');
        return;
      }
      var bets = await this.getBetsByCategory(this.props.routeParams.category, ebetsContractInstance);
      //events
      const betsEvents = ebetsContractInstance.allEvents({fromBlock: 'latest', toBlock: 'latest'});
      betsEvents.watch((error, response) => {
        if (response.args.category === this.props.routeParams.category)
          this.setState(previousState => ({bets: previousState.bets.concat(response.args.betAddr)}));
      })
      this.setState({
        bets: bets,
        //betsEvents: betsEvents,
        ebetsContractInstance: ebetsContractInstance
      });
    }
  );
  }

  render() {
    var { category, address } = this.props.routeParams;
    var listItems = [];
    if (category !== undefined) {
      if (this.props.routeParams.subcategory !== undefined)
        category = category + '/' + this.props.routeParams.subcategory;
      listItems = this.state.bets.map(betCat => 
        <Bet isDetailed={false}<commit_msg>bugfix: filter bet by category<commit_after>/* Copyright (C) 2017 ethbets
 * All rights reserved.
 * 
 * This software may be modified and distributed under the terms
 * of the BSD license. See the LICENSE file for details.
*/

/*global web3:true */
import React, { Component } from 'react';

import EbetsJson from 'build/contracts/Ebets.json';
import Bet from 'components/Bet';
import PropTypes from 'prop-types';
import {getParsedCategories} from 'utils/ebetsCategories';

class Ebets extends Component {
  constructor(props) {
    super(props);
    this.state = {
      bets: [],
      ebetsContractInstance: null
    }
  }

  getBetsByCategory = (category, ebetsContractInstance) => {
    console.log(category);
    return new Promise( async (resolve, reject) => {
      let betPromises = [];
      if (category === 'all_bets') {
        betPromises = getParsedCategories().map(category => ({
            bets: ebetsContractInstance.getBetsByCategory(category.key),
            category: category.key
        }));
      }
      else {
        betPromises = [{
          bets: ebetsContractInstance.getBetsByCategory(category),
          category: category
        }];
      }
      const bets = (await Promise.all(betPromises.map(betCat => (betCat.bets)))).reduce((before, bet, idx) => {
        return before.concat(bet.map(b => ({bet: b, category: betPromises[idx].category})));
      }, []);
      resolve(bets);
    });
  }
  
  componentWillMount() {
    this.instantiateContract();
  }
  componentWillUnmount () {
    if (this.state.betsEvents)
      this.state.betsEvents.stopWatching();
  }
  componentWillReceiveProps(nextProps) {
    var category = nextProps.routeParams.category;
    if (nextProps.routeParams.subcategory)
        category += '/' + nextProps.routeParams.subcategory;
    if (this.state.ebetsContractInstance !== null && 
        category !== undefined) {
      this.getBetsByCategory(category, this.state.ebetsContractInstance)
      .then(bets => {
        this.setState({bets: bets})
      });
    }
  }

  async instantiateContract() {
    const contract = require('truffle-contract');
    const ebetsContract = contract(EbetsJson);
    ebetsContract.setProvider(web3.currentProvider);
    // Get accounts.
    web3.eth.getAccounts(async (error) => {
      if (error) {
        console.error('Error', error);
        return;
      }
      var ebetsContractInstance;
      try {
        ebetsContractInstance = await ebetsContract.deployed();
      }
      catch(error) {
        console.error('Contract not deployed!');
        return;
      }
      var category = this.props.routeParams.category;
      if (this.props.routeParams.subcategory)
        category += '/' + this.props.routeParams.subcategory;
      var bets = await this.getBetsByCategory(category, ebetsContractInstance);
      //events
      const betsEvents = ebetsContractInstance.allEvents({fromBlock: 'latest', toBlock: 'latest'});
      betsEvents.watch((error, response) => {
        if (response.args.category === this.props.routeParams.category)
          this.setState(previousState => ({bets: previousState.bets.concat(response.args.betAddr)}));
      })
      this.setState({
        bets: bets,
        //betsEvents: betsEvents,
        ebetsContractInstance: ebetsContractInstance
      });
    }
  );
  }

  render() {
    var { category, address } = this.props.routeParams;
    var listItems = [];
    if (category !== undefined) {
      if (this.props.routeParams.subcategory !== undefined)
        category = category + '/' + this.props.routeParams.subcategory;
      listItems = this.state.bets.map(betCat => 
        <Bet isDetailed={false}
{'fraction_non_alphanumeric': 0.12867908431679226, 'fraction_numerical': 0.0019906156689890515, 'mean_word_length': 2.8500273672687464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '6619590', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2044, 'n_words': 409}