BigW Consortium Gitlab

group_label_subscription.js.es6 1.51 KB
Newer Older
1 2
/* eslint-disable func-names, object-shorthand, comma-dangle, wrap-iife, space-before-function-paren, no-param-reassign, padded-blocks, max-len */

3 4 5 6 7
(function(global) {
  class GroupLabelSubscription {
    constructor(container) {
      const $container = $(container);
      this.$dropdown = $container.find('.dropdown');
8 9
      this.$subscribeButtons = $container.find('.js-subscribe-button');
      this.$unsubscribeButtons = $container.find('.js-unsubscribe-button');
10

11 12
      this.$subscribeButtons.on('click', this.subscribe.bind(this));
      this.$unsubscribeButtons.on('click', this.unsubscribe.bind(this));
13 14 15 16 17
    }

    unsubscribe(event) {
      event.preventDefault();

18
      const url = this.$unsubscribeButtons.attr('data-url');
19 20 21 22 23

      $.ajax({
        type: 'POST',
        url: url
      }).done(() => {
24 25
        this.toggleSubscriptionButtons();
        this.$unsubscribeButtons.removeAttr('data-url');
26 27 28 29 30 31 32 33 34
      });
    }

    subscribe(event) {
      event.preventDefault();

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

35
      this.$unsubscribeButtons.attr('data-url', url);
36 37 38 39 40

      $.ajax({
        type: 'POST',
        url: url
      }).done(() => {
41
        this.toggleSubscriptionButtons();
42 43
      });
    }
44 45 46 47 48 49

    toggleSubscriptionButtons() {
      this.$dropdown.toggleClass('hidden');
      this.$subscribeButtons.toggleClass('hidden');
      this.$unsubscribeButtons.toggleClass('hidden');
    }
50 51 52 53 54
  }

  global.GroupLabelSubscription = GroupLabelSubscription;

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