debian-mirror-gitlab/app/assets/javascripts/notifications_form.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as flash } from './flash';
2018-03-17 18:26:18 +05:30
export default class NotificationsForm {
constructor() {
this.toggleCheckbox = this.toggleCheckbox.bind(this);
this.initEventListeners();
}
initEventListeners() {
$(document).on('change', '.js-custom-notification-event', this.toggleCheckbox);
}
toggleCheckbox(e) {
const $checkbox = $(e.currentTarget);
2018-11-08 19:23:39 +05:30
const $parent = $checkbox.closest('.form-check');
2018-03-17 18:26:18 +05:30
this.saveEvent($checkbox, $parent);
}
// eslint-disable-next-line class-methods-use-this
showCheckboxLoadingSpinner($parent) {
2018-12-13 13:39:08 +05:30
$parent
.addClass('is-loading')
2018-03-17 18:26:18 +05:30
.find('.custom-notification-event-loading')
.removeClass('fa-check')
2020-03-13 15:44:24 +05:30
.addClass('spinner align-middle')
2018-03-17 18:26:18 +05:30
.removeClass('is-done');
}
saveEvent($checkbox, $parent) {
2020-03-13 15:44:24 +05:30
const form = $parent.parents('form').first();
2018-03-17 18:26:18 +05:30
this.showCheckboxLoadingSpinner($parent);
axios[form.attr('method')](form.attr('action'), form.serialize())
.then(({ data }) => {
2016-09-13 17:45:13 +05:30
$checkbox.enable();
if (data.saved) {
2018-12-13 13:39:08 +05:30
$parent
.find('.custom-notification-event-loading')
2020-03-13 15:44:24 +05:30
.toggleClass('spinner fa-check is-done align-middle');
2018-03-17 18:26:18 +05:30
setTimeout(() => {
2018-12-13 13:39:08 +05:30
$parent
.removeClass('is-loading')
2018-03-17 18:26:18 +05:30
.find('.custom-notification-event-loading')
2020-03-13 15:44:24 +05:30
.toggleClass('spinner fa-check is-done align-middle');
2016-09-13 17:45:13 +05:30
}, 2000);
}
2018-03-17 18:26:18 +05:30
})
.catch(() => flash(__('There was an error saving your notification settings.')));
}
}