From ec4bf527e791d5b923f3f5a833a46c7bcc20dce8 Mon Sep 17 00:00:00 2001 From: Pirantel Date: Tue, 7 May 2024 13:13:13 +0500 Subject: [PATCH 1/4] 1 --- static/focus.js | 56 +++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..83047c1 100644 --- a/static/focus.js +++ b/static/focus.js @@ -6,36 +6,46 @@ const API = { }; function run() { - sendRequest(API.organizationList, (orgOgrns) => { - const ogrns = orgOgrns.join(","); - sendRequest(`${API.orgReqs}?ogrn=${ogrns}`, (requisites) => { - const orgsMap = reqsToMap(requisites); - sendRequest(`${API.analytics}?ogrn=${ogrns}`, (analytics) => { - addInOrgsMap(orgsMap, analytics, "analytics"); - sendRequest(`${API.buhForms}?ogrn=${ogrns}`, (buh) => { - addInOrgsMap(orgsMap, buh, "buhForms"); - render(orgsMap, orgOgrns); - }); - }); - }); - }); + (async () => { + try { + const orgOgrns = await sendRequest(API.organizationList); + const ogrns = orgOgrns.join(","); + const orgReqs = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); + const orgsMap = reqsToMap(orgReqs); + const analytics = await sendRequest(`${API.analytics}?ogrn=${ogrns}`); + addInOrgsMap(orgsMap, analytics, "analytics"); + const buhForms = await sendRequest(`${API.buhForms}?ogrn=${ogrns}`); + addInOrgsMap(orgsMap, buhForms, "buhForms"); + render(orgsMap, orgOgrns); + } catch (error) { + console.error(error); + } + })(); } run(); function sendRequest(url, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("GET", url, true); - - xhr.onreadystatechange = function () { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200) { - callback(JSON.parse(xhr.response)); + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open("GET", url, true); + + xhr.onreadystatechange = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 200) { + resolve(JSON.parse(xhr.response)); + } else { + reject(xhr.statusText); + } } - } - }; + }; + + xhr.onerror = function () { + reject(xhr.statusText); + }; - xhr.send(); + xhr.send(); + }); } function reqsToMap(requisites) { From 4ed8325cb04686e3d1b0bb65c7bdda5f5a91ecd0 Mon Sep 17 00:00:00 2001 From: Pirantel Date: Tue, 7 May 2024 13:17:56 +0500 Subject: [PATCH 2/4] 2 --- static/focus.js | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/static/focus.js b/static/focus.js index 83047c1..cac3ad3 100644 --- a/static/focus.js +++ b/static/focus.js @@ -25,27 +25,12 @@ function run() { run(); -function sendRequest(url, callback) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.open("GET", url, true); - - xhr.onreadystatechange = function () { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200) { - resolve(JSON.parse(xhr.response)); - } else { - reject(xhr.statusText); - } - } - }; - - xhr.onerror = function () { - reject(xhr.statusText); - }; - - xhr.send(); - }); +async function sendRequest(url) { + const response = await fetch(url, { method: "GET" }); + if (response.ok) { + return response.json(); + } + throw new Error(response.statusText); } function reqsToMap(requisites) { From a10bf977fadd7b1e897bafbb52eee71f5f9430d6 Mon Sep 17 00:00:00 2001 From: Pirantel Date: Tue, 7 May 2024 13:23:40 +0500 Subject: [PATCH 3/4] 3 --- static/focus.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/static/focus.js b/static/focus.js index cac3ad3..c1cbd90 100644 --- a/static/focus.js +++ b/static/focus.js @@ -1,6 +1,6 @@ const API = { organizationList: "/orgsList", - analytics: "/api3/analytics", + analytics: "/api3/analitics", orgReqs: "/api3/reqBase", buhForms: "/api3/buh", }; @@ -30,7 +30,9 @@ async function sendRequest(url) { if (response.ok) { return response.json(); } - throw new Error(response.statusText); + const error = `${response.status} ${response.statusText}`; + alert(error); + return Promise.reject(error); } function reqsToMap(requisites) { From f2d0a483efe54d92150fc3434ea5246242e5d28d Mon Sep 17 00:00:00 2001 From: Pirantel Date: Tue, 7 May 2024 13:25:49 +0500 Subject: [PATCH 4/4] 4 --- static/focus.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/static/focus.js b/static/focus.js index c1cbd90..3eaf194 100644 --- a/static/focus.js +++ b/static/focus.js @@ -1,26 +1,22 @@ const API = { organizationList: "/orgsList", - analytics: "/api3/analitics", + analytics: "/api3/analytics", orgReqs: "/api3/reqBase", buhForms: "/api3/buh", }; -function run() { - (async () => { - try { - const orgOgrns = await sendRequest(API.organizationList); - const ogrns = orgOgrns.join(","); - const orgReqs = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); - const orgsMap = reqsToMap(orgReqs); - const analytics = await sendRequest(`${API.analytics}?ogrn=${ogrns}`); - addInOrgsMap(orgsMap, analytics, "analytics"); - const buhForms = await sendRequest(`${API.buhForms}?ogrn=${ogrns}`); - addInOrgsMap(orgsMap, buhForms, "buhForms"); - render(orgsMap, orgOgrns); - } catch (error) { - console.error(error); - } - })(); +async function run() { + const orgOgrns = await sendRequest(API.organizationList); + const ogrns = orgOgrns.join(","); + const [orgReqs, analytics, buhForms] = await Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?ogrn=${ogrns}`), + ]); + const orgsMap = reqsToMap(orgReqs); + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buhForms, "buhForms"); + render(orgsMap, orgOgrns); } run();