From 80e3a7b3254972911689cbe7f6b264fbb9ad9fd0 Mon Sep 17 00:00:00 2001 From: bozhkovkost <71607999+bozhkovkost@users.noreply.github.com> Date: Thu, 3 Jun 2021 11:50:11 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=91=D0=BE=D0=B6=D0=BA=D0=BE=D0=B2=5F=20a?= =?UTF-8?q?sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/focus.js | 82 +++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 60 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..8db3647 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,30 @@ 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 + alert("Ошибка HTTP: " + response.status); } function reqsToMap(requisites) { @@ -44,90 +37,59 @@ function reqsToMap(requisites) { return acc; }, {}); } - function addInOrgsMap(orgsMap, additionalInfo, key) { for (const item of additionalInfo) { orgsMap[item.ogrn][key] = item[key]; } } - function render(organizationsInfo, organizationsOrder) { const table = document.getElementById("organizations"); table.classList.remove("hide"); - const template = document.getElementById("orgTemplate"); const container = table.querySelector("tbody"); - organizationsOrder.forEach((item) => { renderOrganization(organizationsInfo[item], template, container); }); } - function renderOrganization(orgInfo, template, container) { const clone = document.importNode(template.content, true); const name = clone.querySelector(".name"); const indebtedness = clone.querySelector(".indebtedness"); const money = clone.querySelector(".money"); const address = clone.querySelector(".address"); - name.textContent = (orgInfo.UL && orgInfo.UL.legalName && orgInfo.UL.legalName.short) || ""; indebtedness.textContent = formatMoney(orgInfo.analytics.s1002 || 0); - if ( orgInfo.buhForms && orgInfo.buhForms.length && - orgInfo.buhForms[orgInfo.buhForms.length - 1] && - orgInfo.buhForms[orgInfo.buhForms.length - 1].year === 2017 - ) { - money.textContent = formatMoney( - (orgInfo.buhForms[orgInfo.buhForms.length - 1].form2 && - orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] && - orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] - .endValue) || - 0 - ); + @@ -91,26 +77,20 @@ function renderOrganization(orgInfo, template, container) { } else { money.textContent = "—"; } - const addressFromServer = orgInfo.UL.legalAddress.parsedAddressRF; address.textContent = createAddress(addressFromServer); - container.appendChild(clone); } - function formatMoney(money) { let formatted = money.toFixed(2); formatted = formatted.replace(".", ","); - const rounded = money.toFixed(0); const numLen = rounded.length; for (let i = numLen - 3; i > 0; i -= 3) { formatted = `${formatted.slice(0, i)} ${formatted.slice(i)}`; } - return `${formatted} ₽`; } - function createAddress(address) { const addressToRender = []; if (address.regionName) { - addressToRender.push(createAddressItem("regionName")); - } - if (address.city) { - addressToRender.push(createAddressItem("city")); - } - if (address.street) { - addressToRender.push(createAddressItem("street")); - } + @@ -125,10 +105,8 @@ function createAddress(address) { if (address.house) { addressToRender.push(createAddressItem("house")); } - return addressToRender.join(", "); - function createAddressItem(key) { return `${address[key].topoShortName}. ${address[key].topoValue}`; } From 1f645439990ba943d3794946d1e4d34859340d4d Mon Sep 17 00:00:00 2001 From: bozhkovkost <71607999+bozhkovkost@users.noreply.github.com> Date: Thu, 3 Jun 2021 11:50:55 +0500 Subject: [PATCH 2/2] Update focus.js --- static/focus.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/static/focus.js b/static/focus.js index 8db3647..713f5b6 100644 --- a/static/focus.js +++ b/static/focus.js @@ -4,7 +4,6 @@ const API = { orgReqs: "/api3/reqBase", buhForms: "/api3/buh", }; - async function run() { let orgOgrns = await sendRequest(API.organizationList) let ogrns = orgOgrns.join(","); @@ -21,7 +20,6 @@ async function run() { }) .catch((error) => alert(error)) } - run(); async function sendRequest(url) { let response = await fetch(url); @@ -30,7 +28,6 @@ async function sendRequest(url) { else alert("Ошибка HTTP: " + response.status); } - function reqsToMap(requisites) { return requisites.reduce((acc, item) => { acc[item.ogrn] = item;