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

33 lines
1.1 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import 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 {
constructor() {
2018-02-20 17:20:48 -05:00
$('.project-home-panel .toggle-star').on('click', function toggleStarClickCallback() {
const $this = $(this);
const $starSpan = $this.find('span');
const $startIcon = $this.find('svg');
2018-02-20 17:20:48 -05:00
axios.post($this.data('endpoint'))
.then(({ data }) => {
const isStarred = $starSpan.hasClass('starred');
$this.parent().find('.star-count').text(data.star_count);
2018-02-20 17:20:48 -05:00
if (isStarred) {
$starSpan.removeClass('starred').text(s__('StarProject|Star'));
$startIcon.remove();
$this.prepend(spriteIcon('star-o'));
} else {
$starSpan.addClass('starred').text(__('Unstar'));
$startIcon.remove();
$this.prepend(spriteIcon('star'));
}
2018-02-20 17:20:48 -05:00
})
.catch(() => Flash('Star toggle failed. Try again later.'));
});
}
}