download household on same address

This commit is contained in:
2021-10-01 14:34:27 +02:00
parent c179649c56
commit 58a8949cac
4 changed files with 64 additions and 62 deletions

View File

@@ -0,0 +1,41 @@
const _fetchAction = (page, uri, params) => {
const item_per_page = 50;
if (params === undefined) {
params = {};
}
let url = uri + '?' + new URLSearchParams({ item_per_page, page, ...params });
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
}).then(response => {
if (response.ok) { return response.json(); }
throw Error({ m: response.statusText });
});
};
const fetchResults = async (uri, params) => {
let promises = [],
page = 1;
let firstData = await _fetchAction(page, uri, params);
console.log('firstData', firstData);
promises.push(Promise.resolve(firstData.results));
console.log('more ?', firstData.pagination.more);
if (firstData.pagination.more) {
do {
page = ++page;
promises.push(_fetchAction(page, uri, params).then(r => Promise.resolve(r.results)));
} while (page * firstData.pagination.items_per_page < firstData.count)
}
return Promise.all(promises).then(values => values.flat());
};
export {
fetchResults
};

View File

@@ -1,15 +1,7 @@
import { fetchResults } from 'ChillMainAssets/lib/api/download.js';
const fetchScopes = () => {
return window.fetch('/api/1.0/main/scope.json').then(response => {
if (response.ok) {
return response.json();
}
}).then(data => {
//console.log(data);
return new Promise((resolve, reject) => {
//console.log(data);
resolve(data.results);
});
});
return fetchResults('/api/1.0/main/scope.json');
};
export {