From 0207e4de9a1db4aeec54ebb2f9789fcfbe62378a Mon Sep 17 00:00:00 2001
From: senza <maximesenzamici@gmail.com>
Date: Fri, 14 Aug 2020 14:37:36 +0200
Subject: [PATCH] Update poll options on navigate

---
 js/main.js       | 45 ++++++++++++++++++++++++++-------------------
 js/polls-math.js | 32 +++++++++++++++++++++++++++++---
 2 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/js/main.js b/js/main.js
index c199d6b..588e59d 100644
--- a/js/main.js
+++ b/js/main.js
@@ -1,13 +1,38 @@
 /*==========================
 	Global JS for Polls component
 ==========================*/
+/*imports*/
+
+//import {getPollTotalInfo} from './polls-math.js';
+
+/* Load necessary scripts on load & navigation */
+
+window.addEventListener('load', () => {
+  //set the domain source constant
+  console.log("loading main.js");
+  //update poll ID's
+  setTimeout(function(){
+    try {
+      newOptionContent();
+      //getPollTotalInfo();
+    } catch (error) {
+      console.log(error);
+    } 
+  }, 1500);
+
+  document.querySelectorAll("solid-router")[0].addEventListener("navigate",()=> {
+    console.log("navigating");
+    //newOptionContent();
+    //getPollTotalInfo();
+  });
+});
+
 
 /* ===================
 	JS For Tabs
    ================== */
 
 
-
 function showHideBlocks(activeCTAClass,inactiveCTAClass,diplayBlockClass,hideBlockClass,styleWanted){
 	//get all blocks
 	activeCTAClass = document.querySelectorAll(activeCTAClass);
@@ -51,24 +76,6 @@ function showHideBlocks(activeCTAClass,inactiveCTAClass,diplayBlockClass,hideBlo
 ================== */
 
 
-window.addEventListener('load', () => {
-  //set the domain source constant
-  console.log("loading here");
-  //update poll ID's
-  setTimeout(function(){
-    try {
-      newOptionContent();
-    } catch (error) {
-      console.log(error);
-    } 
-  }, 1500);
-
-  document.querySelectorAll("solid-router")[0].addEventListener("navigate",()=> {
-    console.log("navigating");
-    //newOptionContent();
-  });
-});
-
 
 //change content of button
 function newOptionContent(){
diff --git a/js/polls-math.js b/js/polls-math.js
index f2d7581..0a83d4c 100644
--- a/js/polls-math.js
+++ b/js/polls-math.js
@@ -11,7 +11,7 @@
  *********/
 window.addEventListener('load', () => {
   //set the domain source constant
-  console.log("loading");
+  console.log("loading math.js");
   const dataSourceURI = document.querySelector("solid-display").dataset.src;
   
   const urlComponents = new URL(dataSourceURI);
@@ -19,6 +19,7 @@ window.addEventListener('load', () => {
   console.log("the data domain is",dataSourceDomain);
   //update poll ID's
   try {
+    console.log("update the poll ID on load");
     getPollTotalInfo();
   } catch (error) {
     console.log(error);
@@ -26,7 +27,14 @@ window.addEventListener('load', () => {
 
   //update ID's on navigate
   document.querySelector("solid-router").addEventListener("navigate",()=> {
-    getPollTotalInfo();
+    
+    console.log("updating on navigation - math.js");
+    try {
+      getPollTotalInfo();
+      updateFormSource();
+    }catch (error) {
+      console.log("error on navigation : ",error);
+    }
   });
 
   /*********
@@ -36,6 +44,7 @@ window.addEventListener('load', () => {
     //get value of data-scr linked to poll page
     const pollPage = document.querySelectorAll("#pollPage")[0];
     const pageTitle = pollPage.querySelectorAll("solid-display[id=title]");
+    console.log("page title is",pageTitle);
 
     for (let i = 0; i < pageTitle.length; i++){
       //get the data src node of first sib display & extract the url
@@ -51,12 +60,12 @@ window.addEventListener('load', () => {
       setTotalResultId(pollPage,pollId);
     }
   }
-
   /**
    * push poll ID into data-scr url of current poll
    */
   function setTotalResultId(currentPage,currentId){
     //get the vote blocks
+    console.log("setting currnt source");
     const currentPollValues = currentPage.querySelectorAll("[id=poll-votes-values]")[0];
     //add the poll number
     const currentPollSrc = dataSourceDomain + "/polls/total_votes/" + currentId + "/";
@@ -145,6 +154,20 @@ window.addEventListener('load', () => {
 	});
 });
 
+function updateFormSource(){
+  const pollSection = document.querySelectorAll("#pollListing,#pollPage,#add-survey");
+  
+  pollSection.forEach((section) => {
+    console.log("poll section is :", section);
+    const applicationForms = section.querySelectorAll("solid-form");
+    console.log("forms are : ",applicationForms);
+    applicationForms.forEach((form) => {
+      console.log("updating form sources");
+      form.dataset.src = form.dataset.src;
+    });
+  });
+}
+
 function setChoiceNumber(poll){
 	/* Display each choice number */
   const choiceValueFields = poll.querySelectorAll("div[name=choiceValue]");
@@ -176,3 +199,6 @@ function displayTotalVotes(poll,totalCount){
   //update value in total
   poll.previousElementSibling.querySelector("#totalVotesDisplay").textContent = totalVoteContent;
 }
+
+
+//export  {getPollTotalInfo};
\ No newline at end of file
-- 
GitLab