Skip to content

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

FIX: poll when config is on_close only show results when poll is closed (#28299)

Robert47f749744f7f
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
index 4423418747f..42043a39790 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
@@ -26,7 +26,12 @@ export default class PollOptionsComponent extends Component {
this.args.sendOptionSelect(option, rank);
}
<template>
- <ul class={{concatClass (if @isRankedChoice "ranked-choice-poll-options")}}>
+ <ul
+ class={{concatClass
+ (if @isRankedChoice "ranked-choice-poll-options")
+ "options"
+ }}
+ >
{{#each @options key="rank" as |option|}}
{{#if @isRankedChoice}}
<PollOptionRankedChoice
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
index 1b0eec3b631..d24d2878522 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
@@ -7,6 +7,9 @@ import PollVoters from "./poll-voters";
export default class PollResultsStandardComponent extends Component {
orderOptions = (options) => {
+ options.forEach((option) => {
+ option.votes = option.votes ?? 0;
+ });
return options.sort((a, b) => {
if (a.votes < b.votes) {
return 1;
@@ -41,11 +44,11 @@ export default class PollResultsStandardComponent extends Component {
option.percentage = per;
option.chosen = chosen;
let voters = this.args.isPublic
- ? this.args.voters[option.id]?.voters || []
+ ? this.args.voters?.[option.id]?.voters ?? []
: [];
option.voters = [...voters];
option.loading = this.args.isPublic
- ? this.args.voters[option.id]?.loading || false
+ ? this.args.voters?.[option.id]?.loading ?? false
: false;
});
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll.gjs b/plugins/poll/assets/javascripts/discourse/components/poll.gjs
index 6a953165805..76babde0ebd 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll.gjs
@@ -37,6 +37,7 @@ const OPEN_STATUS = "open";
export default class PollComponent extends Component {
@service currentUser;
@service siteSettings;
+ @service router;
@service appEvents;
@service dialog;
@service modal;
@@ -48,9 +49,10 @@ export default class PollComponent extends Component {
@tracked
showResults =
- this.hasSavedVote ||
- (this.topicArchived && !this.staffOnly) ||
- (this.closed && !this.staffOnly);
+ !(this.poll.results === ON_CLOSE && !this.closed) &&
+ (this.hasSavedVote ||
+ (this.topicArchived && !this.staffOnly) ||
+ (this.closed && !this.staffOnly));
@tracked showTally = false;
@@ -595,6 +597,15 @@ export default class PollComponent extends Component {
) {
this.showResults = this.status === CLOSED_STATUS;
}
+
+ // Votes are only included in serialized results for results=ON_CLOSE when
+ // the poll is closed, so we must refresh the page to pick these up.
+ if (
+ this.poll.results === ON_CLOSE &&
+ this.status === CLOSED_STATUS
+ ) {
+ this.router.refresh();
+ }
})
.catch((error) => {
if (error) {
diff --git a/plugins/poll/test/javascripts/component/poll-test.js b/plugins/poll/test/javascripts/component/poll-test.js
index b23875f5850..7ff004b0e13 100644
--- a/plugins/poll/test/javascripts/component/poll-test.js
+++ b/plugins/poll/test/javascripts/component/poll-test.js
@@ -86,6 +86,43 @@ module("Poll | Component | poll", function (hooks) {
);
});
+ test("does not show results after voting when results are to be shown only on closed", async function (assert) {
+ this.setProperties({
+ attributes: EmberObject.create({
+ post: EmberObject.create({
+ id: 42,
+ topic: {
+ archived: false,
+ },
+ user_id: 29,
+ }),
+ hasSavedVote: true,
+ poll: EmberObject.create({
+ name: "poll",
+ type: "regular",
+ status: "open",
+ results: "on_close",
+ options: [
+ { id: "1f972d1df351de3ce35a787c89faad29", html: "yes" },
+ { id: "d7ebc3a9beea2e680815a1e4f57d6db6", html: "no" },
+ ],
+ voters: 1,
+ chart_type: "bar",
+ }),
+ vote: ["1f972d1df351de3ce35a787c89faad29"],
+ groupableUserFields: [],
+ }),
+ preloadedVoters: [],
+ });
+
+ await render(
+ hbs`<Poll @attrs={{this.attributes}} @preloadedVoters={{this.preloadedVoters}} />`
+ );
+
+ assert.ok(exists("ul.options"), "options are shown");
+ assert.ok(!exists("ul.results"), "results are not shown");
+ });
+
test("can vote", async function (assert) {
this.setProperties({
attributes: EmberObject.create({