summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/group_label_subscription.js
blob: d33e3a375809246b63a1099871a0679909d9e57f (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import $ from 'jquery';
import { __ } from '~/locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';

const tooltipTitles = {
  group: __('Unsubscribe at group level'),
  project: __('Unsubscribe at project level'),
};

export default class GroupLabelSubscription {
  constructor(container) {
    const $container = $(container);
    this.$dropdown = $container.find('.dropdown');
    this.$subscribeButtons = $container.find('.js-subscribe-button');
    this.$unsubscribeButtons = $container.find('.js-unsubscribe-button');

    this.$subscribeButtons.on('click', this.subscribe.bind(this));
    this.$unsubscribeButtons.on('click', this.unsubscribe.bind(this));
  }

  unsubscribe(event) {
    event.preventDefault();

    const url = this.$unsubscribeButtons.attr('data-url');
    axios.post(url)
      .then(() => {
        this.toggleSubscriptionButtons();
        this.$unsubscribeButtons.removeAttr('data-url');
      })
      .catch(() => flash(__('There was an error when unsubscribing from this label.')));
  }

  subscribe(event) {
    event.preventDefault();

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

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

    axios.post(url)
      .then(() => GroupLabelSubscription.setNewTooltip($btn))
      .then(() => this.toggleSubscriptionButtons())
      .catch(() => flash(__('There was an error when subscribing to this label.')));
  }

  toggleSubscriptionButtons() {
    this.$dropdown.toggleClass('hidden');
    this.$subscribeButtons.toggleClass('hidden');
    this.$unsubscribeButtons.toggleClass('hidden');
  }

  static setNewTooltip($button) {
    if (!$button.hasClass('js-subscribe-button')) return;

    const type = $button.hasClass('js-group-level') ? 'group' : 'project';
    const newTitle = tooltipTitles[type];

    $('.js-unsubscribe-button', $button.closest('.label-actions-list'))
      .tooltip('hide').attr('title', newTitle).tooltip('_fixTitle');
  }
}