<commit_before>import React, { PropTypes, Component } from 'react';
import FullWidthButton from '../ui/FullWidthButton';
import SetThreadTitlePopup from './SetThreadTitlePopup';
import ThreadCategoryFilterList from './filters/ThreadCategoryFilterList';
import ChoiceFilter from './filters/ChoiceFilter';
import LocationFilter from './filters/LocationFilter';
import IntegerRangeFilter from './filters/IntegerRangeFilter';
import IntegerFilter from './filters/IntegerFilter';
import MultipleChoicesFilter from './filters/MultipleChoicesFilter';
import DoubleMultipleChoicesFilter from './filters/DoubleMultipleChoicesFilter';
import TagFilter from './filters/TagFilter';
import TagsAndMultipleChoicesFilter from './filters/TagsAndMultipleChoicesFilter';
import * as TagSuggestionsActionCreators from '../../actions/TagSuggestionsActionCreators';
import selectn from 'selectn';
import translate from '../../i18n/Translate';
import FilterStore from '../../stores/FilterStore';

@translate('CreateUsersThread')
export default class CreateUsersThread extends Component {

    static propTypes = {
        userId        : PropTypes.number.isRequired,
        defaultFilters: PropTypes.object.isRequired,
        threadName    : PropTypes.string,
        tags          : PropTypes.array.isRequired,
        thread        : PropTypes.object,
        categories    : PropTypes.array,
        onSave        : PropTypes.func.isRequired,
        // Injected by @translate:
        strings       : PropTypes.object
    };

    constructor(props) {
        super(props);

        this.handleClickAddFilter = this.handleClickAddFilter.bind(this);
        this.handleClickFilterOnList = this.handleClickFilterOnList.bind(this);
        this.renderActiveFilters = this.renderActiveFilters.bind(this);
        this.handleClickFilter = this.handleClickFilter.bind(this);
        this.handleErrorFilter = this.handleErrorFilter.bind(this);
        this.handleClickRemoveFilter = this.handleClickRemoveFilter.bind(this);
        this.renderChoiceFilter = this.renderChoiceFilter.bind(this);
        this.renderLocationFilter = this.renderLocationFilter.bind(this);
        this.renderMultipleChoicesFilter = this.renderMultipleChoicesFilter.bind(this);
        this.renderDoubleMultipleChoicesFilter = this.renderDoubleMultipleChoicesFilter.bind(this);
        this.renderIntegerRangeFilter = this.renderIntegerRangeFilter.bind(this);
        this.renderIntegerFilter = this.renderIntegerFilter.bind(this);
        this.renderTagFilter = this.renderTagFilter.bind(this);
        this.handleChangeFilter = this.handleChangeFilter.bind(this);
        this.handleChangeFilterAndUnSelect = this.handleChangeFilterAndUnSelect.bind(this);
        this.handleClickOutside = this.handleClickOutside.bind(this);
        this.scrollToFilter = this.scrollToFilter.bind(this);
        this.goToSelectedFilters = this.goToSelectedFilters.bind(this);
        this.editThread = this.editThread.bind(this);
        this.createThread = this.createThread.bind(this);
        this.onSaveTitle = this.onSaveTitle.bind(this);
        this.getDefaultTitle = this.getDefaultTitle.bind(this);
        this.getOrderDefaultFilter = this.getOrderDefaultFilter.bind(this);

        this.state = {
            selectFilter        : false,
            selectedFilter      : null,
            filters             : selectn('thread.filters.userFilters', props) || {},
            displayingTitlePopup: null
        }
    }

    componentDidMount() {
        window.nekunoContainer.addEventListener('click', this.handleClickOutside)
    }

    componentWillUnmount() {
        window.nekunoContainer.removeEventListener('click', this.handleClickOutside)
    }

    handleClickAddFilter() {
        TagSuggestionsActionCreators.resetTagSuggestions();
        this.setState({
            selectFilter: true
        })
    }

