From ddef3727677d7a90f1d5b620ff2b1f4f4b856b91 Mon Sep 17 00:00:00 2001 From: Bastien Gatellier Date: Thu, 23 Nov 2023 21:06:29 +0100 Subject: [PATCH] fix: make it possible to switch language on the result page --- assets/js/components/SiteAnalysisResult.js | 13 +++++++++++++ assets/js/services/AnalysisService.js | 9 +++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/assets/js/components/SiteAnalysisResult.js b/assets/js/components/SiteAnalysisResult.js index 11638b2c..1709d286 100644 --- a/assets/js/components/SiteAnalysisResult.js +++ b/assets/js/components/SiteAnalysisResult.js @@ -38,6 +38,7 @@ class SiteAnalysisResult { */ async _init() { const urlParams = new URLSearchParams(window.location.search); + const langSwitchersElements = document.querySelectorAll(".language-switcher a.lang") let pageResultData = {}; @@ -48,12 +49,24 @@ class SiteAnalysisResult { pageResultData[key] = value; } + // update the link URL of every lang switcher + langSwitchersElements.forEach((a) => { + const href = a.getAttribute("href") + "?" + urlParams.toString() + a.setAttribute("href", href) + }) + // else fetch analysis result from id // NOTE : url params example to test : "?id=ec839aca-7c12-42e8-8541-5f7f94c36b7f } else if (urlParams.has("id")) { const id = urlParams.get("id"); // window.location.pathname is something like /resultat (in french) or /en/result (in english) pageResultData = await AnalysisService.fetchAnalysisById(id, window.location.pathname); + + // update the link URL of every lang switcher + langSwitchersElements.forEach((a) => { + const href = a.getAttribute("href") + "?id=" + id + a.setAttribute("href", href) + }) } else { // TODO: redirect to error page or show dialog ? window.location = `${window.location.origin}/erreur/?status=404`; diff --git a/assets/js/services/AnalysisService.js b/assets/js/services/AnalysisService.js index 588230a9..ca20752e 100644 --- a/assets/js/services/AnalysisService.js +++ b/assets/js/services/AnalysisService.js @@ -56,7 +56,12 @@ class AnalysisService { } } - async fetchAnalysisById(id, pathname) { + /** + * + * @param {string} id + * @param {string} resultPagePrefix + */ + async fetchAnalysisById(id, resultPagePrefix) { // Check local storage: if analysis results object exist returns it let apiResult = ResultCacheService.get(id); if (apiResult) { @@ -70,7 +75,7 @@ class AnalysisService { (result) => { apiResult = result; ResultCacheService.add(result); - redirectToResults(result.id, pathname); + redirectToResults(result.id, resultPagePrefix); EcoIndexDialog.close(); }, (e) => {