From 4b06df649bdd33505512598e430a09770850408e Mon Sep 17 00:00:00 2001 From: GlebMoskalev Date: Tue, 23 Apr 2024 16:42:37 +0500 Subject: [PATCH 1/4] Task 1 --- static/focus.js | 57 ++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..42a988a 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,39 +5,48 @@ 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); + } } 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(new Error(`Request failed with status ${xhr.status}`)); + } } - } - }; + }; - xhr.send(); + xhr.onerror = function () { + reject(new Error("Request failed")); + }; + + xhr.send(); + }); } + function reqsToMap(requisites) { return requisites.reduce((acc, item) => { acc[item.ogrn] = item; From e5196adecf49adb6347b27f4bfbecbde3a54c8c2 Mon Sep 17 00:00:00 2001 From: antoshkaxxr <63500718+antoshkaxxr@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:01:37 +0500 Subject: [PATCH 2/4] Task 2 --- static/focus.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/static/focus.js b/static/focus.js index 42a988a..23c8afd 100644 --- a/static/focus.js +++ b/static/focus.js @@ -25,24 +25,19 @@ 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(new Error(`Request failed with status ${xhr.status}`)); + fetch(url) + .then(response => { + if (!response.ok) { + throw new Error(`Request failed with status ${response.status}`); } - } - }; - - xhr.onerror = function () { - reject(new Error("Request failed")); - }; - - xhr.send(); + return response.json(); + }) + .then(data => { + resolve(data); + }) + .catch(error => { + reject(new Error("Request failed")); + }); }); } From cc88f9f1ea15f363ef0df4ccd7ae1d85f664b79b Mon Sep 17 00:00:00 2001 From: antoshkaxxr <63500718+antoshkaxxr@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:26:36 +0500 Subject: [PATCH 3/4] Task 3 --- static/focus.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/focus.js b/static/focus.js index 23c8afd..dbe7687 100644 --- a/static/focus.js +++ b/static/focus.js @@ -28,7 +28,7 @@ function sendRequest(url) { fetch(url) .then(response => { if (!response.ok) { - throw new Error(`Request failed with status ${response.status}`); + alert(`Request failed with status ${response.status} ${response.statusText}`); } return response.json(); }) @@ -36,9 +36,9 @@ function sendRequest(url) { resolve(data); }) .catch(error => { - reject(new Error("Request failed")); + reject(error); }); - }); + }) } From 16482359efb3c5f8e620a6e9421971c0efd0974b Mon Sep 17 00:00:00 2001 From: antoshkaxxr <63500718+antoshkaxxr@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:41:29 +0500 Subject: [PATCH 4/4] Task 4 --- static/focus.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/static/focus.js b/static/focus.js index dbe7687..b73c671 100644 --- a/static/focus.js +++ b/static/focus.js @@ -9,11 +9,15 @@ async function run() { try { const orgOgrns = await sendRequest(API.organizationList); const ogrns = orgOgrns.join(","); - const requisites = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); + + const [requisites, analytics, buh] = await Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?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) { @@ -21,6 +25,7 @@ async function run() { } } + run(); function sendRequest(url) {