From f0331427e18378aaff7f0a4113c97b85d5f4d1d6 Mon Sep 17 00:00:00 2001 From: IronRino Date: Tue, 23 Apr 2024 16:39:59 +0500 Subject: [PATCH 1/4] 1 task --- static/focus.js | 56 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..4a73192 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,45 @@ const API = { buhForms: "/api3/buh", }; -function run() { - sendRequest(API.organizationList, (orgOgrns) => { +async function run() { + try { + const orgOgrns = await sendRequest(API.organizationList); 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); - }); - }); - }); - }); + const requisites = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); + + const orgsMap = reqsToMap(requisites); + + const analytics = await sendRequest(`${API.analytics}?ogrn=${ogrns}`); + addInOrgsMap(orgsMap, analytics, "analytics"); + + const buh = await sendRequest(`${API.buhForms}?ogrn=${ogrns}`); + addInOrgsMap(orgsMap, buh, "buhForms"); + + render(orgsMap, orgOgrns); + } catch (error) { + console.error("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)); +function sendRequest(url) { + 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.status); + } } - } - }; + }; - xhr.send(); + xhr.send(); + }); } function reqsToMap(requisites) { From 18af8980451f8d1cd52deaa7d57a7513e0787ed9 Mon Sep 17 00:00:00 2001 From: VinkiLilai <59568559+VinkiLilai@users.noreply.github.com> Date: Tue, 23 Apr 2024 16:47:12 +0500 Subject: [PATCH 2/4] 2 --- static/focus.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4a73192..852c106 100644 --- a/static/focus.js +++ b/static/focus.js @@ -28,22 +28,14 @@ async function run() { run(); function sendRequest(url) { - 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.status); - } + return fetch(url) + .then(response => { + if (response.status === 200) { + return Promise.resolve(response.json()); + } else { + return Promise.reject(response.status); } - }; - - xhr.send(); - }); + }); } function reqsToMap(requisites) { From cd6d041bfb185a8548aedf0ea87c6eb03671bbb5 Mon Sep 17 00:00:00 2001 From: IronRino Date: Tue, 23 Apr 2024 17:01:05 +0500 Subject: [PATCH 3/4] 3 task --- static/focus.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/static/focus.js b/static/focus.js index 852c106..0800f4b 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", }; @@ -32,6 +32,9 @@ function sendRequest(url) { .then(response => { if (response.status === 200) { return Promise.resolve(response.json()); + } else if (response.status >= 300) { + alert(`${response.status} ${response.statusText}`); + return Promise.reject("error error"); } else { return Promise.reject(response.status); } From 2f74680f1d9a5aaeec13a97ca40a459c29288ce6 Mon Sep 17 00:00:00 2001 From: VinkiLilai <59568559+VinkiLilai@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:20:24 +0500 Subject: [PATCH 4/4] 4 --- static/focus.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/static/focus.js b/static/focus.js index 0800f4b..62d6dd5 100644 --- a/static/focus.js +++ b/static/focus.js @@ -1,6 +1,6 @@ const API = { organizationList: "/orgsList", - analytics: "/api3/analitics", + analytics: "/api3/analytics", orgReqs: "/api3/reqBase", buhForms: "/api3/buh", }; @@ -9,16 +9,17 @@ async function run() { try { const orgOgrns = await sendRequest(API.organizationList); const ogrns = orgOgrns.join(","); - const requisites = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); - const orgsMap = reqsToMap(requisites); + const [requisites, analytics, buh] = + await Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?ogrn=${ogrns}`), + ]); - const analytics = await sendRequest(`${API.analytics}?ogrn=${ogrns}`); + const orgsMap = reqsToMap(requisites); addInOrgsMap(orgsMap, analytics, "analytics"); - - const buh = await sendRequest(`${API.buhForms}?ogrn=${ogrns}`); addInOrgsMap(orgsMap, buh, "buhForms"); - render(orgsMap, orgOgrns); } catch (error) { console.error("Error:", error);