From 0c1f533747366181765d6c9096937fccd970e11b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 10 Jan 2019 19:50:25 +0100 Subject: [PATCH] Updated short URL visits loading so that it loads visits in several requests --- src/utils/services/ShlinkApiClient.js | 6 +++--- src/visits/reducers/shortUrlVisits.js | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/utils/services/ShlinkApiClient.js b/src/utils/services/ShlinkApiClient.js index 72ddfca8..42d27512 100644 --- a/src/utils/services/ShlinkApiClient.js +++ b/src/utils/services/ShlinkApiClient.js @@ -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') diff --git a/src/visits/reducers/shortUrlVisits.js b/src/visits/reducers/shortUrlVisits.js index 8bdedc33..32a86f47 100644 --- a/src/visits/reducers/shortUrlVisits.js +++ b/src/visits/reducers/shortUrlVisits.js @@ -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) {