Skip to content

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

FIX: Ensure poll votes are tracked and synchronized properly (#37368)

Sérgio Saquetim1292ad9234d9
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll.gjs b/plugins/poll/assets/javascripts/discourse/components/poll.gjs
index 86030fa2b64..d32d23e46f6 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll.gjs
@@ -5,6 +5,7 @@ import { action } from "@ember/object";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
+import { TrackedObject } from "@ember-compat/tracked-built-ins";
import icon from "discourse/helpers/d-icon";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
@@ -41,9 +42,10 @@ export default class PollComponent extends Component {
@service dialog;
@service modal;
- @tracked vote = this.args.post.polls_votes?.[this.args.poll.name] || [];
@tracked preloadedVoters = this.defaultPreloadedVoters();
@tracked voterListExpanded = false;
+
+ @tracked vote = this.args.post.polls_votes?.[this.args.poll.name] || [];
@tracked hasSavedVote = this.vote.length > 0;
@tracked
@@ -222,6 +224,10 @@ export default class PollComponent extends Component {
});
this.hasSavedVote = true;
+ if (!this.args.post.polls_votes) {
+ this.args.post.polls_votes = new TrackedObject();
+ }
+ this.args.post.polls_votes[this.poll.name] = this.vote;
Object.assign(this.poll, poll);
this.appEvents.trigger("poll:voted", poll, this.post, this.vote);
@@ -561,6 +567,9 @@ export default class PollComponent extends Component {
}
this.vote = Object.assign([]);
this.hasSavedVote = false;
+ if (this.args.post.polls_votes) {
+ delete this.args.post.polls_votes[this.poll.name];
+ }
this.appEvents.trigger("poll:voted", poll, this.post, this.vote);
this.showResults = false;
})
diff --git a/plugins/poll/assets/javascripts/discourse/initializers/extend-for-poll.gjs b/plugins/poll/assets/javascripts/discourse/initializers/extend-for-poll.gjs
index 40388c1666d..6a39e79a42a 100644
--- a/plugins/poll/assets/javascripts/discourse/initializers/extend-for-poll.gjs
+++ b/plugins/poll/assets/javascripts/discourse/initializers/extend-for-poll.gjs
@@ -92,6 +92,7 @@ function initializePolls(api) {
"model:post",
(Superclass) =>
class extends Superclass {
+ @tracked polls_votes = new TrackedObject();
@tracked pollsObject = new TrackedObject();
@tracked _polls;