<commit_before>/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Component } from 'react';
import PropTypes from 'prop-types';
import isEqual from 'fast-deep-equal';
import { omit } from 'common/utils/omit';
import { LEVEL_STEP, LEVEL_SUITE, LEVEL_TEST, LEVEL_LAUNCH } from 'common/constants/launchLevels';
import {
  StepLevelEntities,
  SuiteLevelEntities,
  LaunchLevelEntities,
} from 'pages/inside/common/filterEntitiesGroups';
import { filterValueShape } from '../propTypes';
import { FilterEntitiesAddHandlerContainer } from './filterEntitiesAddHandlerContainer';

const ENTITY_PROVIDERS = {
  [LEVEL_LAUNCH]: LaunchLevelEntities,
  [LEVEL_SUITE]: SuiteLevelEntities,
  [LEVEL_TEST]: SuiteLevelEntities,
  [LEVEL_STEP]: StepLevelEntities,
};

export class FilterEntitiesContainer extends Component {
  static propTypes = {
    entities: PropTypes.objectOf(filterValueShape),
    onChange: PropTypes.func,
    render: PropTypes.func.isRequired,
    level: PropTypes.string,
    entitiesProvider: PropTypes.elementType,
  };

  static defaultProps = {
    entities: {},
    onChange: () => {},
    entitiesProvider: null,
    level: '',
  };

  static getDerivedStateFromProps(props, state) {
    if (props.entities !== state.values && !isEqual(props.entities, state.prevEntities)) {
      return {
        errors: {},
        values: props.entities,
        prevEntities: props.entities,
        visibleFilters: Object.keys(props.entities),
      };
    }
    return null;
  }
  state = {
    errors: {},
    values: this.props.entities,
    prevEntities: this.props.entities,
    visibleFilters: Object.keys(this.props.entities),
  };
  collectEntities = (values) =>
    Object.keys(values).reduce((acc, entityId) => {
      const value = values[entityId];
      return this.isValidChange(entityId)
        ? { ...acc, [entityId]: value }
        : { ...acc, [entityId]: { ...value, value: '' } };
    }, {});

  isValidChange = (entityId) => {
    const value = this.state.values[entityId] && this.state.values[entityId].value;
    if (!value) {
      return true;
    }
    return !this.state.errors[entityId];
  };

  handleChange = (entityId, value) => {
    this.setState(
      {
        values: { ...this.state.values, [entityId]: { ...this.state.values[entityId], ...value } },
      },
      () =>
        this.isValidChange(entityId) &&
        this.props.onChange(this.collectEntities(this.state.values)),
    );
  };

  handleValidate = (entityId, error) => {
    if (!error) {
      this.setState({ errors: omit(this.state.errors, [entityId]) });
    } else {
      this.setState({ errors: { ...this.state.errors, [entityId]: error } });
    }
  };

  mergeEntityValue = (oldEntity, newEntity) => {
    if (oldEntity) {
      const oldEntityValue = oldEntity.value;
      const isNewValueValid =
        newEntity.value && oldEntityValue.split(',').indexOf(newEntity.value) === -1;

      return {
        ...oldEntity,<commit_msg>EPMRPP-56167 Skip merging for empty entity value<commit_after>/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Component } from 'react';
import PropTypes from 'prop-types';
import isEqual from 'fast-deep-equal';
import { omit } from 'common/utils/omit';
import { LEVEL_STEP, LEVEL_SUITE, LEVEL_TEST, LEVEL_LAUNCH } from 'common/constants/launchLevels';
import {
  StepLevelEntities,
  SuiteLevelEntities,
  LaunchLevelEntities,
} from 'pages/inside/common/filterEntitiesGroups';
import { filterValueShape } from '../propTypes';
import { FilterEntitiesAddHandlerContainer } from './filterEntitiesAddHandlerContainer';

const ENTITY_PROVIDERS = {
  [LEVEL_LAUNCH]: LaunchLevelEntities,
  [LEVEL_SUITE]: SuiteLevelEntities,
  [LEVEL_TEST]: SuiteLevelEntities,
  [LEVEL_STEP]: StepLevelEntities,
};

export class FilterEntitiesContainer extends Component {
  static propTypes = {
    entities: PropTypes.objectOf(filterValueShape),
    onChange: PropTypes.func,
    render: PropTypes.func.isRequired,
    level: PropTypes.string,
    entitiesProvider: PropTypes.elementType,
  };

  static defaultProps = {
    entities: {},
    onChange: () => {},
    entitiesProvider: null,
    level: '',
  };

  static getDerivedStateFromProps(props, state) {
    if (props.entities !== state.values && !isEqual(props.entities, state.prevEntities)) {
      return {
        errors: {},
        values: props.entities,
        prevEntities: props.entities,
        visibleFilters: Object.keys(props.entities),
      };
    }
    return null;
  }
  state = {
    errors: {},
    values: this.props.entities,
    prevEntities: this.props.entities,
    visibleFilters: Object.keys(this.props.entities),
  };
  collectEntities = (values) =>
    Object.keys(values).reduce((acc, entityId) => {
      const value = values[entityId];
      return this.isValidChange(entityId)
        ? { ...acc, [entityId]: value }
        : { ...acc, [entityId]: { ...value, value: '' } };
    }, {});

  isValidChange = (entityId) => {
    const value = this.state.values[entityId] && this.state.values[entityId].value;
    if (!value) {
      return true;
    }
    return !this.state.errors[entityId];
  };

  handleChange = (entityId, value) => {
    this.setState(
      {
        values: { ...this.state.values, [entityId]: { ...this.state.values[entityId], ...value } },
      },
      () =>
        this.isValidChange(entityId) &&
        this.props.onChange(this.collectEntities(this.state.values)),
    );
  };

  handleValidate = (entityId, error) => {
    if (!error) {
      this.setState({ errors: omit(this.state.errors, [entityId]) });
    } else {
      this.setState({ errors: { ...this.state.errors, [entityId]: error } });
    }
  };

  mergeEntityValue = (oldEntity, newEntity) => {
    if (oldEntity && oldEntity.value) {
      const oldEntityValue = oldEntity.value;
      const isNewValueValid =
        newEntity.value && oldEntityValue.split(',').indexOf(newEntity.value) === -1;

      return {
        ...oldEntity,
{'fraction_non_alphanumeric': 0.13342776203966006, 'fraction_numerical': 0.0032577903682719546, 'mean_word_length': 3.2976262933657945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5833283', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2126, 'n_words': 504}