2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2020-09-21 15:09:44 +00:00
|
|
|
import { Rails } from '~/lib/utils/rails_ujs';
|
2020-08-20 09:09:55 +00:00
|
|
|
import { deprecatedCreateFlash as Flash } from './flash';
|
2019-04-03 16:15:15 +00:00
|
|
|
import { __ } from '~/locale';
|
2016-12-14 05:26:26 +00:00
|
|
|
|
2017-12-15 09:31:58 +00:00
|
|
|
export default function notificationsDropdown() {
|
|
|
|
$(document).on('click', '.update-notification', function updateNotificationCallback(e) {
|
|
|
|
e.preventDefault();
|
2019-03-27 21:33:20 +00:00
|
|
|
|
2018-02-20 22:20:48 +00:00
|
|
|
if ($(this).is('.is-active') && $(this).data('notificationLevel') === 'custom') {
|
2017-12-15 09:31:58 +00:00
|
|
|
return;
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 22:20:48 +00:00
|
|
|
const notificationLevel = $(this).data('notificationLevel');
|
2020-12-23 12:10:26 +00:00
|
|
|
const form = $(this).parents('.notification-form').first();
|
2017-12-15 09:31:58 +00:00
|
|
|
|
2020-02-21 15:09:05 +00:00
|
|
|
form.find('.js-notification-loading').toggleClass('spinner');
|
2018-12-07 14:11:42 +00:00
|
|
|
if (form.hasClass('no-label')) {
|
|
|
|
form.find('.js-notification-loading').toggleClass('hidden');
|
|
|
|
form.find('.js-notifications-icon').toggleClass('hidden');
|
|
|
|
}
|
2017-12-15 09:31:58 +00:00
|
|
|
form.find('#notification_setting_level').val(notificationLevel);
|
2020-09-21 15:09:44 +00:00
|
|
|
Rails.fire(form[0], 'submit');
|
2017-12-15 09:31:58 +00:00
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
$(document).on('ajax:success', '.notification-form', (e) => {
|
2020-09-21 15:09:44 +00:00
|
|
|
const data = e.detail[0];
|
|
|
|
|
2017-12-15 09:31:58 +00:00
|
|
|
if (data.saved) {
|
2020-12-23 12:10:26 +00:00
|
|
|
$(e.currentTarget).closest('.js-notification-dropdown').replaceWith(data.html);
|
2017-12-15 09:31:58 +00:00
|
|
|
} else {
|
2019-04-03 16:15:15 +00:00
|
|
|
Flash(__('Failed to save new settings'), 'alert');
|
2017-12-15 09:31:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|