2023-07-16 09:47:10 +03:00
|
|
|
import { isBetween } from '../../../utils/helpers/date';
|
2023-07-24 18:30:58 +03:00
|
|
|
import type { ShlinkApiClient } from '../../api-contract';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlIdentifier } from '../../short-urls/data';
|
|
|
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
2022-11-12 22:37:04 +03:00
|
|
|
import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { LoadVisits, VisitsInfo } from './types';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/shortUrlVisits';
|
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 11:38:24 +03:00
|
|
|
export interface LoadShortUrlVisits extends LoadVisits {
|
|
|
|
shortCode: string;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-07-24 10:48:41 +03:00
|
|
|
export const getShortUrlVisits = (apiClient: ShlinkApiClient) => createVisitsAsyncThunk({
|
2022-11-13 11:59:49 +03:00
|
|
|
typePrefix: `${REDUCER_PREFIX}/getShortUrlVisits`,
|
2023-07-24 10:48:41 +03:00
|
|
|
createLoaders: ({ shortCode, query = {}, doIntervalFallback = false }: LoadShortUrlVisits) => {
|
|
|
|
const { getShortUrlVisits: shlinkGetShortUrlVisits } = apiClient;
|
2022-11-12 21:36:12 +03:00
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => shlinkGetShortUrlVisits(
|
|
|
|
shortCode,
|
|
|
|
{ ...query, page, itemsPerPage },
|
|
|
|
);
|
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(
|
|
|
|
doIntervalFallback,
|
|
|
|
async (params) => shlinkGetShortUrlVisits(shortCode, { ...params, domain: query.domain }),
|
|
|
|
);
|
2020-04-18 11:50:01 +03:00
|
|
|
|
2022-11-12 21:36:12 +03:00
|
|
|
return [visitsLoader, lastVisitLoader];
|
2020-04-18 11:50:01 +03:00
|
|
|
},
|
2022-11-12 21:36:12 +03:00
|
|
|
getExtraFulfilledPayload: ({ shortCode, query = {} }: LoadShortUrlVisits) => (
|
|
|
|
{ shortCode, query, domain: query.domain }
|
|
|
|
),
|
|
|
|
shouldCancel: (getState) => getState().shortUrlVisits.cancelLoad,
|
|
|
|
});
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2022-11-12 21:36:12 +03:00
|
|
|
export const shortUrlVisitsReducerCreator = (
|
2022-11-13 11:59:49 +03:00
|
|
|
asyncThunkCreator: ReturnType<typeof getShortUrlVisits>,
|
|
|
|
) => createVisitsReducer({
|
|
|
|
name: REDUCER_PREFIX,
|
2022-11-12 22:37:04 +03:00
|
|
|
initialState,
|
2022-11-13 11:59:49 +03:00
|
|
|
// @ts-expect-error TODO Fix type inference
|
|
|
|
asyncThunkCreator,
|
|
|
|
filterCreatedVisits: ({ shortCode, domain, query = {} }: ShortUrlVisits, createdVisits) => {
|
2022-11-12 22:37:04 +03:00
|
|
|
const { startDate, endDate } = query;
|
|
|
|
return createdVisits.filter(
|
|
|
|
({ shortUrl, visit }) =>
|
|
|
|
shortUrl && shortUrlMatches(shortUrl, shortCode, domain) && isBetween(visit.date, startDate, endDate),
|
|
|
|
);
|
|
|
|
},
|
2022-11-13 11:59:49 +03:00
|
|
|
});
|