diff --git a/static/focus.js b/static/focus.js index 4b43735..e64a7d5 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,33 @@ const API = { buhForms: "/api3/buh", }; -function run() { - sendRequest(API.organizationList, (orgOgrns) => { - const ogrns = orgOgrns.join(","); - sendRequest(`${API.orgReqs}?ogrn=${ogrns}`, (requisites) => { +async function run() { + let orgOgrns = await sendRequest(API.organizationList) + let ogrns = orgOgrns.join(","); + + Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?ogrn=${ogrns}`) + ]) + .then(([requisites, analytics, buh]) => { 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); - }); - }); - }); - }); + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buh, "buhForms"); + render(orgsMap, orgOgrns); + }) + .catch((error) => alert(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)); - } - } - }; - - xhr.send(); +async function sendRequest(url, _) { + let response = await fetch(url); + if (response.ok) { + return await response.json(); + } else { + throw new Error("Ошибка HTTP: " + response.status) + } } function reqsToMap(requisites) { @@ -86,7 +82,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 +127,4 @@ function createAddress(address) { function createAddressItem(key) { return `${address[key].topoShortName}. ${address[key].topoValue}`; } -} +} \ No newline at end of file