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 Городничая Шаров #111

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
54 changes: 29 additions & 25 deletions static/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,41 @@ 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);
});
});
});
});

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:", 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));
function sendRequest(url) {
return fetch(url)
.then(response => {
if (response.status === 200) {
return Promise.resolve(response.json());
} else if (response.status >= 300) {
alert(`${response.status} ${response.statusText}`);
return Promise.reject("error error");
} else {
return Promise.reject(response.status);
}
}
};

xhr.send();
});
}

function reqsToMap(requisites) {
Expand Down