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

77 lines
3 KiB
JavaScript
Raw Normal View History

import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types';
import { shortUrlMatches } from '../../short-urls/helpers';
import { VisitType } from '../types';
2020-05-10 20:02:58 +03:00
import { getVisitsWithLoader } from './common';
import { CREATE_VISIT } from './visitCreation';
2018-07-29 20:25:22 +03:00
/* eslint-disable padding-line-between-statements */
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';
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
export const GET_SHORT_URL_VISITS_CANCEL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_CANCEL';
export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_PROGRESS_CHANGED';
/* eslint-enable padding-line-between-statements */
2018-07-29 20:25:22 +03:00
export const shortUrlVisitsType = PropTypes.shape({ // TODO Should extend from VisitInfoType
visits: PropTypes.arrayOf(VisitType),
shortCode: PropTypes.string,
domain: PropTypes.string,
loading: PropTypes.bool,
2020-05-10 20:02:58 +03:00
loadingLarge: PropTypes.bool,
error: PropTypes.bool,
progress: PropTypes.number,
});
2018-07-29 20:25:22 +03:00
const initialState = {
visits: [],
shortCode: '',
domain: undefined,
2018-07-29 20:25:22 +03:00
loading: false,
loadingLarge: false,
error: false,
cancelLoad: false,
progress: 0,
2018-07-29 20:25:22 +03:00
};
export default handleActions({
[GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }),
[GET_SHORT_URL_VISITS_ERROR]: () => ({ ...initialState, error: true }),
[GET_SHORT_URL_VISITS]: (state, { visits, shortCode, domain }) => ({
...initialState,
visits,
shortCode,
domain,
}),
[GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
[GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
[GET_SHORT_URL_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
2020-05-10 20:02:58 +03:00
[CREATE_VISIT]: (state, { shortUrl, visit }) => { // eslint-disable-line object-shorthand
const { shortCode, domain, visits } = state;
if (!shortUrlMatches(shortUrl, shortCode, domain)) {
return state;
}
return { ...state, visits: [ ...visits, visit ] };
},
}, initialState);
2018-07-29 20:25:22 +03:00
2020-05-10 20:02:58 +03:00
export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, query = {}) => (dispatch, getState) => {
const { getShortUrlVisits } = buildShlinkApiClient(getState);
2020-05-10 20:02:58 +03:00
const visitsLoader = (page, itemsPerPage) => getShortUrlVisits(shortCode, { ...query, page, itemsPerPage });
const extraFinishActionData = { shortCode, domain: query.domain };
const actionMap = {
start: GET_SHORT_URL_VISITS_START,
large: GET_SHORT_URL_VISITS_LARGE,
finish: GET_SHORT_URL_VISITS,
error: GET_SHORT_URL_VISITS_ERROR,
progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
};
2020-05-10 20:02:58 +03:00
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, getState);
2018-07-29 20:25:22 +03:00
};
export const cancelGetShortUrlVisits = createAction(GET_SHORT_URL_VISITS_CANCEL);