Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ФТ-202 Габов Москалев #116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions static/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
}

run();
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);
addInOrgsMap(orgsMap, analytics, "analytics");
addInOrgsMap(orgsMap, buh, "buhForms");
render(orgsMap, orgOgrns);
} catch (error) {
console.error(error);
}
}

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));
}
}
};
run();

xhr.send();
function sendRequest(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => {
if (!response.ok) {
alert(`Request failed with status ${response.status} ${response.statusText}`);
}
return response.json();
})
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
})
}


function reqsToMap(requisites) {
return requisites.reduce((acc, item) => {
acc[item.ogrn] = item;
Expand Down