2020-08-28 19:33:37 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2020-04-18 11:50:01 +03:00
|
|
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
2020-12-22 01:41:50 +03:00
|
|
|
import { Visit, VisitsInfo, VisitsLoadFailedAction, VisitsLoadProgressChangedAction } from '../types';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { ShortUrlIdentifier } from '../../short-urls/data';
|
|
|
|
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { GetState } from '../../container/types';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { ShlinkVisitsParams } from '../../api/types';
|
2020-05-10 20:02:58 +03:00
|
|
|
import { getVisitsWithLoader } from './common';
|
2020-09-12 12:31:44 +03:00
|
|
|
import { CREATE_VISITS, CreateVisitsAction } 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';
|
2020-05-11 20:32:42 +03:00
|
|
|
export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_PROGRESS_CHANGED';
|
2019-03-17 11:36:07 +03:00
|
|
|
/* eslint-enable padding-line-between-statements */
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
|
|
|
|
|
|
|
|
interface ShortUrlVisitsAction extends Action<string>, ShortUrlIdentifier {
|
|
|
|
visits: Visit[];
|
|
|
|
}
|
|
|
|
|
2020-12-22 01:41:50 +03:00
|
|
|
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction
|
|
|
|
& VisitsLoadProgressChangedAction
|
|
|
|
& CreateVisitsAction
|
|
|
|
& VisitsLoadFailedAction;
|
2020-08-28 19:33:37 +03:00
|
|
|
|
|
|
|
const initialState: ShortUrlVisits = {
|
2018-07-29 20:25:22 +03:00
|
|
|
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,
|
2020-05-11 20:32:42 +03:00
|
|
|
progress: 0,
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export default buildReducer<ShortUrlVisits, ShortUrlVisitsCombinedAction>({
|
2020-05-11 19:55:35 +03:00
|
|
|
[GET_SHORT_URL_VISITS_START]: () => ({ ...initialState, loading: true }),
|
2020-12-22 01:41:50 +03:00
|
|
|
[GET_SHORT_URL_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
2020-08-28 19:33:37 +03:00
|
|
|
[GET_SHORT_URL_VISITS]: (_, { 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-11 20:32:42 +03:00
|
|
|
[GET_SHORT_URL_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
2021-02-27 22:03:51 +03:00
|
|
|
[CREATE_VISITS]: (state, { createdVisits }) => {
|
2020-04-18 11:50:01 +03:00
|
|
|
const { shortCode, domain, visits } = state;
|
2020-09-12 12:31:44 +03:00
|
|
|
const newVisits = createdVisits
|
2021-02-27 22:03:51 +03:00
|
|
|
.filter(({ shortUrl }) => shortUrl && shortUrlMatches(shortUrl, shortCode, domain))
|
2020-09-12 12:31:44 +03:00
|
|
|
.map(({ visit }) => visit);
|
2020-04-18 11:50:01 +03:00
|
|
|
|
2021-05-01 17:39:13 +03:00
|
|
|
return { ...state, visits: [ ...newVisits, ...visits ] };
|
2020-04-18 11:50:01 +03:00
|
|
|
},
|
2019-03-17 11:36:07 +03:00
|
|
|
}, initialState);
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
|
|
|
shortCode: string,
|
2021-06-30 04:23:45 +03:00
|
|
|
query: ShlinkVisitsParams = {},
|
2020-08-28 19:33:37 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2020-03-05 11:23:53 +03:00
|
|
|
const { getShortUrlVisits } = buildShlinkApiClient(getState);
|
2020-09-02 21:27:50 +03:00
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => getShortUrlVisits(
|
2020-08-28 19:33:37 +03:00
|
|
|
shortCode,
|
|
|
|
{ ...query, page, itemsPerPage },
|
|
|
|
);
|
2020-09-19 11:50:49 +03:00
|
|
|
const shouldCancel = () => getState().shortUrlVisits.cancelLoad;
|
2020-08-28 19:33:37 +03:00
|
|
|
const extraFinishActionData: Partial<ShortUrlVisitsAction> = { shortCode, domain: query.domain };
|
2020-05-10 20:02:58 +03:00
|
|
|
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,
|
2020-05-11 20:32:42 +03:00
|
|
|
progress: GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
2019-01-10 21:50:25 +03:00
|
|
|
};
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2020-09-19 11:50:49 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2019-03-08 21:40:43 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export const cancelGetShortUrlVisits = buildActionCreator(GET_SHORT_URL_VISITS_CANCEL);
|