Skip to content

A platform for community discussion. Free, open, simple.

FIX: Post voting count not updating reactively (#39416)

Régis Hanolc5a6754e29fb
diff --git a/plugins/discourse-post-voting/assets/javascripts/discourse/components/post-voting-vote-controls.gjs b/plugins/discourse-post-voting/assets/javascripts/discourse/components/post-voting-vote-controls.gjs
index e4e0be20167..b79ff709143 100644
--- a/plugins/discourse-post-voting/assets/javascripts/discourse/components/post-voting-vote-controls.gjs
+++ b/plugins/discourse-post-voting/assets/javascripts/discourse/components/post-voting-vote-controls.gjs
@@ -15,7 +15,7 @@ export default class PostVotingVoteControls extends Component {
@tracked loading;
get count() {
- return this.args.post_voting_vote_count || 0;
+ return this.args.post.post_voting_vote_count || 0;
}
get disabled() {
@@ -36,26 +36,10 @@ export default class PostVotingVoteControls extends Component {
@action
async removeVote(direction) {
- const post = this.args.post;
const countChange = direction === "up" ? -1 : 1;
-
- post.post_voting_user_voted_direction = null;
- post.post_voting_vote_count = post.post_voting_vote_count + countChange;
-
- const voteCount = post.post_voting_vote_count;
-
- this.loading = true;
-
- try {
- return await removeVote({ post_id: post.id });
- } catch (error) {
- post.post_voting_user_voted_direction = direction;
- post.post_voting_vote_count = voteCount - countChange;
-
- popupAjaxError(error);
- } finally {
- this.loading = false;
- }
+ return this.#submitVote(null, countChange, () =>
+ removeVote({ post_id: this.args.post.id })
+ );
}
@action
@@ -65,35 +49,31 @@ export default class PostVotingVoteControls extends Component {
}
const post = this.args.post;
-
- let vote = {
- post_id: post.id,
- direction,
- };
-
- const isUpVote = direction === "up";
- let countChange;
-
- if (post.post_voting_user_voted_direction) {
- countChange = isUpVote ? 2 : -2;
- } else {
- countChange = isUpVote ? 1 : -1;
+ let countChange = post.post_voting_user_voted_direction ? 2 : 1;
+ if (direction === "down") {
+ countChange *= -1;
}
- post.post_voting_user_voted_direction = direction;
- post.post_voting_vote_count = post.post_voting_vote_count + countChange;
+ return this.#submitVote(direction, countChange, () =>
+ castVote({ post_id: post.id, direction })
+ );
+ }
+
+ async #submitVote(newDirection, countChange, apiCall) {
+ const post = this.args.post;
+ const originalDirection = post.post_voting_user_voted_direction;
+ const originalCount = post.post_voting_vote_count;
- const voteCount = post.post_voting_vote_count;
+ post.post_voting_user_voted_direction = newDirection;
+ post.post_voting_vote_count = originalCount + countChange;
this.loading = true;
try {
- return await castVote(vote);
+ return await apiCall();
} catch (error) {
- post.setProperties({
- post_voting_user_voted_direction: null,
- post_voting_vote_count: voteCount - countChange,
- });
+ post.post_voting_user_voted_direction = originalDirection;
+ post.post_voting_vote_count = originalCount;
popupAjaxError(error);
} finally {
this.loading = false;
diff --git a/plugins/discourse-post-voting/assets/javascripts/discourse/initializers/post-voting-edits.gjs b/plugins/discourse-post-voting/assets/javascripts/discourse/initializers/post-voting-edits.gjs
index a9caa85125f..28403ff0489 100644
--- a/plugins/discourse-post-voting/assets/javascripts/discourse/initializers/post-voting-edits.gjs
+++ b/plugins/discourse-post-voting/assets/javascripts/discourse/initializers/post-voting-edits.gjs
@@ -47,7 +47,8 @@ function customizePost(api) {
api.addTrackedPostProperties(
"comments_count",
"post_voting_user_voted_direction",
- "post_voting_has_votes"
+ "post_voting_has_votes",
+ "post_voting_vote_count"
);
api.modifyClass(
diff --git a/plugins/discourse-post-voting/test/javascripts/acceptance/post-voting-test.js b/plugins/discourse-post-voting/test/javascripts/acceptance/post-voting-test.js
index d36adbe74d7..03232c6ecd5 100644
--- a/plugins/discourse-post-voting/test/javascripts/acceptance/post-voting-test.js
+++ b/plugins/discourse-post-voting/test/javascripts/acceptance/post-voting-test.js
@@ -201,6 +201,14 @@ function setupPostVoting(needs) {
return helper.response({});
});
+ server.post("/post_voting/vote", () => {
+ return helper.response({});
+ });
+
+ server.delete("/post_voting/vote", () => {
+ return helper.response({});
+ });
+
server.get("/latest.json", () => {
return helper.response(postVotingTopicListResponse());
});
@@ -663,6 +671,37 @@ acceptance("logged in user", function (needs) {
.hasText("0", "displays 0 after vote is removed");
});
+ test("voting on a post and removing vote updates the count reactively", async function (assert) {
+ await visit("/t/280");
+
+ assert
+ .dom("#post_2 .post-voting-post-toggle-voters")
+ .hasText("2", "displays the initial post vote count");
+
+ await click("#post_2 .post-voting-button-upvote");
+
+ assert
+ .dom("#post_2 .post-voting-post-toggle-voters")
+ .hasText("1", "decrements the post vote count after removing upvote");
+
+ assert
+ .dom("#post_2 .post-voting-button-upvote")
+ .doesNotHaveClass(
+ "post-voting-button-voted",
+ "unhighlights the upvote button"
+ );
+
+ await click("#post_2 .post-voting-button-downvote");
+
+ assert
+ .dom("#post_2 .post-voting-post-toggle-voters")
+ .hasText("0", "decrements the post vote count after downvoting");
+
+ assert
+ .dom("#post_2 .post-voting-button-downvote")
+ .hasClass("post-voting-button-voted", "highlights the downvote button");
+ });
+
test("topic list link overrides work", async function (assert) {
await visit("/");