<commit_before>import cn from "classnames";
import {fromJS, Map, Set} from "immutable";
import {fetchJson, debounce} from "./tools";
import URI from "urijs";

let range = (from, to) => from > to ? [] : [from].concat(range(from + 1, to));
const MIN_YEAR = 2010;
const MAX_YEAR = 2020;
const MENU_BOX_COMPARISON = "menu-box";

export default class OCApp extends React.Component{
  constructor(props){
    super(props);
    this.tabs = [];
    this.state = {
      width: 0,
      currentTab: 0,
      menuBox: "",
      compareBy: "",
      comparisonCriteriaValues: [],
      selectedYears: Set(range(MIN_YEAR, MAX_YEAR)),
      filters: fromJS({}),
      data: fromJS({}),
      comparisonData: fromJS({}),
      bidTypes: fromJS({})
    }
  }

  registerTab(tab){
    this.tabs.push(tab);
  }

  __(text){
    return text;
  }

  filters(){
    return <div className="filters">
      <img className="top-nav-icon" src="assets/icons/filter.svg"/> {this.__('Filter the data')} <i className="glyphicon glyphicon-menu-down"></i>
    </div>
  }

  updateComparisonCriteria(criteria){
    this.setState({
      menuBox: "",
      compareBy: criteria,
      comparisonCriteriaValues: [],
      comparisonData: fromJS({})
    });
    if(!criteria) return;
    fetchJson(new URI('/api/costEffectivenessTenderAmount').addSearch({
      groupByCategory: criteria,
      pageSize: 3
    })).then(data => this.setState({
      comparisonCriteriaValues: data.map(datum => datum[0] || datum._id)
    }));
  }

  fetchBidTypes(){
    fetchJson('/api/ocds/bidType/all').then(data =>
        this.setState({bidTypes: data.reduce((map, datum) => map.set(datum.id, datum.description), Map())})
    );
  }

  componentDidMount(){
    this.fetchBidTypes();

    this.setState({
      width: document.querySelector('.years-bar').offsetWidth
    });

    window.addEventListener("resize", debounce(() => {
      this.setState({
        width: document.querySelector('.years-bar').offsetWidth
      });
    }));
  }

  comparison(){
    let {menuBox, compareBy} = this.state;
    return <div
        onClick={_ => this.setState({menuBox: menuBox == MENU_BOX_COMPARISON ? "" : MENU_BOX_COMPARISON})}
        className={cn("filters compare", {open: menuBox == MENU_BOX_COMPARISON})}
    >
      <img className="top-nav-icon" src="assets/icons/compare.svg"/> {this.__('Compare')} <i className="glyphicon glyphicon-menu-down"></i>
      <div className="box" onClick={e => e.stopPropagation()}>
        <div className="col-sm-6">
          <label>{this.__('Comparison criteria')}</label><commit_msg>OCVN-223 Clicking outside of a menu box closes the box<commit_after>import cn from "classnames";
import {fromJS, Map, Set} from "immutable";
import {fetchJson, debounce} from "./tools";
import URI from "urijs";

let range = (from, to) => from > to ? [] : [from].concat(range(from + 1, to));
const MIN_YEAR = 2010;
const MAX_YEAR = 2020;
const MENU_BOX_COMPARISON = "menu-box";
const MENU_BOX_FILTERS = 'filters';

export default class OCApp extends React.Component{
  constructor(props){
    super(props);
    this.tabs = [];
    this.state = {
      width: 0,
      currentTab: 0,
      menuBox: "",
      compareBy: "",
      comparisonCriteriaValues: [],
      selectedYears: Set(range(MIN_YEAR, MAX_YEAR)),
      filters: fromJS({}),
      data: fromJS({}),
      comparisonData: fromJS({}),
      bidTypes: fromJS({})
    }
  }

  registerTab(tab){
    this.tabs.push(tab);
  }

  __(text){
    return text;
  }

  updateComparisonCriteria(criteria){
    this.setState({
      menuBox: "",
      compareBy: criteria,
      comparisonCriteriaValues: [],
      comparisonData: fromJS({})
    });
    if(!criteria) return;
    fetchJson(new URI('/api/costEffectivenessTenderAmount').addSearch({
      groupByCategory: criteria,
      pageSize: 3
    })).then(data => this.setState({
      comparisonCriteriaValues: data.map(datum => datum[0] || datum._id)
    }));
  }

  fetchBidTypes(){
    fetchJson('/api/ocds/bidType/all').then(data =>
        this.setState({bidTypes: data.reduce((map, datum) => map.set(datum.id, datum.description), Map())})
    );
  }

  componentDidMount(){
    this.fetchBidTypes();

    this.setState({
      width: document.querySelector('.years-bar').offsetWidth
    });

    window.addEventListener("resize", debounce(() => {
      this.setState({
        width: document.querySelector('.years-bar').offsetWidth
      });
    }));
  }

  setMenuBox(e, slug){
    let {menuBox} = this.state;
    e.stopPropagation();
    this.setState({menuBox: menuBox == slug ? "" : slug})
  }

  filters(){
    return <div className="filters">
      <img className="top-nav-icon" src="assets/icons/filter.svg"/> {this.__('Filter the data')} <i className="glyphicon glyphicon-menu-down"></i>
    </div>
  }

  comparison(){
    let {menuBox, compareBy} = this.state;
    return <div
        onClick={e => this.setMenuBox(e, MENU_BOX_COMPARISON)}
        className={cn("filters compare", {open: menuBox == MENU_BOX_COMPARISON})}
    >
      <img className="top-nav-icon" src="assets/icons/compare.svg"/> {this.__('Compare')} <i className="glyphicon glyphicon-menu-down"></i>
      <div className="box" onClick={e => e.stopPropagation()}>
        <div className="col-sm-6">
          <label>{this.__('Comparison criteria')}</label>
{'fraction_non_alphanumeric': 0.1698645598194131, 'fraction_numerical': 0.005831452219714071, 'mean_word_length': 3.298302344381568, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 47, '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': '3169835', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1708, 'n_words': 331}