2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-06-05 05:47:43 -04:00
|
|
|
import { __ } from '~/locale';
|
2018-01-29 07:15:13 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import flash from './flash';
|
2018-06-05 05:47:43 -04:00
|
|
|
|
|
|
|
const tooltipTitles = {
|
|
|
|
group: __('Unsubscribe at group level'),
|
|
|
|
project: __('Unsubscribe at project level'),
|
|
|
|
};
|
2018-01-29 07:15:13 -05:00
|
|
|
|
2017-10-23 09:05:53 -04:00
|
|
|
export default class GroupLabelSubscription {
|
2017-03-11 02:30:44 -05:00
|
|
|
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));
|
2016-11-15 16:59:12 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
unsubscribe(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const url = this.$unsubscribeButtons.attr('data-url');
|
2018-10-24 15:17:03 -04:00
|
|
|
axios
|
|
|
|
.post(url)
|
2018-01-29 07:15:13 -05:00
|
|
|
.then(() => {
|
|
|
|
this.toggleSubscriptionButtons();
|
|
|
|
this.$unsubscribeButtons.removeAttr('data-url');
|
|
|
|
})
|
|
|
|
.catch(() => flash(__('There was an error when unsubscribing from this label.')));
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const $btn = $(event.currentTarget);
|
|
|
|
const url = $btn.attr('data-url');
|
|
|
|
|
|
|
|
this.$unsubscribeButtons.attr('data-url', url);
|
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
axios
|
|
|
|
.post(url)
|
2018-05-28 07:05:40 -04:00
|
|
|
.then(() => GroupLabelSubscription.setNewTooltip($btn))
|
2018-01-29 07:15:13 -05:00
|
|
|
.then(() => this.toggleSubscriptionButtons())
|
|
|
|
.catch(() => flash(__('There was an error when subscribing to this label.')));
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleSubscriptionButtons() {
|
|
|
|
this.$dropdown.toggleClass('hidden');
|
|
|
|
this.$subscribeButtons.toggleClass('hidden');
|
|
|
|
this.$unsubscribeButtons.toggleClass('hidden');
|
|
|
|
}
|
2018-05-28 07:05:40 -04:00
|
|
|
|
|
|
|
static setNewTooltip($button) {
|
|
|
|
if (!$button.hasClass('js-subscribe-button')) return;
|
2018-06-05 08:11:48 -04:00
|
|
|
|
2018-05-28 07:05:40 -04:00
|
|
|
const type = $button.hasClass('js-group-level') ? 'group' : 'project';
|
2018-06-05 05:47:43 -04:00
|
|
|
const newTitle = tooltipTitles[type];
|
2018-05-28 07:05:40 -04:00
|
|
|
|
2018-06-05 05:47:43 -04:00
|
|
|
$('.js-unsubscribe-button', $button.closest('.label-actions-list'))
|
2018-10-24 15:17:03 -04:00
|
|
|
.tooltip('hide')
|
|
|
|
.attr('title', newTitle)
|
|
|
|
.tooltip('_fixTitle');
|
2018-05-28 07:05:40 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|