summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/task_list.js
blob: 419c458ff3421ff7a7942b4f11f26fb7bc6a44b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* global Flash */

import 'deckar01-task_list';

class TaskList {
  constructor(options = {}) {
    this.selector = options.selector;
    this.dataType = options.dataType;
    this.fieldName = options.fieldName;
    this.onSuccess = options.onSuccess || (() => {});
    this.onError = function showFlash(response) {
      let errorMessages = '';

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

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

    this.init();
  }

  init() {
    // Prevent duplicate event bindings
    this.disable();
    $(`${this.selector} .js-task-list-container`).taskList('enable');
    $(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
  }

  disable() {
    $(`${this.selector} .js-task-list-container`).taskList('disable');
    $(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
  }

  update(e) {
    const $target = $(e.target);
    const patchData = {};
    patchData[this.dataType] = {
      [this.fieldName]: $target.val(),
    };
    return $.ajax({
      type: 'PATCH',
      url: $target.data('update-url') || $('form.js-issuable-update').attr('action'),
      data: patchData,
      success: this.onSuccess,
      error: this.onError,
    });
  }
}

window.gl = window.gl || {};
window.gl.TaskList = TaskList;