debian-mirror-gitlab/app/assets/javascripts/projects/star.js

44 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2022-03-02 08:16:31 +05:30
import axios from '~/lib/utils/axios_utils';
import { spriteIcon } from '~/lib/utils/common_utils';
import { __, s__ } from '~/locale';
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
export default class Star {
2022-08-13 15:12:31 +05:30
constructor(containerSelector = '.project-home-panel') {
const container = document.querySelector(containerSelector);
const starToggle = container.querySelector('.toggle-star');
2022-11-25 23:54:43 +05:30
starToggle?.addEventListener('click', function toggleStarClickCallback() {
2022-08-13 15:12:31 +05:30
const starSpan = starToggle.querySelector('span');
const starIcon = starToggle.querySelector('svg');
const iconClasses = Array.from(starIcon.classList.values());
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
axios
2022-08-13 15:12:31 +05:30
.post(starToggle.dataset.endpoint)
2018-03-27 19:54:05 +05:30
.then(({ data }) => {
2022-08-13 15:12:31 +05:30
const isStarred = starSpan.classList.contains('starred');
starToggle.parentNode.querySelector('.count').textContent = data.star_count;
2018-03-27 19:54:05 +05:30
2018-03-17 18:26:18 +05:30
if (isStarred) {
2022-08-13 15:12:31 +05:30
starSpan.classList.remove('starred');
starSpan.textContent = s__('StarProject|Star');
starIcon.remove();
2022-10-11 01:57:18 +05:30
// eslint-disable-next-line no-unsanitized/method
2022-08-13 15:12:31 +05:30
starSpan.insertAdjacentHTML('beforebegin', spriteIcon('star-o', iconClasses));
2018-03-17 18:26:18 +05:30
} else {
2022-08-13 15:12:31 +05:30
starSpan.classList.add('starred');
starSpan.textContent = __('Unstar');
starIcon.remove();
2022-10-11 01:57:18 +05:30
// eslint-disable-next-line no-unsanitized/method
2022-08-13 15:12:31 +05:30
starSpan.insertAdjacentHTML('beforebegin', spriteIcon('star', iconClasses));
2018-03-17 18:26:18 +05:30
}
2018-03-27 19:54:05 +05:30
})
2021-09-30 23:02:18 +05:30
.catch(() =>
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-30 23:02:18 +05:30
message: __('Star toggle failed. Try again later.'),
}),
);
2018-03-27 19:54:05 +05:30
});
2017-09-10 17:25:29 +05:30
}
}