Skip to content

Commit 7cef654

Browse files
committed
refactor: improve readability
1 parent c42822e commit 7cef654

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

assets/js/components/SiteAnalysisResult.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class SiteAnalysisResult {
5252
pageResultData[key] = value;
5353

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

74-
const screenshotUrl = AnalysisService.fetchAnalysisScreenshotUrlById(analysisId)
74+
const screenshotUrl = AnalysisService.getAnalysisScreenshotUrlById(analysisId)
7575
screenshotImgElement.setAttribute("src", screenshotUrl)
7676

7777
// update the link URL of every lang switcher

assets/js/services/AnalysisService.js

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,25 @@ class AnalysisService {
2222
try {
2323
EcoIndexDialog.openPendingAnalysis(url);
2424

25-
ApiService.newAnalysisTaskByURL(url).then(
26-
(taskId) => {
27-
ApiService.fetchAnalysisTaskById(taskId).then(
28-
(taskResult) => {
29-
const ecoindex = taskResult.ecoindex_result;
25+
const taskId = await ApiService.newAnalysisTaskByURL(url)
3026

31-
if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") {
32-
ResultCacheService.add(ecoindex.detail);
33-
redirectToResults(taskId, resultUrlPrefix);
34-
}
27+
const taskResult = await ApiService.fetchAnalysisTaskById(taskId)
28+
29+
const ecoindex = taskResult.ecoindex_result;
3530

36-
if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") {
37-
const e = taskResult.ecoindex_result.error;
38-
EcoIndexDialog.openErrorMessage(e.status_code, e);
39-
}
31+
if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") {
32+
ResultCacheService.add(ecoindex.detail);
33+
redirectToResults(taskId, resultUrlPrefix);
34+
}
4035

41-
if (taskResult.status === "FAILURE") {
42-
EcoIndexDialog.openErrorMessage(599, taskResult.task_error);
43-
}
44-
},
45-
(e) => {
46-
this.#handleError(e);
47-
}
48-
);
49-
},
50-
(e) => {
51-
this.#handleError(e);
52-
}
53-
);
36+
if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") {
37+
const e = taskResult.ecoindex_result.error;
38+
EcoIndexDialog.openErrorMessage(e.status_code, e);
39+
}
40+
41+
if (taskResult.status === "FAILURE") {
42+
EcoIndexDialog.openErrorMessage(599, taskResult.task_error);
43+
}
5444
} catch (e) {
5545
this.#handleError(e);
5646
}
@@ -60,8 +50,8 @@ class AnalysisService {
6050
* @param {string} id
6151
* @returns {string}
6252
*/
63-
fetchAnalysisScreenshotUrlById(id) {
64-
return ApiService.fetchAnalysisScreenshotUrlById(id)
53+
getAnalysisScreenshotUrlById(id) {
54+
return ApiService.getAnalysisScreenshotUrlById(id)
6555
}
6656

6757
/**
@@ -79,17 +69,10 @@ class AnalysisService {
7969
// Otherwise fetch from api
8070
try {
8171
EcoIndexDialog.openAnalysisRetrieval();
82-
await ApiService.fetchAnalysisById(id).then(
83-
(result) => {
84-
apiResult = result;
85-
ResultCacheService.add(result);
86-
redirectToResults(result.id, resultPagePrefix);
87-
EcoIndexDialog.close();
88-
},
89-
(e) => {
90-
this.#handleError(e);
91-
}
92-
);
72+
apiResult = await ApiService.fetchAnalysisById(id)
73+
ResultCacheService.add(apiResult);
74+
redirectToResults(apiResult.id, resultPagePrefix);
75+
EcoIndexDialog.close();
9376
} catch (e) {
9477
this.#handleError(e);
9578
return null;

assets/js/services/ApiService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ApiService {
5050
* @param {string} id Analysis Id
5151
* @returns {string} API URL
5252
*/
53-
fetchAnalysisScreenshotUrlById(id) {
53+
getAnalysisScreenshotUrlById(id) {
5454
return `${BASE_URL}ecoindexes/${id}/screenshot`;
5555
}
5656

0 commit comments

Comments
 (0)