BigW Consortium Gitlab

task_list.js 1.34 KB
Newer Older
1
/* global Flash */
2

3
import 'deckar01-task_list';
4

5
export default class TaskList {
6 7 8
  constructor(options = {}) {
    this.selector = options.selector;
    this.dataType = options.dataType;
9
    this.fieldName = options.fieldName;
Simon Knox committed
10
    this.onSuccess = options.onSuccess || (() => {});
11 12 13 14 15 16 17 18 19 20
    this.onError = function showFlash(response) {
      let errorMessages = '';

      if (response.responseJSON) {
        errorMessages = response.responseJSON.errors.join(' ');
      }

      return new Flash(errorMessages || 'Update failed', 'alert');
    };

21 22 23 24
    this.init();
  }

  init() {
25 26
    // Prevent duplicate event bindings
    this.disable();
Simon Knox committed
27
    $(`${this.selector} .js-task-list-container`).taskList('enable');
28
    $(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
29 30 31
  }

  disable() {
Simon Knox committed
32
    $(`${this.selector} .js-task-list-container`).taskList('disable');
Simon Knox committed
33
    $(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
34 35 36
  }

  update(e) {
37
    const $target = $(e.target);
Simon Knox committed
38
    const patchData = {};
39
    patchData[this.dataType] = {
40
      [this.fieldName]: $target.val(),
41 42 43
    };
    return $.ajax({
      type: 'PATCH',
44
      url: $target.data('update-url') || $('form.js-issuable-update').attr('action'),
45
      data: patchData,
46
      success: this.onSuccess,
47
      error: this.onError,
48 49 50
    });
  }
}