40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
define(['jquery'], function ($) {
|
|
'use strict';
|
|
|
|
return function (config, element) {
|
|
var $root = $(element),
|
|
voteUrl = config.voteUrl || '';
|
|
|
|
$root.on('click', '.esun-review-vote', function () {
|
|
var $button = $(this),
|
|
$votes = $button.closest('.esun-review-votes'),
|
|
reviewId = parseInt($votes.data('review-id'), 10) || 0,
|
|
voteType = parseInt($button.data('vote-type'), 10) || 0;
|
|
|
|
if (!voteUrl || !reviewId || $votes.data('can-vote') !== 1) {
|
|
return;
|
|
}
|
|
|
|
$.ajax({
|
|
url: voteUrl,
|
|
type: 'POST',
|
|
dataType: 'json',
|
|
data: {
|
|
review_id: reviewId,
|
|
vote_type: voteType,
|
|
form_key: $('input[name="form_key"]').first().val() || ($.mage && $.mage.cookies ? $.mage.cookies.get('form_key') : '')
|
|
}
|
|
}).done(function (response) {
|
|
if (!response || !response.success) {
|
|
return;
|
|
}
|
|
|
|
$votes.find('[data-role="vote-up-count"]').text(response.up || 0);
|
|
$votes.find('[data-role="vote-down-count"]').text(response.down || 0);
|
|
$votes.find('.esun-review-vote').removeClass('is-active');
|
|
$votes.find('[data-vote-type="' + response.customer_vote + '"]').addClass('is-active');
|
|
});
|
|
});
|
|
};
|
|
});
|