debian-mirror-gitlab/app/assets/javascripts/project_label_subscription.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
export default class ProjectLabelSubscription {
constructor(container) {
this.$container = $(container);
this.$buttons = this.$container.find('.js-subscribe-button');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$buttons.on('click', this.toggleSubscription.bind(this));
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
toggleSubscription(event) {
event.preventDefault();
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
const $btn = $(event.currentTarget);
const $span = $btn.find('span');
const url = $btn.attr('data-url');
const oldStatus = $btn.attr('data-status');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$btn.addClass('disabled');
$span.toggleClass('hidden');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
axios.post(url).then(() => {
let newStatus;
let newAction;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (oldStatus === 'unsubscribed') {
[newStatus, newAction] = ['subscribed', 'Unsubscribe'];
} else {
[newStatus, newAction] = ['unsubscribed', 'Subscribe'];
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$span.toggleClass('hidden');
$btn.removeClass('disabled');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$buttons.attr('data-status', newStatus);
this.$buttons.find('> span').text(newAction);
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$buttons.map((button) => {
const $button = $(button);
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if ($button.attr('data-original-title')) {
$button.tooltip('hide').attr('data-original-title', newAction).tooltip('fixTitle');
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
return button;
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
}).catch(() => flash(__('There was an error subscribing to this label.')));
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
}