Skip to content

Commit

Permalink
refactor: improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
bgatellier committed Apr 13, 2024
1 parent c42822e commit 7cef654
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
4 changes: 2 additions & 2 deletions assets/js/components/SiteAnalysisResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SiteAnalysisResult {
pageResultData[key] = value;

if (key === "id") {
const screenshotUrl = AnalysisService.fetchAnalysisScreenshotUrlById(urlParams.get("id"))
const screenshotUrl = AnalysisService.getAnalysisScreenshotUrlById(urlParams.get("id"))
screenshotImgElement.setAttribute("src", screenshotUrl)
}
}
Expand All @@ -71,7 +71,7 @@ class SiteAnalysisResult {
// window.location.pathname is something like /resultat (in french) or /en/result (in english)
pageResultData = await AnalysisService.fetchAnalysisById(analysisId, window.location.pathname)

const screenshotUrl = AnalysisService.fetchAnalysisScreenshotUrlById(analysisId)
const screenshotUrl = AnalysisService.getAnalysisScreenshotUrlById(analysisId)
screenshotImgElement.setAttribute("src", screenshotUrl)

// update the link URL of every lang switcher
Expand Down
61 changes: 22 additions & 39 deletions assets/js/services/AnalysisService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,25 @@ class AnalysisService {
try {
EcoIndexDialog.openPendingAnalysis(url);

ApiService.newAnalysisTaskByURL(url).then(
(taskId) => {
ApiService.fetchAnalysisTaskById(taskId).then(
(taskResult) => {
const ecoindex = taskResult.ecoindex_result;
const taskId = await ApiService.newAnalysisTaskByURL(url)

if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") {
ResultCacheService.add(ecoindex.detail);
redirectToResults(taskId, resultUrlPrefix);
}
const taskResult = await ApiService.fetchAnalysisTaskById(taskId)

const ecoindex = taskResult.ecoindex_result;

if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") {
const e = taskResult.ecoindex_result.error;
EcoIndexDialog.openErrorMessage(e.status_code, e);
}
if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") {
ResultCacheService.add(ecoindex.detail);
redirectToResults(taskId, resultUrlPrefix);
}

if (taskResult.status === "FAILURE") {
EcoIndexDialog.openErrorMessage(599, taskResult.task_error);
}
},
(e) => {
this.#handleError(e);
}
);
},
(e) => {
this.#handleError(e);
}
);
if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") {
const e = taskResult.ecoindex_result.error;
EcoIndexDialog.openErrorMessage(e.status_code, e);
}

if (taskResult.status === "FAILURE") {
EcoIndexDialog.openErrorMessage(599, taskResult.task_error);
}
} catch (e) {
this.#handleError(e);
}
Expand All @@ -60,8 +50,8 @@ class AnalysisService {
* @param {string} id
* @returns {string}
*/
fetchAnalysisScreenshotUrlById(id) {
return ApiService.fetchAnalysisScreenshotUrlById(id)
getAnalysisScreenshotUrlById(id) {
return ApiService.getAnalysisScreenshotUrlById(id)
}

/**
Expand All @@ -79,17 +69,10 @@ class AnalysisService {
// Otherwise fetch from api
try {
EcoIndexDialog.openAnalysisRetrieval();
await ApiService.fetchAnalysisById(id).then(
(result) => {
apiResult = result;
ResultCacheService.add(result);
redirectToResults(result.id, resultPagePrefix);
EcoIndexDialog.close();
},
(e) => {
this.#handleError(e);
}
);
apiResult = await ApiService.fetchAnalysisById(id)
ResultCacheService.add(apiResult);
redirectToResults(apiResult.id, resultPagePrefix);
EcoIndexDialog.close();
} catch (e) {
this.#handleError(e);
return null;
Expand Down
2 changes: 1 addition & 1 deletion assets/js/services/ApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ApiService {
* @param {string} id Analysis Id
* @returns {string} API URL
*/
fetchAnalysisScreenshotUrlById(id) {
getAnalysisScreenshotUrlById(id) {
return `${BASE_URL}ecoindexes/${id}/screenshot`;
}

Expand Down

0 comments on commit 7cef654

Please sign in to comment.