Skip to content

Node.js based forum software built for the modern web

feat(topic): instant vote UI with rollback for topic and world feed

Julian Lam9f09ee9027d4
diff --git a/public/src/client/topic/votes.js b/public/src/client/topic/votes.js
index a17a152294..a32ef45751 100644
--- a/public/src/client/topic/votes.js
+++ b/public/src/client/topic/votes.js
@@ -90,17 +90,21 @@ define('forum/topic/votes', [
}
- Votes.toggleVote = function (button, className, delta) {
+ Votes.toggleVote = async function (button, className, delta, iconSelector) {
const post = button.closest('[data-pid]');
- const currentState = post.find(className).length;
+ // Determine current state from the button element itself (works for both
+ // topic page with component="post/upvote" and world feed with data-action="upvote")
+ const classToCheck = className.replace(/^\./, '');
+ const currentState = button.hasClass(classToCheck);
const method = currentState ? 'del' : 'put';
+ const voteDelta = currentState ? -delta : delta;
const pid = post.attr('data-pid');
if (!app.user.uid) {
if (!config.activitypub.enabled) {
ajaxify.go('login');
- return;
+ return false;
}
let objectId;
@@ -115,17 +119,51 @@ define('forum/topic/votes', [
return false;
}
- api[method](`/posts/${encodeURIComponent(pid)}/vote`, {
- delta: delta,
- }, function (err) {
- if (err) {
- return alerts.error(err);
- }
- hooks.fire('action:post.toggleVote', {
- pid: pid,
- delta: delta,
- unvote: method === 'del',
+ // Optimistic UI update — instant feedback
+ button.toggleClass(classToCheck);
+
+ // Toggle icon state (optional — callers pass a selector for the icon element)
+ if (iconSelector) {
+ const icon = button.find(iconSelector);
+ icon.toggleClass('fa text-danger', !currentState)
+ .toggleClass('fa-regular text-muted', currentState);
+ }
+
+ // Topic page uses [component="post/vote-count"], world feed uses [component="upvote-count"]
+ const voteCountEl = post.find('[component="post/vote-count"], [component="upvote-count"]');
+ if (voteCountEl.length) {
+ const currentVotes = parseInt(voteCountEl.attr('data-votes') || voteCountEl.text(), 10) || 0;
+ voteCountEl.text(currentVotes + voteDelta).attr('data-votes', currentVotes + voteDelta);
+ }
+
+ // API call in background
+ try {
+ await api[method](`/posts/${encodeURIComponent(pid)}/vote`, {
+ delta: voteDelta,
});
+ } catch (e) {
+ // Rollback on failure — revert button + counter + icon
+ button.toggleClass(classToCheck);
+
+ if (iconSelector) {
+ const icon = button.find(iconSelector);
+ icon.toggleClass('fa text-danger', !currentState)
+ .toggleClass('fa-regular text-muted', currentState);
+ }
+
+ if (voteCountEl.length) {
+ const rollbackVotes = parseInt(voteCountEl.attr('data-votes') || voteCountEl.text(), 10) || 0;
+ voteCountEl.text(rollbackVotes - voteDelta).attr('data-votes', rollbackVotes - voteDelta);
+ }
+
+ alerts.error(e.message);
+ return false;
+ }
+
+ hooks.fire('action:post.toggleVote', {
+ pid: pid,
+ delta: voteDelta,
+ unvote: method === 'del',
});
return false;
diff --git a/public/src/client/world.js b/public/src/client/world.js
index 92c451803d..3aff63c08e 100644
--- a/public/src/client/world.js
+++ b/public/src/client/world.js
@@ -170,19 +170,10 @@ define('forum/world', [
feedEl.on('click', '[data-action="upvote"]', function (e) {
e.preventDefault();
- votes.toggleVote($(this), '.upvoted', 1);
+ votes.toggleVote($(this), 'upvoted', 1, 'i');
return false;
});
- // Handle UI updates for upvotes in the world feed
- hooks.onPage('action:post.toggleVote', function (data) {
- if (data.unvote) {
- $(`[data-pid="${data.pid}"][data-action="upvote"]`).removeClass('upvoted');
- } else {
- $(`[data-pid="${data.pid}"][data-action="upvote"]`).addClass('upvoted');
- }
- });
-
feedEl.on('click', '[data-action="reply"]', function () {
const $this = $(this);
const isMain = $this.attr('data-is-main') === 'true';