summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notifications_dropdown.js
blob: 479a512ed6515a0b19e50d732edca8cb3e9b4ea6 (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
import Flash from './flash';

export default function notificationsDropdown() {
  $(document).on('click', '.update-notification', function updateNotificationCallback(e) {
    e.preventDefault();
    if ($(this).is('.is-active') && $(this).data('notificationLevel') === 'custom') {
      return;
    }

    const notificationLevel = $(this).data('notificationLevel');
    const form = $(this).parents('.notification-form:first');

    form.find('.js-notification-loading').toggleClass('fa-bell fa-spin fa-spinner');
    form.find('#notification_setting_level').val(notificationLevel);
    form.submit();
  });

  $(document).on('ajax:success', '.notification-form', (e, data) => {
    if (data.saved) {
      $(e.currentTarget).closest('.js-notification-dropdown').replaceWith(data.html);
    } else {
      Flash('Failed to save new settings', 'alert');
    }
  });
}