<commit_before>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ClassSet from 'react-classset';
import Dictionary from 'utils/dictionary/dictionary';
import { COMPONENT_TYPE } from 'constants/pogues-constants';
import DropZone from 'questionnaire/components/drop-zone/drop-zone';

import { DragSource, DropTarget } from 'react-dnd';
import { PropType, componentSource, cardTarget, collect } from 'utils/component/component-dragndrop';

const { QUESTION, SEQUENCE, SUBSEQUENCE } = COMPONENT_TYPE;

@DropTarget(PropType, cardTarget, (connect, monitor) => ({
  connectDropTarget: connect.dropTarget(),
  canDrop: monitor.canDrop(),
  isOver: monitor.isOver({ shallow: true }),
}))
@DragSource(PropType, componentSource, collect)
class QuestionnaireElement extends Component {
  static propTypes = {
    name: PropTypes.string.isRequired,
    id: PropTypes.string.isRequired,
    parent: PropTypes.string.isRequired,
    parentType: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    label: PropTypes.string.isRequired,
    weight: PropTypes.number.isRequired,
    selected: PropTypes.bool.isRequired,
    children: PropTypes.arrayOf(PropTypes.object),
    childrenId: PropTypes.arrayOf(PropTypes.string),
    onClickElement: PropTypes.func.isRequired,
    onClickDetail: PropTypes.func.isRequired,
    connectDragSource: PropTypes.func.isRequired,
    moveComponent: PropTypes.func.isRequired,
    canDrop: PropTypes.bool.isRequired,
    connectDropTarget: PropTypes.func.isRequired,
    isOver: PropTypes.bool.isRequired,
  };
  static defaultProps = {
    children: [],
  };
  componentDidMount() {
    this.ensureSelected();
  }

  componentDidUpdate() {
    this.ensureSelected();
  }

  ensureSelected() {
    if (this.props.selected) {
      this.node.scrollIntoView();
    }
  }

  render() {
    const {
      connectDragSource,
      connectDropTarget,
      isOver,
      canDrop,
      name,
      id,
      type,
      label,
      selected,
      children,
      childrenId,
      onClickElement,
      onClickDetail,
    } = this.props;

    let style = {};

    /**
     * If the component can have children but not yet, we will add the dragged component as child
     */
    if ((type === SEQUENCE || type === SUBSEQUENCE) && childrenId.length === 0) {
      style = {
        marginLeft: '25px',
      };
    }

    /**<commit_msg>Improve Drag&Drop<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ClassSet from 'react-classset';
import Dictionary from 'utils/dictionary/dictionary';
import { COMPONENT_TYPE } from 'constants/pogues-constants';
import DropZone from 'questionnaire/components/drop-zone/drop-zone';

import { DragSource, DropTarget } from 'react-dnd';
import { PropType, componentSource, cardTarget, collect } from 'utils/component/component-dragndrop';
import { couldInsertAsChild } from 'utils/component/component-utils';

const { QUESTION, SEQUENCE, SUBSEQUENCE } = COMPONENT_TYPE;

@DropTarget(PropType, cardTarget, (connect, monitor) => ({
  connectDropTarget: connect.dropTarget(),
  canDrop: monitor.canDrop(),
  isOver: monitor.isOver({ shallow: true }),
  draggedItem: monitor.getItem(),
}))
@DragSource(PropType, componentSource, collect)
class QuestionnaireElement extends Component {
  static propTypes = {
    name: PropTypes.string.isRequired,
    id: PropTypes.string.isRequired,
    parent: PropTypes.string.isRequired,
    parentType: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    label: PropTypes.string.isRequired,
    weight: PropTypes.number.isRequired,
    selected: PropTypes.bool.isRequired,
    children: PropTypes.arrayOf(PropTypes.object),
    childrenId: PropTypes.arrayOf(PropTypes.string),
    onClickElement: PropTypes.func.isRequired,
    onClickDetail: PropTypes.func.isRequired,
    connectDragSource: PropTypes.func.isRequired,
    moveComponent: PropTypes.func.isRequired,
    canDrop: PropTypes.bool.isRequired,
    connectDropTarget: PropTypes.func.isRequired,
    isOver: PropTypes.bool.isRequired,
    draggedItem: PropTypes.object,
  };
  static defaultProps = {
    children: [],
  };
  componentDidMount() {
    this.ensureSelected();
  }

  componentDidUpdate() {
    this.ensureSelected();
  }

  ensureSelected() {
    if (this.props.selected) {
      this.node.scrollIntoView();
    }
  }

  render() {
    const {
      connectDragSource,
      connectDropTarget,
      isOver,
      canDrop,
      name,
      id,
      type,
      label,
      selected,
      children,
      childrenId,
      onClickElement,
      onClickDetail,
      parentType,
      draggedItem,
    } = this.props;

    let style = {};

    /**
     * If the component can have children but not yet, we will add the dragged component as child
     */
    if (couldInsertAsChild(draggedItem, this.props)) {
      style = {
        marginLeft: '25px',
      };
    }

    /**
{'fraction_non_alphanumeric': 0.11317329029655034, 'fraction_numerical': 0.0010086746015735323, 'mean_word_length': 3.5319926873857406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1787199', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1466, 'n_words': 313}