summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/group_label_subscription.js.es6
blob: 3bc1d7127d18bfca9448f66798af539fc971950f (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
40
41
42
43
44
45
46
47
48
/* eslint-disable */
(function(global) {
  class GroupLabelSubscription {
    constructor(container) {
      const $container = $(container);
      this.$dropdown = $container.find('.dropdown');
      this.$unsubscribeBtn = $container.find('.js-unsubscribe-button');

      $container.on('click', '.js-subscribe-button', this.subscribe.bind(this));
      $container.on('click', '.js-unsubscribe-button', this.unsubscribe.bind(this));
    }

    unsubscribe(event) {
      event.preventDefault();

      const url = this.$unsubscribeBtn.attr('data-url');

      $.ajax({
        type: 'POST',
        url: url
      }).done(() => {
        this.$dropdown.toggleClass('hidden');
        this.$unsubscribeBtn.toggleClass('hidden');
        this.$unsubscribeBtn.removeAttr('data-url');
      });
    }

    subscribe(event) {
      event.preventDefault();

      const $btn = $(event.currentTarget);
      const url = $btn.attr('data-url');

      this.$unsubscribeBtn.attr('data-url', url);

      $.ajax({
        type: 'POST',
        url: url
      }).done(() => {
        this.$dropdown.toggleClass('hidden');
        this.$unsubscribeBtn.toggleClass('hidden');
      });
    }
  }

  global.GroupLabelSubscription = GroupLabelSubscription;

})(window.gl || (window.gl = {}));