2022-11-12 10:49:14 +03:00
|
|
|
import { createAction } from '@reduxjs/toolkit';
|
2022-11-12 12:33:52 +03:00
|
|
|
import { Dispatch } from 'redux';
|
2020-04-18 11:50:01 +03:00
|
|
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { ShortUrlIdentifier } from '../../short-urls/data';
|
2022-11-12 10:49:14 +03:00
|
|
|
import { 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-08-21 18:53:06 +03:00
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
2021-10-24 11:31:32 +03:00
|
|
|
import { isBetween } from '../../utils/helpers/date';
|
2021-12-23 12:38:02 +03:00
|
|
|
import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common';
|
2022-11-08 00:29:15 +03:00
|
|
|
import { createNewVisits, CreateVisitsAction } from './visitCreation';
|
2022-11-12 12:33:52 +03:00
|
|
|
import {
|
|
|
|
LoadVisits,
|
|
|
|
VisitsFallbackIntervalAction,
|
|
|
|
VisitsInfo,
|
|
|
|
VisitsLoaded,
|
|
|
|
VisitsLoadedAction,
|
|
|
|
VisitsLoadProgressChangedAction,
|
|
|
|
} from './types';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/shortUrlVisits';
|
|
|
|
export const GET_SHORT_URL_VISITS_START = `${REDUCER_PREFIX}/getShortUrlVisits/pending`;
|
|
|
|
export const GET_SHORT_URL_VISITS_ERROR = `${REDUCER_PREFIX}/getShortUrlVisits/rejected`;
|
|
|
|
export const GET_SHORT_URL_VISITS = `${REDUCER_PREFIX}/getShortUrlVisits/fulfilled`;
|
|
|
|
export const GET_SHORT_URL_VISITS_LARGE = `${REDUCER_PREFIX}/getShortUrlVisits/large`;
|
|
|
|
export const GET_SHORT_URL_VISITS_CANCEL = `${REDUCER_PREFIX}/getShortUrlVisits/cancel`;
|
|
|
|
export const GET_SHORT_URL_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getShortUrlVisits/progressChanged`;
|
|
|
|
export const GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getShortUrlVisits/fallbackToInterval`;
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
|
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
type ShortUrlVisitsAction = VisitsLoadedAction<ShortUrlIdentifier>;
|
2020-08-28 19:33:37 +03:00
|
|
|
|
2022-11-12 11:38:24 +03:00
|
|
|
export interface LoadShortUrlVisits extends LoadVisits {
|
|
|
|
shortCode: string;
|
|
|
|
}
|
|
|
|
|
2020-12-22 01:41:50 +03:00
|
|
|
type ShortUrlVisitsCombinedAction = ShortUrlVisitsAction
|
|
|
|
& VisitsLoadProgressChangedAction
|
2021-12-23 12:38:02 +03:00
|
|
|
& VisitsFallbackIntervalAction
|
2020-12-22 01:41:50 +03:00
|
|
|
& CreateVisitsAction
|
2021-08-21 18:53:06 +03:00
|
|
|
& ApiErrorAction;
|
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: '',
|
2021-10-24 11:31:32 +03:00
|
|
|
domain: undefined, // Deprecated. Value from query params can be used instead
|
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>({
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/pending`]: () => ({ ...initialState, loading: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
2022-11-12 12:33:52 +03:00
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/fulfilled`]: (state, { payload }: ShortUrlVisitsAction) => ({
|
2021-12-23 12:38:02 +03:00
|
|
|
...state,
|
2022-11-12 12:33:52 +03:00
|
|
|
...payload,
|
2021-12-23 12:38:02 +03:00
|
|
|
loading: false,
|
2022-09-13 16:56:53 +03:00
|
|
|
loadingLarge: false,
|
2021-12-23 12:38:02 +03:00
|
|
|
error: false,
|
2019-03-17 11:36:07 +03:00
|
|
|
}),
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/large`]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }),
|
2022-11-12 11:01:43 +03:00
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/progressChanged`]: (state, { payload: progress }) => ({ ...state, progress }),
|
|
|
|
[`${REDUCER_PREFIX}/getShortUrlVisits/fallbackToInterval`]: (state, { payload: fallbackInterval }) => (
|
2022-11-12 10:49:14 +03:00
|
|
|
{ ...state, fallbackInterval }
|
|
|
|
),
|
2022-11-12 11:01:43 +03:00
|
|
|
[createNewVisits.toString()]: (state, { payload }: CreateVisitsAction) => {
|
2021-10-24 11:31:32 +03:00
|
|
|
const { shortCode, domain, visits, query = {} } = state;
|
|
|
|
const { startDate, endDate } = query;
|
2022-11-05 15:01:00 +03:00
|
|
|
const newVisits = payload.createdVisits
|
2021-10-24 11:31:32 +03:00
|
|
|
.filter(
|
|
|
|
({ shortUrl, visit }) =>
|
|
|
|
shortUrl && shortUrlMatches(shortUrl, shortCode, domain) && isBetween(visit.date, startDate, endDate),
|
|
|
|
)
|
2020-09-12 12:31:44 +03:00
|
|
|
.map(({ visit }) => visit);
|
2020-04-18 11:50:01 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return newVisits.length === 0 ? state : { ...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) => (
|
2022-11-12 11:38:24 +03:00
|
|
|
{ shortCode, query = {}, doIntervalFallback = false }: LoadShortUrlVisits,
|
2020-08-28 19:33:37 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const { getShortUrlVisits: shlinkGetShortUrlVisits } = buildShlinkApiClient(getState);
|
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => shlinkGetShortUrlVisits(
|
2020-08-28 19:33:37 +03:00
|
|
|
shortCode,
|
|
|
|
{ ...query, page, itemsPerPage },
|
|
|
|
);
|
2021-12-23 12:38:02 +03:00
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(
|
2021-12-23 12:51:13 +03:00
|
|
|
doIntervalFallback,
|
2022-03-26 14:17:42 +03:00
|
|
|
async (params) => shlinkGetShortUrlVisits(shortCode, { ...params, domain: query.domain }),
|
2021-12-23 12:38:02 +03:00
|
|
|
);
|
2020-09-19 11:50:49 +03:00
|
|
|
const shouldCancel = () => getState().shortUrlVisits.cancelLoad;
|
2022-11-12 12:33:52 +03:00
|
|
|
const extraFinishActionData: Partial<VisitsLoaded<ShortUrlIdentifier>> = { shortCode, query, domain: query.domain };
|
2022-11-12 10:49:14 +03:00
|
|
|
const prefix = `${REDUCER_PREFIX}/getShortUrlVisits`;
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel);
|
2018-07-29 20:25:22 +03:00
|
|
|
};
|
2019-03-08 21:40:43 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
export const cancelGetShortUrlVisits = createAction<void>(`${REDUCER_PREFIX}/getShortUrlVisits/cancel`);
|