gitlab-org--gitlab-foss/app/assets/javascripts/task_list.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import 'deckar01-task_list';
2018-02-02 06:26:08 -05:00
import axios from './lib/utils/axios_utils';
import Flash from './flash';
export default class TaskList {
constructor(options = {}) {
this.selector = options.selector;
this.dataType = options.dataType;
this.fieldName = options.fieldName;
2017-02-08 05:54:33 -05:00
this.onSuccess = options.onSuccess || (() => {});
this.onError = function showFlash(e) {
let errorMessages = '';
if (e.response.data && typeof e.response.data === 'object') {
errorMessages = e.response.data.errors.join(' ');
}
return new Flash(errorMessages || 'Update failed', 'alert');
};
this.init();
}
init() {
// Prevent duplicate event bindings
this.disable();
2017-02-08 03:10:04 -05:00
$(`${this.selector} .js-task-list-container`).taskList('enable');
$(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
}
disable() {
2017-02-08 03:10:04 -05:00
$(`${this.selector} .js-task-list-container`).taskList('disable');
2017-02-09 17:14:21 -05:00
$(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
}
update(e) {
const $target = $(e.target);
2017-02-08 03:10:04 -05:00
const patchData = {};
patchData[this.dataType] = {
[this.fieldName]: $target.val(),
};
2018-02-02 06:26:08 -05:00
2018-02-20 17:20:48 -05:00
return axios.patch($target.data('updateUrl') || $('form.js-issuable-update').attr('action'), patchData)
2018-02-02 06:26:08 -05:00
.then(({ data }) => this.onSuccess(data))
.catch(err => this.onError(err));
}
}