<commit_before>import React from 'react';
import Proptypes from 'prop-types';
import isEqual from 'lodash/isEqual';
import debounce from 'lodash/debounce';

// redux
import { connect } from 'react-redux';

// redactions
import { setTmpSources } from 'redactions/admin/sources';

// components
import Field from 'components/form/Field';
import Input from 'components/form/Input';

// constants
import { SOURCE_ELEMENTS } from 'components/admin/metadata/form/constants';

class Source extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      values: props.values
    };
  }

  onChange = (name, value) => {
    this.setState({
      values: {
        ...this.state.values,
        [name]: value
      }
    }, () => {
      this.onAddSource(this.state.values, this.props.index);
    });
  }

  componentWillReceiveProps(nextProps) {
    if(!isEqual(this.props.values, nextProps.values)) {
      this.setState({
        values: nextProps.values
      });
    }
  }

  onAddSource = (value, index) => {
    const sources = [...this.props.tmpSources];
    let existentSource = sources[index];

    if (existentSource) {
      sources.splice(index, 1, { ...value, id: index });
    } else {
      sources.push({...value, id: index });
    }

    this.props.setTmpSources(sources);
  }

  onRemoveSource(index) {
    const sources = [...this.props.tmpSources];
    sources.splice(index, 1);
    if (!sources.length) sources.push({});
    this.props.setTmpSources(sources);

    delete SOURCE_ELEMENTS.elements[`source-name-${index + 1}`];
    delete SOURCE_ELEMENTS.elements[`source-description-${index + 1}`];
  }

  render() {
    const { index } = this.props;
    const { values } = this.state;

    return (
      <div className="c-source-item">
        <div className="l-row row">
          <div className="column small-4">
            <Field
              ref={(c) => { if (c) SOURCE_ELEMENTS.elements[`source-name-${index + 1}`] = c; }}
              onChange={value => this.onChange('source-name', value)}
              validations={['url', 'required']}
              properties={{
                name: 'source-name',
                label: 'Source',
                type: 'text',
                default: values['source-name'] || '',
                value: values['source-name'] || ''
              }}
            >
              {Input}
            </Field>
          </div>

          <div className="column small-5">
            <Field
              ref={(c) => { if (c) SOURCE_ELEMENTS.elements[`source-description-${index + 1}`] = c;}}
              onChange={value => this.onChange('source-description', value)}<commit_msg>Removes input validations<commit_after>import React from 'react';
import Proptypes from 'prop-types';
import isEqual from 'lodash/isEqual';
import debounce from 'lodash/debounce';

// redux
import { connect } from 'react-redux';

// redactions
import { setTmpSources } from 'redactions/admin/sources';

// components
import Field from 'components/form/Field';
import Input from 'components/form/Input';

// constants
import { SOURCE_ELEMENTS } from 'components/admin/metadata/form/constants';

class Source extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      values: props.values
    };
  }

  onChange = (name, value) => {
    this.setState({
      values: {
        ...this.state.values,
        [name]: value
      }
    }, () => {
      this.onAddSource(this.state.values, this.props.index);
    });
  }

  componentWillReceiveProps(nextProps) {
    if(!isEqual(this.props.values, nextProps.values)) {
      this.setState({
        values: nextProps.values
      });
    }
  }

  onAddSource = (value, index) => {
    const sources = [...this.props.tmpSources];
    let existentSource = sources[index];

    if (existentSource) {
      sources.splice(index, 1, { ...value, id: index });
    } else {
      sources.push({...value, id: index });
    }

    this.props.setTmpSources(sources);
  }

  onRemoveSource(index) {
    const sources = [...this.props.tmpSources];
    sources.splice(index, 1);
    if (!sources.length) sources.push({ id: -1 });
    this.props.setTmpSources(sources);

    delete SOURCE_ELEMENTS.elements[`source-name-${index + 1}`];
    delete SOURCE_ELEMENTS.elements[`source-description-${index + 1}`];
  }

  render() {
    const { index } = this.props;
    const { values } = this.state;

    return (
      <div className="c-source-item">
        <div className="l-row row">
          <div className="column small-4">
            <Field
              ref={(c) => { if (c) SOURCE_ELEMENTS.elements[`source-name-${index + 1}`] = c; }}
              onChange={value => this.onChange('source-name', value)}
              validations={[]}
              properties={{
                name: 'source-name',
                label: 'Source',
                type: 'text',
                default: values['source-name'] || '',
                value: values['source-name'] || ''
              }}
            >
              {Input}
            </Field>
          </div>

          <div className="column small-5">
            <Field
              ref={(c) => { if (c) SOURCE_ELEMENTS.elements[`source-description-${index + 1}`] = c;}}
              onChange={value => this.onChange('source-description', value)}
{'fraction_non_alphanumeric': 0.15789473684210525, 'fraction_numerical': 0.003184116875819442, 'mean_word_length': 2.2800982800982803, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '4810357', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1609, 'n_words': 316}