2019-03-17 11:36:07 +03:00
|
|
|
import { createAction, handleActions } from 'redux-actions';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-04-18 11:50:01 +03:00
|
|
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
2020-05-10 18:49:55 +03:00
|
|
|
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
|
|
|
|
2019-03-17 11:36:07 +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';
|
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';
|
2019-03-17 11:36:07 +03:00
|
|
|
/* eslint-enable padding-line-between-statements */
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2020-05-10 18:49:55 +03:00
|
|
|
export const shortUrlVisitsType = PropTypes.shape({ // TODO Should extend from VisitInfoType
|
|
|
|
visits: PropTypes.arrayOf(VisitType),
|
2020-04-18 11:50:01 +03:00
|
|
|
shortCode: PropTypes.string,
|
|
|
|
domain: PropTypes.string,
|
2018-08-26 00:39:27 +03:00
|
|
|
loading: PropTypes.bool,
|
2020-05-10 20:02:58 +03:00
|
|
|
loadingLarge: PropTypes.bool,
|
2018-08-26 00:39:27 +03:00
|
|
|
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: [],
|
2020-04-18 11:50:01 +03:00
|
|
|
shortCode: '',
|
|
|
|
domain: undefined,
|
2018-07-29 20:25:22 +03:00
|
|
|
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-17 11:36:07 +03:00
|
|
|
export default handleActions({
|
2020-05-11 19:55:35 +03:00
|
|
|
[GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }),
|
|
|
|
[GET_SHORT_URL_VISITS_ERROR]: () => ({ ...initialState, error: true }),
|
2020-04-18 11:50:01 +03:00
|
|
|
[GET_SHORT_URL_VISITS]: (state, { visits, shortCode, domain }) => ({
|
2020-05-11 19:55:35 +03:00
|
|
|
...initialState,
|
2019-03-17 11:36:07 +03:00
|
|
|
visits,
|
2020-04-18 11:50:01 +03:00
|
|
|
shortCode,
|
|
|
|
domain,
|
2019-03-17 11:36:07 +03:00
|
|
|
}),
|
|
|
|
[GET_SHORT_URL_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[GET_SHORT_URL_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
2020-05-10 20:02:58 +03:00
|
|
|
[CREATE_VISIT]: (state, { shortUrl, visit }) => { // eslint-disable-line object-shorthand
|
2020-04-18 11:50:01 +03:00
|
|
|
const { shortCode, domain, visits } = state;
|
2020-04-17 18:11:52 +03:00
|
|
|
|
2020-04-18 11:50:01 +03:00
|
|
|
if (!shortUrlMatches(shortUrl, shortCode, domain)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...state, visits: [ ...visits, visit ] };
|
|
|
|
},
|
2019-03-17 11:36:07 +03:00
|
|
|
}, 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) => {
|
2020-03-05 11:23:53 +03:00
|
|
|
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,
|
2019-01-10 21:50:25 +03:00
|
|
|
};
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2020-05-10 20:02:58 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, getState);
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2019-03-08 21:40:43 +03:00
|
|
|
|
2019-03-17 11:36:07 +03:00
|
|
|
export const cancelGetShortUrlVisits = createAction(GET_SHORT_URL_VISITS_CANCEL);
|