2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2021-02-01 10:08:56 -05:00
|
|
|
import { fixTitle } from '~/tooltips';
|
2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from './flash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import { __ } from './locale';
|
2018-02-02 05:38:31 -05:00
|
|
|
|
2018-06-05 05:47:43 -04:00
|
|
|
const tooltipTitles = {
|
|
|
|
group: {
|
|
|
|
subscribed: __('Unsubscribe at group level'),
|
|
|
|
unsubscribed: __('Subscribe at group level'),
|
|
|
|
},
|
|
|
|
project: {
|
|
|
|
subscribed: __('Unsubscribe at project level'),
|
|
|
|
unsubscribed: __('Subscribe at project level'),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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 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');
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.post(url)
|
|
|
|
.then(() => {
|
|
|
|
let newStatus;
|
|
|
|
let newAction;
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
if (oldStatus === 'unsubscribed') {
|
2019-05-07 02:24:27 -04:00
|
|
|
[newStatus, newAction] = ['subscribed', __('Unsubscribe')];
|
2018-10-10 03:13:34 -04:00
|
|
|
} else {
|
2019-05-07 02:24:27 -04:00
|
|
|
[newStatus, newAction] = ['unsubscribed', __('Subscribe')];
|
2018-10-10 03:13:34 -04:00
|
|
|
}
|
2016-11-15 14:28:02 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
$btn.removeClass('disabled');
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
this.$buttons.attr('data-status', newStatus);
|
|
|
|
this.$buttons.find('> span').text(newAction);
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
this.$buttons.map((i, button) => {
|
|
|
|
const $button = $(button);
|
|
|
|
const originalTitle = $button.attr('data-original-title');
|
2016-11-15 18:00:45 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
if (originalTitle) {
|
|
|
|
ProjectLabelSubscription.setNewTitle($button, originalTitle, newStatus, newAction);
|
|
|
|
}
|
2017-02-15 15:15:44 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
return button;
|
|
|
|
});
|
|
|
|
})
|
2021-06-20 23:10:27 -04:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('There was an error subscribing to this label.'),
|
|
|
|
}),
|
|
|
|
);
|
2016-11-15 14:28:02 -05:00
|
|
|
}
|
2018-05-28 07:05:40 -04:00
|
|
|
|
2018-06-05 05:47:43 -04:00
|
|
|
static setNewTitle($button, originalTitle, newStatus) {
|
|
|
|
const type = /group/.test(originalTitle) ? 'group' : 'project';
|
|
|
|
const newTitle = tooltipTitles[type][newStatus];
|
2018-05-28 07:05:40 -04:00
|
|
|
|
2020-10-06 11:08:33 -04:00
|
|
|
$button.attr('title', newTitle);
|
|
|
|
fixTitle($button);
|
2018-05-28 07:05:40 -04:00
|
|
|
}
|
2017-11-17 06:30:09 -05:00
|
|
|
}
|