<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import ButtonToolbar from 'react-bootstrap/ButtonToolbar';
import { observer, inject, Provider } from 'mobx-react';

import { CONTROLLER_NAME, createFiltersStore, getFilterString, submitSearch } from './utils';
import ErrorBoundary from '../ErrorBoundary';
import FormFilter from './FormFilter';
import QuestionFilter from './QuestionFilter';
import AdvancedSearchFilter from './AdvancedSearchFilter';

@inject('filtersStore')
@observer
class FiltersRoot extends React.Component {
  static propTypes = {
    filtersStore: PropTypes.object.isRequired,
    controllerName: PropTypes.string,
    allForms: PropTypes.arrayOf(PropTypes.shape({
      id: PropTypes.string,
      name: PropTypes.string,
    })).isRequired,
    selectedFormIds: PropTypes.arrayOf(PropTypes.string).isRequired,
    advancedSearchText: PropTypes.string.isRequired,
  };

  static defaultProps = {
    // This is expected to be null if the feature flag is disabled.
    controllerName: null,
  };

  constructor(props) {
    super(props);

    const {
      filtersStore,
      allForms,
      selectedFormIds,
      advancedSearchText,
    } = props;

    // Directly assign initial values to the store.
    Object.assign(filtersStore, {
      allForms,
      originalFormIds: selectedFormIds,
      selectedFormIds,
      advancedSearchText,
    });
  }

  handleSubmit = () => {
    const { filtersStore } = this.props;
    const filterString = getFilterString(filtersStore);
    submitSearch(filterString);
  }

  handleClearFilters = () => {
    submitSearch(null);
  }

  renderFilterButtons = () => {
    return (
      <ButtonToolbar>
        <FormFilter
          onSubmit={this.handleSubmit}
        />
        <QuestionFilter
          onSubmit={this.handleSubmit}
        />
      </ButtonToolbar>
    );
  }

  render() {
    const { controllerName } = this.props;
    const shouldRenderButtons = controllerName === CONTROLLER_NAME.RESPONSES;

    return (
      <div className="filters">
        <ErrorBoundary>
          {shouldRenderButtons ? this.renderFilterButtons() : null}

          <AdvancedSearchFilter
            onClear={this.handleClearFilters}
            onSubmit={this.handleSubmit}
          />
        </ErrorBoundary>
      </div>
    );
  }
}

const Filters = (props) => (
  <Provider filtersStore={createFiltersStore()}>
    <FiltersRoot {...props} />
  </Provider>
);

export default Filters;

// Root component for testing.
export { FiltersRoot };<commit_msg>9397: Also provide conditionSetStore for Filters condition sets<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import ButtonToolbar from 'react-bootstrap/ButtonToolbar';
import { observer, inject, Provider } from 'mobx-react';

import { CONTROLLER_NAME, createFiltersStore, getFilterString, submitSearch } from './utils';
import ErrorBoundary from '../ErrorBoundary';
import FormFilter from './FormFilter';
import QuestionFilter from './QuestionFilter';
import AdvancedSearchFilter from './AdvancedSearchFilter';

@inject('filtersStore')
@observer
class FiltersRoot extends React.Component {
  static propTypes = {
    filtersStore: PropTypes.object.isRequired,
    controllerName: PropTypes.string,
    allForms: PropTypes.arrayOf(PropTypes.shape({
      id: PropTypes.string,
      name: PropTypes.string,
    })).isRequired,
    selectedFormIds: PropTypes.arrayOf(PropTypes.string).isRequired,
    advancedSearchText: PropTypes.string.isRequired,
  };

  static defaultProps = {
    // This is expected to be null if the feature flag is disabled.
    controllerName: null,
  };

  constructor(props) {
    super(props);

    const {
      filtersStore,
      allForms,
      selectedFormIds,
      advancedSearchText,
    } = props;

    // Directly assign initial values to the store.
    Object.assign(filtersStore, {
      allForms,
      originalFormIds: selectedFormIds,
      selectedFormIds,
      advancedSearchText,
    });
  }

  handleSubmit = () => {
    const { filtersStore } = this.props;
    const filterString = getFilterString(filtersStore);
    submitSearch(filterString);
  }

  handleClearFilters = () => {
    submitSearch(null);
  }

  renderFilterButtons = () => {
    return (
      <ButtonToolbar>
        <FormFilter
          onSubmit={this.handleSubmit}
        />
        <QuestionFilter
          onSubmit={this.handleSubmit}
        />
      </ButtonToolbar>
    );
  }

  render() {
    const { controllerName } = this.props;
    const shouldRenderButtons = controllerName === CONTROLLER_NAME.RESPONSES;

    return (
      <div className="filters">
        <ErrorBoundary>
          {shouldRenderButtons ? this.renderFilterButtons() : null}

          <AdvancedSearchFilter
            onClear={this.handleClearFilters}
            onSubmit={this.handleSubmit}
          />
        </ErrorBoundary>
      </div>
    );
  }
}

const Filters = (props) => {
  const filtersStore = createFiltersStore();
  const { conditionSetStore } = filtersStore;
  return (
    <Provider filtersStore={filtersStore} conditionSetStore={conditionSetStore}>
      <FiltersRoot {...props} />
    </Provider>
  );
};

export default Filters;

// Root component for testing.
export { FiltersRoot };
{'fraction_non_alphanumeric': 0.10871620344110418, 'fraction_numerical': 0.0007562866326337682, 'mean_word_length': 3.08179012345679, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, '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': '4928472', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1467, 'n_words': 327}