2017-11-17 06:30:09 -05:00
|
|
|
export default class ProjectLabelSubscription {
|
|
|
|
constructor(container) {
|
|
|
|
this.$container = $(container);
|
|
|
|
this.$buttons = this.$container.find('.js-subscribe-button');
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
this.$buttons.on('click', this.toggleSubscription.bind(this));
|
|
|
|
}
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
toggleSubscription(event) {
|
|
|
|
event.preventDefault();
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
const $btn = $(event.currentTarget);
|
|
|
|
const $span = $btn.find('span');
|
|
|
|
const url = $btn.attr('data-url');
|
|
|
|
const oldStatus = $btn.attr('data-status');
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
$btn.addClass('disabled');
|
|
|
|
$span.toggleClass('hidden');
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url,
|
|
|
|
}).done(() => {
|
|
|
|
let newStatus;
|
|
|
|
let newAction;
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
if (oldStatus === 'unsubscribed') {
|
|
|
|
[newStatus, newAction] = ['subscribed', 'Unsubscribe'];
|
|
|
|
} else {
|
|
|
|
[newStatus, newAction] = ['unsubscribed', 'Subscribe'];
|
|
|
|
}
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
$span.toggleClass('hidden');
|
|
|
|
$btn.removeClass('disabled');
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
this.$buttons.attr('data-status', newStatus);
|
|
|
|
this.$buttons.find('> span').text(newAction);
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
this.$buttons.map((button) => {
|
|
|
|
const $button = $(button);
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
if ($button.attr('data-original-title')) {
|
|
|
|
$button.tooltip('hide').attr('data-original-title', newAction).tooltip('fixTitle');
|
|
|
|
}
|
2017-02-15 15:15:44 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
return button;
|
2016-11-15 14:28:02 -05:00
|
|
|
});
|
2017-11-17 06:30:09 -05:00
|
|
|
});
|
2016-11-15 14:28:02 -05:00
|
|
|
}
|
2017-11-17 06:30:09 -05:00
|
|
|
}
|