-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathys-upvote.js
48 lines (43 loc) · 1.46 KB
/
ys-upvote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
jQuery(document).ready(function ($) {
const upvoteButton = $('.ys-upvote-button');
const downvoteButton = $('.ys-downvote-button');
// Update button UI based on voting status from PHP
if (ys_vote_ajax.already_upvoted) {
upvoteButton.addClass('already-upvoted').prop('disabled', true);
}
if (ys_vote_ajax.already_downvoted) {
downvoteButton.addClass('already-downvoted').prop('disabled', true);
}
// Handle vote clicks
function handleVote(button, voteType) {
$.ajax({
type: 'POST',
url: ys_vote_ajax.ajax_url,
data: {
action: 'ys_vote',
post_id: ys_vote_ajax.post_id,
vote_type: voteType,
nonce: ys_vote_ajax.nonce
},
success: function (response) {
if (response.success) {
alert(`Thank you for your ${voteType}!`);
button.addClass(`already-${voteType}d`).prop('disabled', true);
} else {
alert(response.data.message);
}
},
error: function () {
alert('Error processing the vote.');
}
});
}
upvoteButton.on('click', function (e) {
e.preventDefault();
handleVote($(this), 'upvote');
});
downvoteButton.on('click', function (e) {
e.preventDefault();
handleVote($(this), 'downvote');
});
});