From 62d48f4fe180e8bba18577351a72c8a2b31ca884 Mon Sep 17 00:00:00 2001 From: Ural Date: Mon, 22 Nov 2021 16:29:41 +0500 Subject: [PATCH] All tasks --- static/focus.js | 84 ++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..892160c 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,57 @@ 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); + //console.log(orgOgrns); + + const 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); + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buh, "buhForms"); + render(orgsMap, orgOgrns); }); -} -run(); + /* + const ogrns = orgOgrns.join(","); + const requisites = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); -function sendRequest(url, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("GET", url, true); + 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); + */ +} - xhr.onreadystatechange = function () { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200) { - callback(JSON.parse(xhr.response)); - } - } - }; +run(); - xhr.send(); +async function sendRequest(url) { + return fetch(url) + .then((response) => { + //console.log(response); + if (response.ok) + return response.json(); + else + alert("Ошибка HTTP: " + response.status); + }); + + /*return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open("GET", url); + xhr.onload = () => resolve(JSON.parse(xhr.response)); + xhr.onerror = () => reject(xhr.statusText); + xhr.send(); + });*/ } function reqsToMap(requisites) { @@ -99,15 +119,9 @@ function renderOrganization(orgInfo, template, container) { } 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)}`; - } - + //не зря мы же это изучили в начале семестра^^ + money = new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(money); + let formatted = money.replace(/\./g, '\u2009').slice(0, -2); return `${formatted} ₽`; }