From 7912e3caec2d4216dd51f6eed925d3723ce7bfb7 Mon Sep 17 00:00:00 2001 From: Ga1kinSasha Date: Tue, 23 Apr 2024 18:49:18 +0500 Subject: [PATCH] done all --- static/focus.js | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..4026532 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,32 @@ const API = { buhForms: "/api3/buh", }; -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 function run() { + const orgOgrns = await sendRequest(API.organizationList); + const ogrns = orgOgrns.join(","); + let requisites = sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); + let analytics = sendRequest(`${API.analytics}?ogrn=${ogrns}`); + let buh = sendRequest(`${API.buhForms}?ogrn=${ogrns}`); + const result = await Promise.all([requisites, analytics, buh]); + const orgsMap = reqsToMap(result[0]); + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buh, "buhForms"); + render(orgsMap, orgOgrns); } 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)); - } - } - }; +async function sendRequest(url) { + const response = await fetch(url); - xhr.send(); + if (response.ok) { + const resJson = response.json(); + console.log(resJson); + return resJson; + } else { + alert("Ошибка HTTP: " + response.status); + return Promise.reject(response.status) + } } function reqsToMap(requisites) { @@ -86,7 +81,7 @@ function renderOrganization(orgInfo, template, container) { orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] && orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] .endValue) || - 0 + 0 ); } else { money.textContent = "—"; @@ -131,4 +126,4 @@ function createAddress(address) { function createAddressItem(key) { return `${address[key].topoShortName}. ${address[key].topoValue}`; } -} +} \ No newline at end of file