<commit_before>import React from 'react';
import {createRxComponent} from 'react-rx-component';
import Rx from 'rx'
import _ from 'lodash';
import './index.scss';
import URI from 'urijs'
import {hashHistory} from 'react-router'
import DatePicker from '../DatePicker.jsx'
import {
  FILTER_ORGANS_TYPE,
  FILTER_SORT_TYPE,
  FILTER_DATE_TYPE,
  FILTER_DATE_PICKER_TYPE
} from '../../reducers/organs'
import moment from 'moment'

const {combineLatest} = Rx.Observable;

class Filter extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      current: []
    }
  }

  onClick(index, id) {
    let {current} = this.state;
    let {options, query} = this.props;
    let filters = _.get(options, 'entities.filters');

    current = _.set(current.slice(0, index), index, id);

    let metadata = _.get(filters, id);

    let getId = id=> {
      let result = _.find(filters, {value: {orgId: id}});
      let name = _.get(result, 'id', '');
      let parentOrgId = Number(_.get(result, 'parentOrgId'));

      return parentOrgId
        ? `${getId(parentOrgId)},${name}`
        : name;
    };

    switch (_.get(metadata, 'type')) {
      case FILTER_ORGANS_TYPE:
        _.map(
          getId(Number(_.get(query, 'orgId'))).split(','),
          item=>current.push(item)
        );
        break;
      case FILTER_SORT_TYPE:
        current.push(
          _.get(_.find(filters, {
            value: {sortType: _.get(query, 'sortType', 'wordsDesc')}
          }), 'id')
        );
        break;
      case FILTER_DATE_TYPE:
        current.push(
          _.get(_.find(filters, {
              value: {
                startDate: _.get(query, 'startDate', moment().hours(-24).format('YYYY-MM-DD')),
                endDate: _.get(query, 'endDate', moment().hours(-24).format('YYYY-MM-DD'))
              }
            }),
            'id',
            _.get(_.find(filters, {type: FILTER_DATE_PICKER_TYPE}), 'id')
          )
        );
        break;
    }

    this.setState({current});

    if (_.isEmpty(_.get(metadata, 'items'))) {
      _.map(current, id=>
        _.assign(query, _.get(filters, `${id}.value`))
      );

      this.setState({current: []});

      hashHistory.push(
        String(new URI('/').query({
          ...query
          // startDate: moment(this.state.startDate).format('YYYY-MM-DD'),
          // endDate: moment(this.state.endDate).format('YYYY-MM-DD')
        }))
      );
    }
  }

  render() {
    console.log('render')
    console.log(this.state.current)

    let {current} = this.state;
    let {options, query} = this.props;
    let filters = _.get(options, 'entities.filters');
    let getOrganName = id=> {
      let result = _.find(filters, {value: {orgId: id}});
      let name = _.get(result, 'name', '');
      let parentOrgId = Number(_.get(result, 'parentOrgId'));

      return parentOrgId
        ? `${getOrganName(parentOrgId)}${name}`
        : name;
    };

    return (
      <div className={current.length > 0?'filter-container':''}>
        <div className="filter">
          <ul>
            {
              _.map(_.get(options, 'result'),
                id => {
                  let selected = id == _.get(current, 0),<commit_msg>filter<commit_after>import React from 'react';
import {createRxComponent} from 'react-rx-component';
import Rx from 'rx'
import _ from 'lodash';
import './index.scss';
import URI from 'urijs'
import {hashHistory} from 'react-router'
import DatePicker from '../DatePicker.jsx'
import {
  FILTER_ORGANS_TYPE,
  FILTER_SORT_TYPE,
  FILTER_DATE_TYPE,
  FILTER_DATE_PICKER_TYPE
} from '../../reducers/organs'
import moment from 'moment'

const {combineLatest} = Rx.Observable;

class Filter extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      current: []
    }
  }

  onClick(index, id) {
    let {current} = this.state;
    let {options, query} = this.props;
    let filters = _.get(options, 'entities.filters');

    current = _.set(current.slice(0, index), index, id);

    let metadata = _.get(filters, id);

    let getId = id=> {
      let result = _.find(filters, {value: {orgId: id}});
      let name = _.get(result, 'id', '');
      let parentOrgId = Number(_.get(result, 'parentOrgId'));

      return parentOrgId
        ? `${getId(parentOrgId)},${name}`
        : name;
    };

    switch (_.get(metadata, 'type')) {
      case FILTER_ORGANS_TYPE:
        _.map(
          getId(Number(_.get(query, 'orgId'))).split(','),
          item=>current.push(item)
        );
        break;
      case FILTER_SORT_TYPE:
        current.push(
          _.get(_.find(filters, {
            value: {sortType: _.get(query, 'sortType', 'wordsDesc')}
          }), 'id')
        );
        break;
      case FILTER_DATE_TYPE:
        current.push(
          _.get(_.find(filters, {
              value: {
                startDate: _.get(query, 'startDate', moment().hours(-24).format('YYYY-MM-DD')),
                endDate: _.get(query, 'endDate', moment().hours(-24).format('YYYY-MM-DD'))
              }
            }),
            'id',
            _.get(_.find(filters, {type: FILTER_DATE_PICKER_TYPE}), 'id')
          )
        );
        break;
    }

    this.setState({current});

    if (_.isEmpty(_.get(metadata, 'items'))) {
      _.map(current, id=>
        _.assign(query, _.get(filters, `${id}.value`))
      );

      this.setState({current: []});

      hashHistory.push(
        String(new URI('/').query({
          startDate: moment(this.state.startDate).format('YYYY-MM-DD'),
          endDate: moment(this.state.endDate).format('YYYY-MM-DD'),
          ...query
        }))
      );
    }
  }

  render() {
    console.log('render')
    console.log(this.state.current)

    let {current} = this.state;
    let {options, query} = this.props;
    let filters = _.get(options, 'entities.filters');
    let getOrganName = id=> {
      let result = _.find(filters, {value: {orgId: id}});
      let name = _.get(result, 'name', '');
      let parentOrgId = Number(_.get(result, 'parentOrgId'));

      return parentOrgId
        ? `${getOrganName(parentOrgId)}${name}`
        : name;
    };

    return (
      <div className={current.length > 0?'filter-container':''}>
        <div className="filter">
          <ul>
            {
              _.map(_.get(options, 'result'),
                id => {
                  let selected = id == _.get(current, 0),
{'fraction_non_alphanumeric': 0.16765071472964574, 'fraction_numerical': 0.002175264139216905, 'mean_word_length': 2.204081632653061, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '5154787', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2048, 'n_words': 403}