Skip to content

Commit

Permalink
fix: make it possible to switch language on the result page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Gatellier committed Nov 23, 2023
1 parent 9af25b3 commit ddef372
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions assets/js/components/SiteAnalysisResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -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`;
Expand Down
9 changes: 7 additions & 2 deletions assets/js/services/AnalysisService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -70,7 +75,7 @@ class AnalysisService {
(result) => {
apiResult = result;
ResultCacheService.add(result);
redirectToResults(result.id, pathname);
redirectToResults(result.id, resultPagePrefix);
EcoIndexDialog.close();
},
(e) => {
Expand Down

0 comments on commit ddef372

Please sign in to comment.