Currently showing 0 discussions in this category
No Threads Yet
There are no discussions in this category. Be the first to post!
async function voteThread(threadId, voteType, btn) {
window.location.href = '/login?error=Please log in to vote on discussions.';
return;
try {
const scoreSpan = btn.parentElement.querySelector('.vote-score');
const upBtn = btn.parentElement.querySelector('.upvote-btn');
const downBtn = btn.parentElement.querySelector('.downvote-btn');
let currentVote = 0;
if (upBtn.style.color.includes('teal')) currentVote = 1;
if (downBtn.style.color.includes('rose') || downBtn.style.color.includes('rgb(244, 63, 94)')) currentVote = -1;
let targetVote = voteType;
if (currentVote === voteType) {
// Cancel vote
targetVote = 0;
}
const res = await fetch(`/forum/thread/${threadId}/vote`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ voteType: targetVote })
});
const data = await res.json();
if (data.success) {
// Update UI
scoreSpan.innerText = data.newScore;
// Clear current states
upBtn.style.color = 'var(--text-muted)';
downBtn.style.color = 'var(--text-muted)';
scoreSpan.style.color = 'var(--text-primary)';
if (targetVote === 1) {
upBtn.style.color = 'var(--accent-teal-hover)';
scoreSpan.style.color = 'var(--accent-teal-hover)';
} else if (targetVote === -1) {
downBtn.style.color = '#f43f5e';
scoreSpan.style.color = '#f43f5e';
}
} else {
alert('Voting error: ' + data.error);
}
} catch (e) {
console.error('Voting request failed:', e);
}
}