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';
|
2021-12-23 12:38:02 +03:00
|
|
|
import { Visit, VisitsFallbackIntervalAction, VisitsInfo, 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';
|
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';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
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';
|
2021-12-23 12:38:02 +03:00
|
|
|
export const GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL';
|
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[];
|
2021-10-24 11:31:32 +03:00
|
|
|
query?: ShlinkVisitsParams;
|
2020-08-28 19:33:37 +03:00
|
|
|
}
|
|
|
|
|
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>({
|
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 }),
|
2021-12-23 12:38:02 +03:00
|
|
|
[GET_SHORT_URL_VISITS]: (state, { visits, query, shortCode, domain }) => ({
|
|
|
|
...state,
|
2019-03-17 11:36:07 +03:00
|
|
|
visits,
|
2020-04-18 11:50:01 +03:00
|
|
|
shortCode,
|
|
|
|
domain,
|
2021-10-24 11:31:32 +03:00
|
|
|
query,
|
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
|
|
|
}),
|
|
|
|
[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-12-23 12:38:02 +03:00
|
|
|
[GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL]: (state, { fallbackInterval }) => ({ ...state, fallbackInterval }),
|
2022-11-08 00:29:15 +03:00
|
|
|
[createNewVisits.toString()]: (state, { payload }) => {
|
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) => (
|
|
|
|
shortCode: string,
|
2021-06-30 04:23:45 +03:00
|
|
|
query: ShlinkVisitsParams = {},
|
2021-12-23 12:51:13 +03:00
|
|
|
doIntervalFallback = false,
|
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;
|
2021-10-24 11:31:32 +03:00
|
|
|
const extraFinishActionData: Partial<ShortUrlVisitsAction> = { shortCode, query, 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,
|
2021-12-23 12:38:02 +03:00
|
|
|
fallbackToInterval: GET_SHORT_URL_VISITS_FALLBACK_TO_INTERVAL,
|
2019-01-10 21:50:25 +03:00
|
|
|
};
|
2018-12-18 12:14:25 +03:00
|
|
|
|
2021-12-23 12:38:02 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, 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);
|