shlink-web-client/src/visits/reducers/shortUrlVisits.js

72 lines
2 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2018-07-29 20:25:22 +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';
/* eslint-enable padding-line-between-statements, newline-after-var */
2018-07-29 20:25:22 +03:00
export const shortUrlVisitsType = PropTypes.shape({
visits: PropTypes.array,
loading: PropTypes.bool,
error: PropTypes.bool,
});
2018-07-29 20:25:22 +03:00
const initialState = {
visits: [],
loading: false,
error: false,
2018-07-29 20:25:22 +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,
loading: true,
2018-07-29 20:25:22 +03:00
};
case GET_SHORT_URL_VISITS_ERROR:
return {
...state,
loading: false,
error: true,
2018-07-29 20:25:22 +03:00
};
case GET_SHORT_URL_VISITS:
return {
visits: action.visits,
loading: false,
error: false,
2018-07-29 20:25:22 +03:00
};
default:
return state;
}
}
export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) => async (dispatch, getState) => {
2018-07-29 20:25:22 +03:00
dispatch({ type: GET_SHORT_URL_VISITS_START });
const { selectedServer } = getState();
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 loadVisits();
dispatch({ visits, type: GET_SHORT_URL_VISITS });
} catch (e) {
dispatch({ type: GET_SHORT_URL_VISITS_ERROR });
}
2018-07-29 20:25:22 +03:00
};