<commit_before>import React, {Component} from 'react';
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from 'react-sortable-hoc';
import { registerComponent, Components, withCurrentUser } from 'meteor/vulcan:core';
import IconButton from 'material-ui/IconButton';
import RemoveIcon from 'material-ui/svg-icons/content/remove-circle-outline';
import DragIcon from 'material-ui/svg-icons/editor/drag-handle';
import {List, ListItem} from 'material-ui/List';


const SortableItem = SortableElement(({sequenceId, currentUser, removeItem}) =>
  <li className="sequences-list-editor-item">
    <Components.SequencesListEditorItem documentId={sequenceId} currentUser={currentUser} removeItem={removeItem} />
  </li>
);

const SortableList = SortableContainer(({items, currentUser, removeItem}) => {
  if (items) {
    return (
      <div>
        {items.map((sequenceId, index) => (
          <SortableItem key={`item-${index}`} removeItem={removeItem} index={index} sequenceId={sequenceId} currentUser={currentUser}/>
        ))}
      </div>
    );
  } else {
    return <div>No Sequences added yet</div>
  }

});

class SequencesListEditor extends Component {
  constructor(props, context) {
    super(props, context);
    const fieldName = props.name;
    let sequenceIds = [];
    if (props.document[fieldName]) {
      sequenceIds = JSON.parse(JSON.stringify(props.document[fieldName]));
    }
    this.state = {
      sequenceIds: sequenceIds,
    }
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: sequenceIds});

    const addToSuccessForm = this.context.addToSuccessForm;
    addToSuccessForm((results) => this.resetSequenceIds(results));
  }
  onSortEnd = ({oldIndex, newIndex}) => {
    console.log("onSortEnd", oldIndex, newIndex, this.state.sequenceIds);
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    const newIds = arrayMove(this.state.sequenceIds, oldIndex, newIndex);
    this.setState({
      sequenceIds: newIds,
    });
    addValues({[fieldName]: newIds});
  };
  addSequenceId = (sequenceId) => {
    const newIds = [...this.state.sequenceIds, sequenceId];
    this.setState({
      sequenceIds: newIds,
    })
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: newIds});
  }
  removeSequenceId = (sequenceId) => {
    const newIds = _.without(this.state.sequenceIds, sequenceId);
    console.log("removeSequenceId", newIds);
    this.setState({
      sequenceIds: newIds || [],
    })
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: newIds});
  }
  resetSequenceIds = (args) => {
    this.setState({
      sequenceIds: [],
    })
    return args;
  }<commit_msg>Fixed sequencesListEditor when dealing with empty sequences<commit_after>import React, {Component} from 'react';
import {SortableContainer, SortableElement, SortableHandle, arrayMove} from 'react-sortable-hoc';
import { registerComponent, Components, withCurrentUser } from 'meteor/vulcan:core';
import IconButton from 'material-ui/IconButton';
import RemoveIcon from 'material-ui/svg-icons/content/remove-circle-outline';
import DragIcon from 'material-ui/svg-icons/editor/drag-handle';
import {List, ListItem} from 'material-ui/List';


const SortableItem = SortableElement(({sequenceId, currentUser, removeItem}) =>
  <li className="sequences-list-editor-item">
    <Components.SequencesListEditorItem documentId={sequenceId} currentUser={currentUser} removeItem={removeItem} />
  </li>
);

const SortableList = SortableContainer(({items, currentUser, removeItem}) => {
  if (items) {
    return (
      <div>
        {items.map((sequenceId, index) => (
          <SortableItem key={`item-${index}`} removeItem={removeItem} index={index} sequenceId={sequenceId} currentUser={currentUser}/>
        ))}
      </div>
    );
  } else {
    return <div>No Sequences added yet</div>
  }

});

class SequencesListEditor extends Component {
  constructor(props, context) {
    super(props, context);
    const fieldName = props.name;
    let sequenceIds = [];
    if (props.document[fieldName]) {
      sequenceIds = _.compact(JSON.parse(JSON.stringify(props.document[fieldName])));
    }
    this.state = {
      sequenceIds: sequenceIds,
    }
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: sequenceIds});

    const addToSuccessForm = this.context.addToSuccessForm;
    addToSuccessForm((results) => this.resetSequenceIds(results));
  }
  onSortEnd = ({oldIndex, newIndex}) => {
    console.log("onSortEnd", oldIndex, newIndex, this.state.sequenceIds);
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    const newIds = arrayMove(this.state.sequenceIds, oldIndex, newIndex);
    this.setState({
      sequenceIds: newIds,
    });
    addValues({[fieldName]: newIds});
  };
  addSequenceId = (sequenceId) => {
    const newIds = _.compact([...this.state.sequenceIds, sequenceId]);
    this.setState({
      sequenceIds: newIds,
    })
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: newIds});
  }
  removeSequenceId = (sequenceId) => {
    const newIds = _.without(this.state.sequenceIds, sequenceId);
    console.log("removeSequenceId", newIds);
    this.setState({
      sequenceIds: newIds || [],
    })
    const fieldName = this.props.name;
    const addValues = this.context.addToAutofilledValues;
    addValues({[fieldName]: newIds});
  }
  resetSequenceIds = (args) => {
    this.setState({
      sequenceIds: [],
    })
    return args;
  }
{'fraction_non_alphanumeric': 0.13724808895066018, 'fraction_numerical': 0.0, 'mean_word_length': 4.054433713784021, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, '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': '1089544', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1735, 'n_words': 338}