<commit_before>import React from 'react';
import PropTypes from 'prop-types';

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

// Services
import DatasetsService from 'services/DatasetsService';
import LayersService from 'services/LayersService';
import { toastr } from 'react-redux-toastr';

// Constants
import { STATE_DEFAULT, FORM_ELEMENTS } from 'components/admin/layers/form/constants';

import { setLayerInteractionError } from 'components/admin/layers/form/layer-preview/layer-preview-actions';

// Components
import Step1 from 'components/admin/layers/form/steps/Step1';
import Navigation from 'components/form/Navigation';
import Spinner from 'components/ui/Spinner';

import LayerManager from 'utils/layers/LayerManager';

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

    const formObj = props.dataset ?
      Object.assign({}, STATE_DEFAULT.form,
        { dataset: props.dataset, application: props.application }) :
      Object.assign({}, STATE_DEFAULT.form, {
        application: props.application
      });

    this.state = Object.assign({}, STATE_DEFAULT, {
      id: props.id,
      dataset: props.dataset,
      datasets: [],
      form: formObj,
      loading: !!props.id
    });

    // Service
    this.datasetsService = new DatasetsService({
      authorization: props.authorization,
      language: props.locale
    });

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

    this.layerManager = new LayerManager(null, {
      layersUpdated: (valid, err) => {
        this.props.dispatch(setLayerInteractionError(valid ? false : err));
      }
    });

    // BINDINGS
    this.onSubmit = this.onSubmit.bind(this);
    this.onChange = this.onChange.bind(this);
    this.onChangeDataset = this.onChangeDataset.bind(this);
    this.onStepChange = this.onStepChange.bind(this);
  }

  componentDidMount() {
    const { id } = this.state;

    const promises = [
      this.datasetsService.fetchAllData({})
    ];

    // Add the dashboard promise if the id exists
    if (id) {
      promises.push(this.service.fetchData({ id }));
    }

    Promise.all(promises)
      .then((response) => {
        const datasets = response[0];
        const current = response[1];

        const formState = (id) ? this.setFormFromParams(current) : this.state.form;

        this.setState({
          // CURRENT LAYER
          form: formState,
          loading: false,
          // CURRENT DATASET
          dataset: formState.dataset,
          // OPTIONS
          datasets: datasets.map(d => ({ label: d.name, value: d.id }))
        });
      })
      .catch((err) => {
        toastr.error('Error', err);
      });
  }

  /**
   * UI EVENTS
   * - onSubmit
   * - onChange
   * - onChangeDataset
  */
  onSubmit(event) {
    if (event) event.preventDefault();

    // Validate the form
    FORM_ELEMENTS.validate(this.state.step);

    // Set a timeout due to the setState function of react
    setTimeout(() => {
      // Validate all the inputs on the current step
      const valid = FORM_ELEMENTS.isValid(this.state.step);
      const { interactions } = this.props;

      let interactionConfig = this.state.form.interactionConfig;
      // Grab all the interactions from the redux store
      if (this.state.form.provider !== 'gee') {
        interactionConfig = Object.assign(
          {},
          this.state.form.interactionConfig,
          { output: interactions.added }
        );
      }

      const form = Object.assign({}, this.state.form, { interactionConfig });

      // Verify that layers are valid, otherwise render error
      const { adminLayerPreview } = this.props;
      const { layerGroups } = adminLayerPreview;
      const cartoLayer = layerGroups.length && 'layers' in layerGroups[0] ?
        layerGroups[0].layers.filter(layer => layer.provider === 'cartodb') : [];

      if (valid) {
        // Start the submitting
        this.setState({ submitting: true });
        this.props.dispatch(setLayerInteractionError(false));
<commit_msg>hotfix: bug in layers interactivity form from the back office<commit_after>import React from 'react';
import PropTypes from 'prop-types';

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

// Services
import DatasetsService from 'services/DatasetsService';
import LayersService from 'services/LayersService';
import { toastr } from 'react-redux-toastr';

// Constants
import { STATE_DEFAULT, FORM_ELEMENTS } from 'components/admin/layers/form/constants';

import { setLayerInteractionError } from 'components/admin/layers/form/layer-preview/layer-preview-actions';

// Components
import Step1 from 'components/admin/layers/form/steps/Step1';
import Navigation from 'components/form/Navigation';
import Spinner from 'components/ui/Spinner';

import LayerManager from 'utils/layers/LayerManager';

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

    const formObj = props.dataset ?
      Object.assign({}, STATE_DEFAULT.form,
        { dataset: props.dataset, application: props.application }) :
      Object.assign({}, STATE_DEFAULT.form, {
        application: props.application
      });

    this.state = Object.assign({}, STATE_DEFAULT, {
      id: props.id,
      dataset: props.dataset,
      datasets: [],
      form: formObj,
      loading: !!props.id
    });

    // Service
    this.datasetsService = new DatasetsService({
      authorization: props.authorization,
      language: props.locale
    });

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

    this.layerManager = new LayerManager(null, {
      layersUpdated: (valid, err) => {
        this.props.dispatch(setLayerInteractionError(valid ? false : err));
      }
    });

    // BINDINGS
    this.onSubmit = this.onSubmit.bind(this);
    this.onChange = this.onChange.bind(this);
    this.onChangeDataset = this.onChangeDataset.bind(this);
    this.onStepChange = this.onStepChange.bind(this);
  }

  componentDidMount() {
    const { id } = this.state;

    const promises = [
      this.datasetsService.fetchAllData({})
    ];

    // Add the dashboard promise if the id exists
    if (id) {
      promises.push(this.service.fetchData({ id }));
    }

    Promise.all(promises)
      .then((response) => {
        const datasets = response[0];
        const current = response[1];

        const formState = (id) ? this.setFormFromParams(current) : this.state.form;

        this.setState({
          // CURRENT LAYER
          form: formState,
          loading: false,
          // CURRENT DATASET
          dataset: formState.dataset,
          // OPTIONS
          datasets: datasets.map(d => ({ label: d.name, value: d.id }))
        });
      })
      .catch((err) => {
        toastr.error('Error', err);
      });
  }

  /**
   * UI EVENTS
   * - onSubmit
   * - onChange
   * - onChangeDataset
  */
  onSubmit(event) {
    if (event) event.preventDefault();

    // Validate the form
    FORM_ELEMENTS.validate(this.state.step);

    // Set a timeout due to the setState function of react
    setTimeout(() => {
      // Validate all the inputs on the current step
      const valid = FORM_ELEMENTS.isValid(this.state.step);
      const { interactions } = this.props;

      let interactionConfig = this.state.form.interactionConfig;
      // Grab all the interactions from the redux store
      if (this.state.form.provider === 'cartodb') {
        interactionConfig = Object.assign(
          {},
          this.state.form.interactionConfig,
          { output: interactions.added }
        );
      }

      const form = Object.assign({}, this.state.form, { interactionConfig });

      // Verify that layers are valid, otherwise render error
      const { adminLayerPreview } = this.props;
      const { layerGroups } = adminLayerPreview;
      const cartoLayer = layerGroups.length && 'layers' in layerGroups[0] ?
        layerGroups[0].layers.filter(layer => layer.provider === 'cartodb') : [];

      if (valid) {
        // Start the submitting
        this.setState({ submitting: true });
        this.props.dispatch(setLayerInteractionError(false));

{'fraction_non_alphanumeric': 0.12380835981422635, 'fraction_numerical': 0.0014666340747983377, 'mean_word_length': 2.6728007181328546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5552060', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2336, 'n_words': 533}