fa2af5e0f5
Reduced the technical debt around our JS flash function by making it a module that is imported rather than relying on the global function. The global function still exists mainly for technical debt with how some requests are being completed, but new JS should import the module directly. Also reduces some tech debt in the file by removing the need for jQuery. Instead Flash is now 100% vanilla JS.
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
import 'deckar01-task_list';
|
|
import Flash from './flash';
|
|
|
|
export default 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,
|
|
});
|
|
}
|
|
}
|