Fix javascript error when viewing polls as a guest
authorMarcel Werk <burntime@woltlab.com>
Mon, 18 Jul 2022 15:01:05 +0000 (17:01 +0200)
committerMarcel Werk <burntime@woltlab.com>
Mon, 18 Jul 2022 15:01:05 +0000 (17:01 +0200)
The view mistakenly assumed that the button for showing the results was always present. This caused a javascript error for guests or for users who could only see the result.

ts/WoltLabSuite/Core/Ui/Poll/Poll.ts
ts/WoltLabSuite/Core/Ui/Poll/View/Results.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Poll.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/View/Results.js

index 83e4bfd121b76bdf1661404bd90c563c91f0fd3c..ea13c96ae37c4e3db6a4a48553412d2f04d193a3 100644 (file)
@@ -51,8 +51,9 @@ export class Poll {
         }
       });
 
-    if (this.canViewResults) {
-      this.resultsView = new Results(this);
+    const button = this.element.querySelector<HTMLButtonElement>(".showResultsButton");
+    if (this.canViewResults && button !== null) {
+      this.resultsView = new Results(this, button);
     }
 
     if (this.canVote) {
index 1ee868f76bba1925262502afd1a9b597f1e882bc..0c7208503568d5e98d8e9b0a26daaee05ded891c 100644 (file)
@@ -19,15 +19,8 @@ export class Results {
   private readonly pollManager: Poll;
   private readonly button: HTMLButtonElement;
 
-  public constructor(manager: Poll) {
+  public constructor(manager: Poll, button: HTMLButtonElement) {
     this.pollManager = manager;
-
-    const button = this.pollManager.getElement().querySelector<HTMLButtonElement>(".showResultsButton");
-
-    if (!button) {
-      throw new Error(`Could not find button with selector ".showResultsButton" for poll "${this.pollManager.pollId}"`);
-    }
-
     this.button = button;
 
     this.button.addEventListener("click", async (event) => {
index d79fd8b093fe8f5f016027573243dd7faf628315..5fb76fd76e76de6e0c64dc94172364d5f5e44aa8 100644 (file)
@@ -42,8 +42,9 @@ define(["require", "exports", "tslib", "../../Dom/Change/Listener", "../../Dom/U
                     this.views.set(element.dataset.key, element);
                 }
             });
-            if (this.canViewResults) {
-                this.resultsView = new Results_1.default(this);
+            const button = this.element.querySelector(".showResultsButton");
+            if (this.canViewResults && button !== null) {
+                this.resultsView = new Results_1.default(this, button);
             }
             if (this.canVote) {
                 this.voteView = new Vote_1.default(this);
index 18c774c38a78072816a453371768f0877858ae1f..4c5b015ef346e3666fcf8231e1472864f63fcacb 100644 (file)
@@ -13,12 +13,8 @@ define(["require", "exports", "tslib", "../../../Ajax", "../Poll"], function (re
     exports.Results = void 0;
     Ajax = tslib_1.__importStar(Ajax);
     class Results {
-        constructor(manager) {
+        constructor(manager, button) {
             this.pollManager = manager;
-            const button = this.pollManager.getElement().querySelector(".showResultsButton");
-            if (!button) {
-                throw new Error(`Could not find button with selector ".showResultsButton" for poll "${this.pollManager.pollId}"`);
-            }
             this.button = button;
             this.button.addEventListener("click", async (event) => {
                 if (event) {