<commit_before>import Component from '@ember/component';
import { A } from '@ember/array';
import { run } from '@ember/runloop';
import { alias, sort } from '@ember/object/computed';
import { computed } from '@ember/object';
import { task, all } from 'ember-concurrency';
import { inject as service } from '@ember/service';
import { isBlank } from '@ember/utils';
import $ from 'jquery';

export default Component.extend({
  store: service(),
  notify: service(),

  isLoadingQuestions: true,

  questionsSorting: ['sortOrder'],
  fullQuestions: sort('surveyTemplate.questionsNotDeleted', 'questionsSorting'),
  sortedQuestions: sort('surveyTemplate.filteredQuestions', 'questionsSorting'),
  isFullyEditable: alias('surveyTemplate.fullyEditable'),

  init() {
    this._super(...arguments);
    this.selectedQuestions = A();
  },

  loadingProgress: computed('surveyTemplate.questions.@each.isLoading', function() {
    let questions = this.get('surveyTemplate.questions');
    let total = questions.get('length');
    let loaded = questions.filterBy('isLoading', false).length;
    if (total === loaded) {
      run.next(this, function() {
        this.set('isLoadingQuestions', false);
      });
    }
    return parseInt((loaded * 100) / total);
  }),

  deleteQuestionTask: task(function*(question, row) {
    question.deleteRecord();
    yield question.save();
    this._recursivelyDelete(question.get('id'));
    if (row) {
      $('.delete-confirm', row).fadeOut();
    }
  }),

  _recursivelyDelete(questionId) {
    let childrenQuestions = this.get('surveyTemplate.questions').filterBy('parentId', questionId.toString());
    for (let childQuestion of childrenQuestions) {
      this._recursivelyDelete(childQuestion.get('id'));
      childQuestion.deleteRecord();
    }
  },

  deleteQuestionsTask: task(function*() {
    let selectedQuestions = this.get('selectedQuestions');

    let toDeleteQuestions = this._filterSectionsAndRepeaters(selectedQuestions);
    let deleteQuestionTask = this.get('deleteQuestionTask');
    try {
      yield all(toDeleteQuestions.map(question => deleteQuestionTask.perform(question)));
      this.get('notify').success('Questions successfully deleted');
      yield this.get('updateSortOrderTask').perform(this.get('fullQuestions'), true);
    } catch (e) {
      console.log('Error:', e); // eslint-disable-line no-console
      this.get('notify').alert('There was an error trying to delete questions');
    }
    this.unSelectAll();
  }),

  duplicateQuestionsTask: task(function*() {
    let selectedQuestions = this.get('selectedQuestions');
    let store = this.get('store');
    let surveyTemplate = this.get('surveyTemplate');

    // If there are entire sections / repeaters then dont copy them all
    let toDuplicateQuestions = this._filterSectionsAndRepeaters(selectedQuestions);

    try {
      // make sure the list is clean in terms of sorting values
      yield all(
        toDuplicateQuestions.map(question => {
          let params = { section: false };
          if (question.get('isContainer') || question.get('isARepeater')) {
            params.section = true;
          }
          return question.duplicate(params);
        })
      );
      yield surveyTemplate.reload();
      yield surveyTemplate.hasMany('questions').reload();
      this.get('notify').success('Questions successfully duplicated');
    } catch (e) {
      console.log('Error:', e); // eslint-disable-line no-console
      this.get('notify').alert('There was an error trying to duplicate questions');
    }
    this.unSelectAll();
  }).drop(),
<commit_msg>fix eslint<commit_after>import Component from '@ember/component';
import { A } from '@ember/array';
import { run } from '@ember/runloop';
import { alias, sort } from '@ember/object/computed';
import { computed } from '@ember/object';
import { task, all } from 'ember-concurrency';
import { inject as service } from '@ember/service';
import { isBlank } from '@ember/utils';
import $ from 'jquery';

export default Component.extend({
  notify: service(),

  isLoadingQuestions: true,

  questionsSorting: ['sortOrder'],
  fullQuestions: sort('surveyTemplate.questionsNotDeleted', 'questionsSorting'),
  sortedQuestions: sort('surveyTemplate.filteredQuestions', 'questionsSorting'),
  isFullyEditable: alias('surveyTemplate.fullyEditable'),

  init() {
    this._super(...arguments);
    this.selectedQuestions = A();
  },

  loadingProgress: computed('surveyTemplate.questions.@each.isLoading', function() {
    let questions = this.get('surveyTemplate.questions');
    let total = questions.get('length');
    let loaded = questions.filterBy('isLoading', false).length;
    if (total === loaded) {
      run.next(this, function() {
        this.set('isLoadingQuestions', false);
      });
    }
    return parseInt((loaded * 100) / total);
  }),

  deleteQuestionTask: task(function*(question, row) {
    question.deleteRecord();
    yield question.save();
    this._recursivelyDelete(question.get('id'));
    if (row) {
      $('.delete-confirm', row).fadeOut();
    }
  }),

  _recursivelyDelete(questionId) {
    let childrenQuestions = this.get('surveyTemplate.questions').filterBy('parentId', questionId.toString());
    for (let childQuestion of childrenQuestions) {
      this._recursivelyDelete(childQuestion.get('id'));
      childQuestion.deleteRecord();
    }
  },

  deleteQuestionsTask: task(function*() {
    let selectedQuestions = this.get('selectedQuestions');

    let toDeleteQuestions = this._filterSectionsAndRepeaters(selectedQuestions);
    let deleteQuestionTask = this.get('deleteQuestionTask');
    try {
      yield all(toDeleteQuestions.map(question => deleteQuestionTask.perform(question)));
      this.get('notify').success('Questions successfully deleted');
      yield this.get('updateSortOrderTask').perform(this.get('fullQuestions'), true);
    } catch (e) {
      console.log('Error:', e); // eslint-disable-line no-console
      this.get('notify').alert('There was an error trying to delete questions');
    }
    this.unSelectAll();
  }),

  duplicateQuestionsTask: task(function*() {
    let selectedQuestions = this.get('selectedQuestions');
    let surveyTemplate = this.get('surveyTemplate');

    // If there are entire sections / repeaters then dont copy them all
    let toDuplicateQuestions = this._filterSectionsAndRepeaters(selectedQuestions);

    try {
      // make sure the list is clean in terms of sorting values
      yield all(
        toDuplicateQuestions.map(question => {
          let params = { section: false };
          if (question.get('isContainer') || question.get('isARepeater')) {
            params.section = true;
          }
          return question.duplicate(params);
        })
      );
      yield surveyTemplate.reload();
      yield surveyTemplate.hasMany('questions').reload();
      this.get('notify').success('Questions successfully duplicated');
    } catch (e) {
      console.log('Error:', e); // eslint-disable-line no-console
      this.get('notify').alert('There was an error trying to duplicate questions');
    }
    this.unSelectAll();
  }).drop(),

{'fraction_non_alphanumeric': 0.1428771733034212, 'fraction_numerical': 0.0008412787436904094, 'mean_word_length': 3.8163403106009453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2388785', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1925, 'n_words': 434}