<commit_before>import React from 'react';
import omit from 'lodash/omit';

import { Autobind } from 'es-decorators';

// Service
import DatasetsService from 'services/DatasetsService';

// Contants
import { STATE_DEFAULT, FORM_ELEMENTS } from 'components/admin/metadata/form/constants';

// Components
import Navigation from 'components/form/Navigation';
import Step1 from 'components/admin/metadata/form/steps/Step1';


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

    const newState = Object.assign({}, STATE_DEFAULT, {
      metadata: [],
      columns: [],
      loading: !!props.dataset,
      form: Object.assign({}, STATE_DEFAULT.form, {
        application: props.application,
        authorization: props.authorization
      })
    });

    this.service = new DatasetsService({
      authorization: props.authorization
    });

    this.state = newState;
  }

  componentDidMount() {
    if (this.props.dataset) {
      this.service.fetchData({ id: this.props.dataset, includes: 'metadata' })
        .then((data) => {
          const metadata = data.attributes.metadata;

          this.setState({
            form: (metadata && metadata.length) ?
              this.setFormFromParams(metadata[0].attributes) :
              this.state.form,
            metadata,
            // Stop the loading
            loading: false
          });
        })
        .catch((err) => {
          this.setState({ loading: false });
          console.error(err);
        });

      this.service.fetchFields({ id: this.props.dataset })
        .then((data) => {
          this.setState({
            columns: data
          });
        })
        .catch((err) => {
          console.error(err);
        });
    }
  }

  /**
   * UI EVENTS
   * - onSubmit
   * - onChange
  */
  @Autobind
  onSubmit(event) {
    event.preventDefault();

    // Validate the form
    FORM_ELEMENTS.validate();

    // Set a timeout due to the setState function of react
    setTimeout(() => {
      const valid = FORM_ELEMENTS.isValid();
      if (valid) {
        const { dataset } = this.props;
        const { metadata, form } = this.state;

        // Start the submitting
        this.setState({ submitting: true });

        // Check if the metadata alerady exists
        const thereIsMetadata = Boolean(metadata.find((m) => {
          const hasLang = m.attributes.language === form.language;
          const hasApp = m.attributes.application === form.application;

          return hasLang && hasApp;
        }));

        // Set the request
        const requestOptions = {
          type: (dataset && thereIsMetadata) ? 'PATCH' : 'POST',
          omit: ['authorization']
        };

        console.log(omit(this.state.form, requestOptions.omit));
        debugger;
        // Save the data
        this.service.saveMetadata({
          type: requestOptions.type,
          id: dataset,
          body: omit(this.state.form, requestOptions.omit)
        })
          .then(() => {
            const successMessage = 'Metadata has been uploaded correctly';
            alert(successMessage);

            this.props.onSubmit && this.props.onSubmit();
          })
          .catch((err) => {
            this.setState({ submitting: false });
            console.error(err);
          });
      }
    }, 0);
  }

  @Autobind
  onChange(obj) {
    const form = Object.assign({}, this.state.form, obj.form);<commit_msg>Metadata: column alias added to metadata form<commit_after>import React from 'react';
import omit from 'lodash/omit';

import { Autobind } from 'es-decorators';

// Service
import DatasetsService from 'services/DatasetsService';

// Contants
import { STATE_DEFAULT, FORM_ELEMENTS } from 'components/admin/metadata/form/constants';

// Components
import Navigation from 'components/form/Navigation';
import Step1 from 'components/admin/metadata/form/steps/Step1';


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

    const newState = Object.assign({}, STATE_DEFAULT, {
      metadata: [],
      columns: [],
      loading: !!props.dataset,
      form: Object.assign({}, STATE_DEFAULT.form, {
        application: props.application,
        authorization: props.authorization
      })
    });

    this.service = new DatasetsService({
      authorization: props.authorization
    });

    this.state = newState;
  }

  componentDidMount() {
    if (this.props.dataset) {
      this.service.fetchData({ id: this.props.dataset, includes: 'metadata' })
        .then((data) => {
          const metadata = data.attributes.metadata;

          this.setState({
            form: (metadata && metadata.length) ?
              this.setFormFromParams(metadata[0].attributes) :
              this.state.form,
            metadata,
            // Stop the loading
            loading: false
          });
        })
        .catch((err) => {
          this.setState({ loading: false });
          console.error(err);
        });

      this.service.fetchFields({ id: this.props.dataset })
        .then((data) => {
          this.setState({
            columns: data
          });
        })
        .catch((err) => {
          console.error(err);
        });
    }
  }

  /**
   * UI EVENTS
   * - onSubmit
   * - onChange
  */
  @Autobind
  onSubmit(event) {
    event.preventDefault();

    // Validate the form
    FORM_ELEMENTS.validate();

    // Set a timeout due to the setState function of react
    setTimeout(() => {
      const valid = FORM_ELEMENTS.isValid();
      if (valid) {
        const { dataset } = this.props;
        const { metadata, form } = this.state;

        // Start the submitting
        this.setState({ submitting: true });

        // Check if the metadata alerady exists
        const thereIsMetadata = Boolean(metadata.find((m) => {
          const hasLang = m.attributes.language === form.language;
          const hasApp = m.attributes.application === form.application;

          return hasLang && hasApp;
        }));

        // Set the request
        const requestOptions = {
          type: (dataset && thereIsMetadata) ? 'PATCH' : 'POST',
          omit: ['authorization']
        };

        // Save the data
        this.service.saveMetadata({
          type: requestOptions.type,
          id: dataset,
          body: omit(this.state.form, requestOptions.omit)
        })
          .then(() => {
            const successMessage = 'Metadata has been uploaded correctly';
            alert(successMessage);

            this.props.onSubmit && this.props.onSubmit();
          })
          .catch((err) => {
            this.setState({ submitting: false });
            console.error(err);
          });
      }
    }, 0);
  }

  @Autobind
  onChange(obj) {
    const form = Object.assign({}, this.state.form, obj.form);
{'fraction_non_alphanumeric': 0.12980558397895045, 'fraction_numerical': 0.00116941967548604, 'mean_word_length': 2.011443661971831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '125632', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1865, 'n_words': 418}