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

74 lines
2 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';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as flash } from './flash';
2021-01-03 14:25:43 +05:30
import { fixTitle } from '~/tooltips';
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
const tooltipTitles = {
group: {
subscribed: __('Unsubscribe at group level'),
unsubscribed: __('Subscribe at group level'),
},
project: {
subscribed: __('Unsubscribe at project level'),
unsubscribed: __('Subscribe at project level'),
},
};
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 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');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
axios
.post(url)
.then(() => {
let newStatus;
let newAction;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
if (oldStatus === 'unsubscribed') {
2019-09-04 21:01:54 +05:30
[newStatus, newAction] = ['subscribed', __('Unsubscribe')];
2018-12-13 13:39:08 +05:30
} else {
2019-09-04 21:01:54 +05:30
[newStatus, newAction] = ['unsubscribed', __('Subscribe')];
2018-12-13 13:39:08 +05:30
}
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$btn.removeClass('disabled');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
this.$buttons.attr('data-status', newStatus);
this.$buttons.find('> span').text(newAction);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
this.$buttons.map((i, button) => {
const $button = $(button);
const originalTitle = $button.attr('data-original-title');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
if (originalTitle) {
ProjectLabelSubscription.setNewTitle($button, originalTitle, newStatus, newAction);
}
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
return button;
});
})
.catch(() => flash(__('There was an error subscribing to this label.')));
2017-08-17 22:00:37 +05:30
}
2018-11-08 19:23:39 +05:30
static setNewTitle($button, originalTitle, newStatus) {
const type = /group/.test(originalTitle) ? 'group' : 'project';
const newTitle = tooltipTitles[type][newStatus];
2021-01-03 14:25:43 +05:30
$button.attr('title', newTitle);
fixTitle($button);
2018-11-08 19:23:39 +05:30
}
2018-03-17 18:26:18 +05:30
}