BigW Consortium Gitlab

subscription.js 1.52 KB
Newer Older
1
(() => {
2
  class Subscription {
3 4
    constructor(containerElm) {
      this.containerElm = containerElm;
5

6
      const subscribeButton = containerElm.querySelector('.js-subscribe-button');
7 8 9 10 11
      if (subscribeButton) {
        // remove class so we don't bind twice
        subscribeButton.classList.remove('js-subscribe-button');
        subscribeButton.addEventListener('click', this.toggleSubscription.bind(this));
      }
Fatih Acet committed
12 13
    }

14 15 16 17 18
    toggleSubscription(event) {
      const button = event.currentTarget;
      const buttonSpan = button.querySelector('span');
      if (!buttonSpan || button.classList.contains('disabled')) {
        return;
19
      }
20 21
      button.classList.add('disabled');

22
      const isSubscribed = buttonSpan.innerHTML.trim().toLowerCase() !== 'subscribe';
23
      const toggleActionUrl = this.containerElm.dataset.url;
24 25 26 27 28 29

      $.post(toggleActionUrl, () => {
        button.classList.remove('disabled');

        // hack to allow this to work with the issue boards Vue object
        if (document.querySelector('html').classList.contains('issue-boards-page')) {
30
          gl.issueBoards.boardStoreIssueSet(
31
            'subscribed',
32
            !gl.issueBoards.BoardsStore.detail.issue.subscribed,
33 34
          );
        } else {
Mike Greiling committed
35
          buttonSpan.innerHTML = isSubscribed ? 'Subscribe' : 'Unsubscribe';
36 37 38
        }
      });
    }
Fatih Acet committed
39

40 41 42 43
    static bindAll(selector) {
      [].forEach.call(document.querySelectorAll(selector), elm => new Subscription(elm));
    }
  }
Fatih Acet committed
44

45 46 47
  window.gl = window.gl || {};
  window.gl.Subscription = Subscription;
})();