summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notifications_dropdown.js
blob: ae992dd5dc5beb403d9dd4fdf2ae096d9019fb16 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import $ from 'jquery';
import { Rails } from '~/lib/utils/rails_ujs';
import { deprecatedCreateFlash as Flash } from './flash';
import { __ } from '~/locale';

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('spinner');
    if (form.hasClass('no-label')) {
      form.find('.js-notification-loading').toggleClass('hidden');
      form.find('.js-notifications-icon').toggleClass('hidden');
    }
    form.find('#notification_setting_level').val(notificationLevel);
    Rails.fire(form[0], 'submit');
  });

  $(document).on('ajax:success', '.notification-form', e => {
    const data = e.detail[0];

    if (data.saved) {
      $(e.currentTarget)
        .closest('.js-notification-dropdown')
        .replaceWith(data.html);
    } else {
      Flash(__('Failed to save new settings'), 'alert');
    }
  });
}