2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-11-08 19:23:39 +05:30
|
|
|
import { __ } from '~/locale';
|
2018-03-17 18:26:18 +05:30
|
|
|
import axios from './lib/utils/axios_utils';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { deprecatedCreateFlash as flash } from './flash';
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
const tooltipTitles = {
|
|
|
|
group: __('Unsubscribe at group level'),
|
|
|
|
project: __('Unsubscribe at project level'),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default class GroupLabelSubscription {
|
2017-08-17 22:00:37 +05:30
|
|
|
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');
|
2018-12-13 13:39:08 +05:30
|
|
|
axios
|
|
|
|
.post(url)
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(() => {
|
|
|
|
this.toggleSubscriptionButtons();
|
|
|
|
this.$unsubscribeButtons.removeAttr('data-url');
|
|
|
|
})
|
|
|
|
.catch(() => flash(__('There was an error when unsubscribing from this label.')));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const $btn = $(event.currentTarget);
|
|
|
|
const url = $btn.attr('data-url');
|
|
|
|
|
|
|
|
this.$unsubscribeButtons.attr('data-url', url);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
axios
|
|
|
|
.post(url)
|
2018-11-08 19:23:39 +05:30
|
|
|
.then(() => GroupLabelSubscription.setNewTooltip($btn))
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(() => this.toggleSubscriptionButtons())
|
|
|
|
.catch(() => flash(__('There was an error when subscribing to this label.')));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
toggleSubscriptionButtons() {
|
|
|
|
this.$dropdown.toggleClass('hidden');
|
|
|
|
this.$subscribeButtons.toggleClass('hidden');
|
|
|
|
this.$unsubscribeButtons.toggleClass('hidden');
|
|
|
|
}
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
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'))
|
2018-12-13 13:39:08 +05:30
|
|
|
.tooltip('hide')
|
|
|
|
.attr('title', newTitle)
|
|
|
|
.tooltip('_fixTitle');
|
2018-11-08 19:23:39 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|