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 Красиков #132

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
47 changes: 20 additions & 27 deletions static/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
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(",");
const [orgReqs, analytics, buhForms] = await Promise.all([
sendRequest(`${API.orgReqs}?ogrn=${ogrns}`),
sendRequest(`${API.analytics}?ogrn=${ogrns}`),
sendRequest(`${API.buhForms}?ogrn=${ogrns}`),
]);
const orgsMap = reqsToMap(orgReqs);
addInOrgsMap(orgsMap, analytics, "analytics");
addInOrgsMap(orgsMap, buhForms, "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));
}
}
};

xhr.send();
async function sendRequest(url) {
const response = await fetch(url, { method: "GET" });
if (response.ok) {
return response.json();
}
const error = `${response.status} ${response.statusText}`;
alert(error);
return Promise.reject(error);
}

function reqsToMap(requisites) {
Expand Down