Updated short URL visits loading so that it loads visits in several requests

This commit is contained in:
Alejandro Celaya 2019-01-10 19:50:25 +01:00
parent 58d3a59e58
commit 0c1f533747
2 changed files with 18 additions and 5 deletions

View file

@ -22,9 +22,9 @@ export default class ShlinkApiClient {
.then((resp) => resp.data);
};
getShortUrlVisits = (shortCode, dates) =>
this._performRequest(`/short-urls/${shortCode}/visits`, 'GET', dates)
.then((resp) => resp.data.visits.data);
getShortUrlVisits = (shortCode, query) =>
this._performRequest(`/short-urls/${shortCode}/visits`, 'GET', query)
.then((resp) => resp.data.visits);
getShortUrl = (shortCode) =>
this._performRequest(`/short-urls/${shortCode}`, 'GET')

View file

@ -46,10 +46,23 @@ export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) =>
dispatch({ type: GET_SHORT_URL_VISITS_START });
const { selectedServer } = getState();
const shlinkApiClient = buildShlinkApiClient(selectedServer);
const { getShortUrlVisits } = buildShlinkApiClient(selectedServer);
const itemsPerPage = 5000;
const isLastPage = ({ currentPage, pagesCount }) => currentPage >= pagesCount;
const loadVisits = async (page = 1) => {
const { pagination, data } = await getShortUrlVisits(shortCode, { ...dates, page, itemsPerPage });
// If pagination was not returned, then this is an older shlink version. Just return data
if (!pagination || isLastPage(pagination)) {
return data;
}
return data.concat(await loadVisits(page + 1));
};
try {
const visits = await shlinkApiClient.getShortUrlVisits(shortCode, dates);
const visits = await loadVisits();
dispatch({ visits, type: GET_SHORT_URL_VISITS });
} catch (e) {