    handleClickFilterOnList(checked, value) {
        if (this.props.thread.groupId != null && value === 'groups'){
            return;
        }
        let filters = this.state.filters;
        let filterData = Object.keys(filters).find(key => key === value) || null;

        if (checked) {
            filters[value] = filterData;
            this.setState({
                selectFilter  : false,
                selectedFilter: value,
                filters       : filters
            });
            this.scrollToFilter();
        } else {
            delete filters[value];
            this.setState({
                filters       : filters,
                selectedFilter: null
            });
        }
    }

    renderActiveFilters() {
        const defaultFilters = Object.assign({}, this.props.defaultFilters);
        const {filters} = this.state;
        const {tags, thread} = this.props;
        return (
            Object.keys(filters).map(key => {
                const selected = this.state.selectedFilter === key;
                let filter = null;
                if (key == 'order') {
                    return null;
                }
                switch (defaultFilters[key].type) {
                    case 'location_distance':
                        filter = this.renderLocationFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'integer_range':
                        filter = this.renderIntegerRangeFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'birthday_range':
                        filter = this.renderIntegerRangeFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'integer':
                        filter = this.renderIntegerFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'multiple_choices':
                        const cantRemove = thread.groupId != null;
                        filter = this.renderMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected, cantRemove);
                        break;
                    case 'double_multiple_choices':
                        filter = this.renderDoubleMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'tags_and_multiple_choices':
                        filter = this.renderTagsAndMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected, tags);
                        break;
                    case 'tags':
                        filter = this.renderTagFilter(defaultFilters[key], key, filters[key], selected, tags);
                        break;
                }
                return <div key={key} ref={selected ? 'selectedFilter' : ''}>{filter}</div>;
            })
        );
    }


    renderChoiceFilter(filter, key, data, selected) {
        return (
            <ChoiceFilter filterKey={key}
                          filter={filter}
                          data={data}
                          selected={selected}
                          handleClickRemoveFilter={this.handleClickRemoveFilter}
                          handleChangeFilter={this.handleChangeFilterAndUnSelect}
                          handleClickFilter={this.handleClickFilter}
                          cantRemove={key === 'order'}
            /><commit_msg>Fix some filters not removable on group threads<commit_after>import React, { PropTypes, Component } from 'react';
import FullWidthButton from '../ui/FullWidthButton';
import SetThreadTitlePopup from './SetThreadTitlePopup';
import ThreadCategoryFilterList from './filters/ThreadCategoryFilterList';
import ChoiceFilter from './filters/ChoiceFilter';
import LocationFilter from './filters/LocationFilter';
import IntegerRangeFilter from './filters/IntegerRangeFilter';
import IntegerFilter from './filters/IntegerFilter';
import MultipleChoicesFilter from './filters/MultipleChoicesFilter';
import DoubleMultipleChoicesFilter from './filters/DoubleMultipleChoicesFilter';
import TagFilter from './filters/TagFilter';
import TagsAndMultipleChoicesFilter from './filters/TagsAndMultipleChoicesFilter';
import * as TagSuggestionsActionCreators from '../../actions/TagSuggestionsActionCreators';
import selectn from 'selectn';
import translate from '../../i18n/Translate';
import FilterStore from '../../stores/FilterStore';

@translate('CreateUsersThread')
export default class CreateUsersThread extends Component {

    static propTypes = {
        userId        : PropTypes.number.isRequired,
        defaultFilters: PropTypes.object.isRequired,
        threadName    : PropTypes.string,
        tags          : PropTypes.array.isRequired,
        thread        : PropTypes.object,
        categories    : PropTypes.array,
        onSave        : PropTypes.func.isRequired,
        // Injected by @translate:
        strings       : PropTypes.object
    };

    constructor(props) {
        super(props);

        this.handleClickAddFilter = this.handleClickAddFilter.bind(this);
        this.handleClickFilterOnList = this.handleClickFilterOnList.bind(this);
        this.renderActiveFilters = this.renderActiveFilters.bind(this);
        this.handleClickFilter = this.handleClickFilter.bind(this);
        this.handleErrorFilter = this.handleErrorFilter.bind(this);
        this.handleClickRemoveFilter = this.handleClickRemoveFilter.bind(this);
        this.renderChoiceFilter = this.renderChoiceFilter.bind(this);
        this.renderLocationFilter = this.renderLocationFilter.bind(this);
        this.renderMultipleChoicesFilter = this.renderMultipleChoicesFilter.bind(this);
        this.renderDoubleMultipleChoicesFilter = this.renderDoubleMultipleChoicesFilter.bind(this);
        this.renderIntegerRangeFilter = this.renderIntegerRangeFilter.bind(this);
        this.renderIntegerFilter = this.renderIntegerFilter.bind(this);
        this.renderTagFilter = this.renderTagFilter.bind(this);
        this.handleChangeFilter = this.handleChangeFilter.bind(this);
        this.handleChangeFilterAndUnSelect = this.handleChangeFilterAndUnSelect.bind(this);
        this.handleClickOutside = this.handleClickOutside.bind(this);
        this.scrollToFilter = this.scrollToFilter.bind(this);
        this.goToSelectedFilters = this.goToSelectedFilters.bind(this);
        this.editThread = this.editThread.bind(this);
        this.createThread = this.createThread.bind(this);
        this.onSaveTitle = this.onSaveTitle.bind(this);
        this.getDefaultTitle = this.getDefaultTitle.bind(this);
        this.getOrderDefaultFilter = this.getOrderDefaultFilter.bind(this);

        this.state = {
            selectFilter        : false,
            selectedFilter      : null,
            filters             : selectn('thread.filters.userFilters', props) || {},
            displayingTitlePopup: null
        }
    }

    componentDidMount() {
        window.nekunoContainer.addEventListener('click', this.handleClickOutside)
    }

    componentWillUnmount() {
        window.nekunoContainer.removeEventListener('click', this.handleClickOutside)
    }

    handleClickAddFilter() {
        TagSuggestionsActionCreators.resetTagSuggestions();
        this.setState({
            selectFilter: true
        })
    }

    handleClickFilterOnList(checked, value) {
        if (this.props.thread.groupId != null && value === 'groups'){
            return;
        }
        let filters = this.state.filters;
        let filterData = Object.keys(filters).find(key => key === value) || null;

        if (checked) {
            filters[value] = filterData;
            this.setState({
                selectFilter  : false,
                selectedFilter: value,
                filters       : filters
            });
            this.scrollToFilter();
        } else {
            delete filters[value];
            this.setState({
                filters       : filters,
                selectedFilter: null
            });
        }
    }

    renderActiveFilters() {
        const defaultFilters = Object.assign({}, this.props.defaultFilters);
        const {filters} = this.state;
        const {tags, thread} = this.props;
        return (
            Object.keys(filters).map(key => {
                const selected = this.state.selectedFilter === key;
                let filter = null;
                if (key == 'order') {
                    return null;
                }
                switch (defaultFilters[key].type) {
                    case 'location_distance':
                        filter = this.renderLocationFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'integer_range':
                        filter = this.renderIntegerRangeFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'birthday_range':
                        filter = this.renderIntegerRangeFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'integer':
                        filter = this.renderIntegerFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'multiple_choices':
                        const cantRemove = thread.groupId != null && key == 'groups';
                        filter = this.renderMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected, cantRemove);
                        break;
                    case 'double_multiple_choices':
                        filter = this.renderDoubleMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected);
                        break;
                    case 'tags_and_multiple_choices':
                        filter = this.renderTagsAndMultipleChoicesFilter(defaultFilters[key], key, filters[key], selected, tags);
                        break;
                    case 'tags':
                        filter = this.renderTagFilter(defaultFilters[key], key, filters[key], selected, tags);
                        break;
                }
                return <div key={key} ref={selected ? 'selectedFilter' : ''}>{filter}</div>;
            })
        );
    }


    renderChoiceFilter(filter, key, data, selected) {
        return (
            <ChoiceFilter filterKey={key}
                          filter={filter}
                          data={data}
                          selected={selected}
                          handleClickRemoveFilter={this.handleClickRemoveFilter}
                          handleChangeFilter={this.handleChangeFilterAndUnSelect}
                          handleClickFilter={this.handleClickFilter}
                          cantRemove={key === 'order'}
            />
{'fraction_non_alphanumeric': 0.09724189088947333, 'fraction_numerical': 0.0002697417223008969, 'mean_word_length': 2.141949152542373, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '7549947', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3490, 'n_words': 651}