gitlab-org--gitlab-foss/app/assets/javascripts/star.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import { deprecatedCreateFlash as Flash } from './flash';
import { __, s__ } from './locale';
import { spriteIcon } from './lib/utils/common_utils';
2018-02-20 17:20:48 -05:00
import axios from './lib/utils/axios_utils';
export default class Star {
2018-12-13 07:37:54 -05:00
constructor(container = '.project-home-panel') {
$(`${container} .toggle-star`).on('click', function toggleStarClickCallback() {
2018-02-20 17:20:48 -05:00
const $this = $(this);
const $starSpan = $this.find('span');
2018-12-13 07:37:54 -05:00
const $starIcon = $this.find('svg');
const iconClasses = $starIcon.attr('class').split(' ');
axios
.post($this.data('endpoint'))
2018-02-20 17:20:48 -05:00
.then(({ data }) => {
const isStarred = $starSpan.hasClass('starred');
$this
.parent()
.find('.count')
.text(data.star_count);
2018-02-20 17:20:48 -05:00
if (isStarred) {
$starSpan.removeClass('starred').text(s__('StarProject|Star'));
2018-12-13 07:37:54 -05:00
$starIcon.remove();
$this.prepend(spriteIcon('star-o', iconClasses));
} else {
$starSpan.addClass('starred').text(__('Unstar'));
2018-12-13 07:37:54 -05:00
$starIcon.remove();
$this.prepend(spriteIcon('star', iconClasses));
}
2018-02-20 17:20:48 -05:00
})
.catch(() => Flash(__('Star toggle failed. Try again later.')));
2018-02-20 17:20:48 -05:00
});
}
}