2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-03-08 21:40:43 +03:00
|
|
|
import { flatten, prop, range, splitEvery } from 'ramda';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-disable padding-line-between-statements, newline-after-var */
|
2018-09-08 09:49:49 +03:00
|
|
|
export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START';
|
|
|
|
export const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR';
|
|
|
|
export const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS';
|
2019-03-04 21:21:46 +03:00
|
|
|
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
|
2019-03-08 21:40:43 +03:00
|
|
|
export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL';
|
2018-08-26 11:49:23 +03:00
|
|
|
/* eslint-enable padding-line-between-statements, newline-after-var */
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2018-08-26 20:19:45 +03:00
|
|
|
export const shortUrlVisitsType = PropTypes.shape({
|
2018-08-26 00:39:27 +03:00
|
|
|
visits: PropTypes.array,
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
error: PropTypes.bool,
|
2018-08-26 20:19:45 +03:00
|
|
|
});
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
const initialState = {
|
|
|
|
visits: [],
|
|
|
|
loading: false,
|
2019-03-04 21:21:46 +03:00
|
|
|
loadingLarge: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: false,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelLoad: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
|
2018-09-01 12:26:35 +03:00
|
|
|
export default function reducer(state = initialState, action) {
|
2018-07-29 20:25:22 +03:00
|
|
|
switch (action.type) {
|
|
|
|
case GET_SHORT_URL_VISITS_START:
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-26 00:39:27 +03:00
|
|
|
loading: true,
|
2019-03-04 21:21:46 +03:00
|
|
|
loadingLarge: false,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelLoad: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
case GET_SHORT_URL_VISITS_ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: false,
|
2019-03-04 21:21:46 +03:00
|
|
|
loadingLarge: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: true,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelLoad: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
case GET_SHORT_URL_VISITS:
|
|
|
|
return {
|
|
|
|
visits: action.visits,
|
|
|
|
loading: false,
|
2019-03-04 21:21:46 +03:00
|
|
|
loadingLarge: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: false,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelLoad: false,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2019-03-04 21:21:46 +03:00
|
|
|
case GET_SHORT_URL_VISITS_LARGE:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loadingLarge: true,
|
|
|
|
};
|
2019-03-08 21:40:43 +03:00
|
|
|
case GET_SHORT_URL_VISITS_CANCEL:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
cancelLoad: true,
|
|
|
|
};
|
2018-07-29 20:25:22 +03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:32:02 +03:00
|
|
|
export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) => async (dispatch, getState) => {
|
2018-07-29 20:25:22 +03:00
|
|
|
dispatch({ type: GET_SHORT_URL_VISITS_START });
|
|
|
|
|
2018-12-18 12:14:25 +03:00
|
|
|
const { selectedServer } = getState();
|
2019-01-10 21:50:25 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:21:46 +03:00
|
|
|
// If there are more pages, make requests in blocks of 4
|
|
|
|
const parallelRequestsCount = 4;
|
|
|
|
const parallelStartingPage = 2;
|
|
|
|
const pagesRange = range(parallelStartingPage, pagination.pagesCount + 1);
|
|
|
|
const pagesBlocks = splitEvery(parallelRequestsCount, pagesRange);
|
|
|
|
|
|
|
|
if (pagination.pagesCount - 1 > parallelRequestsCount) {
|
|
|
|
dispatch({ type: GET_SHORT_URL_VISITS_LARGE });
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.concat(await loadPagesBlocks(pagesBlocks));
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadPagesBlocks = async (pagesBlocks, index = 0) => {
|
2019-03-08 21:40:43 +03:00
|
|
|
const { shortUrlVisits: { cancelLoad } } = getState();
|
|
|
|
|
|
|
|
if (cancelLoad) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:21:46 +03:00
|
|
|
const data = await loadVisitsInParallel(pagesBlocks[index]);
|
|
|
|
|
|
|
|
if (index < pagesBlocks.length - 1) {
|
|
|
|
return data.concat(await loadPagesBlocks(pagesBlocks, index + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2019-01-10 21:50:25 +03:00
|
|
|
};
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2019-03-04 21:21:46 +03:00
|
|
|
const loadVisitsInParallel = (pages) =>
|
2019-03-08 21:40:43 +03:00
|
|
|
Promise.all(pages.map(
|
|
|
|
(page) =>
|
|
|
|
getShortUrlVisits(shortCode, { ...dates, page, itemsPerPage })
|
|
|
|
.then(prop('data'))
|
|
|
|
))
|
|
|
|
.then(flatten);
|
2019-03-04 21:21:46 +03:00
|
|
|
|
2018-09-01 12:26:35 +03:00
|
|
|
try {
|
2019-01-10 21:50:25 +03:00
|
|
|
const visits = await loadVisits();
|
2018-09-01 12:26:35 +03:00
|
|
|
|
|
|
|
dispatch({ visits, type: GET_SHORT_URL_VISITS });
|
|
|
|
} catch (e) {
|
|
|
|
dispatch({ type: GET_SHORT_URL_VISITS_ERROR });
|
|
|
|
}
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2019-03-08 21:40:43 +03:00
|
|
|
|
|
|
|
export const cancelGetShortUrlVisits = () => ({ type: GET_SHORT_URL_VISITS_CANCEL });
|