2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-11-08 19:23:39 +05:30
|
|
|
import { __ } from '~/locale';
|
2021-01-03 14:25:43 +05:30
|
|
|
import { fixTitle, hide } from '~/tooltips';
|
2021-09-30 23:02:18 +05:30
|
|
|
import createFlash from './flash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from './lib/utils/axios_utils';
|
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');
|
|
|
|
})
|
2021-09-30 23:02:18 +05:30
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('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())
|
2021-09-30 23:02:18 +05:30
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('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];
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
const $el = $('.js-unsubscribe-button', $button.closest('.label-actions-list'));
|
|
|
|
hide($el);
|
|
|
|
$el.attr('title', `${newTitle}`);
|
|
|
|
fixTitle($el);
|
2018-11-08 19:23:39 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|