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';
|
2022-11-12 10:49:14 +03:00
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
2022-04-24 19:36:25 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
|
|
|
import { GetState } from '../../container/types';
|
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
|
|
|
import { isBetween } from '../../utils/helpers/date';
|
|
|
|
import { getVisitsWithLoader, lastVisitLoaderForLoader } from './common';
|
2022-11-08 00:29:15 +03:00
|
|
|
import { createNewVisits, CreateVisitsAction } from './visitCreation';
|
2022-04-24 19:36:25 +03:00
|
|
|
import { domainMatches } from '../../short-urls/helpers';
|
2022-11-12 12:33:52 +03:00
|
|
|
import {
|
|
|
|
LoadVisits,
|
|
|
|
VisitsFallbackIntervalAction,
|
|
|
|
VisitsInfo,
|
|
|
|
VisitsLoaded,
|
|
|
|
VisitsLoadedAction,
|
|
|
|
VisitsLoadProgressChangedAction,
|
|
|
|
} from './types';
|
2022-04-24 19:36:25 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/domainVisits';
|
|
|
|
export const GET_DOMAIN_VISITS_START = `${REDUCER_PREFIX}/getDomainVisits/pending`;
|
|
|
|
export const GET_DOMAIN_VISITS_ERROR = `${REDUCER_PREFIX}/getDomainVisits/rejected`;
|
|
|
|
export const GET_DOMAIN_VISITS = `${REDUCER_PREFIX}/getDomainVisits/fulfilled`;
|
|
|
|
export const GET_DOMAIN_VISITS_LARGE = `${REDUCER_PREFIX}/getDomainVisits/large`;
|
|
|
|
export const GET_DOMAIN_VISITS_CANCEL = `${REDUCER_PREFIX}/getDomainVisits/cancel`;
|
|
|
|
export const GET_DOMAIN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getDomainVisits/progressChanged`;
|
|
|
|
export const GET_DOMAIN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getDomainVisits/fallbackToInterval`;
|
2022-04-24 19:36:25 +03:00
|
|
|
|
|
|
|
export const DEFAULT_DOMAIN = 'DEFAULT';
|
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
interface WithDomain {
|
2022-04-24 19:36:25 +03:00
|
|
|
domain: string;
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
export interface DomainVisits extends VisitsInfo, WithDomain {}
|
2022-11-12 11:18:37 +03:00
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
export interface LoadDomainVisits extends LoadVisits, WithDomain {}
|
|
|
|
|
|
|
|
type DomainVisitsAction = VisitsLoadedAction<WithDomain>;
|
2022-04-24 19:36:25 +03:00
|
|
|
|
|
|
|
type DomainVisitsCombinedAction = DomainVisitsAction
|
|
|
|
& VisitsLoadProgressChangedAction
|
|
|
|
& VisitsFallbackIntervalAction
|
|
|
|
& CreateVisitsAction
|
|
|
|
& ApiErrorAction;
|
|
|
|
|
|
|
|
const initialState: DomainVisits = {
|
|
|
|
visits: [],
|
|
|
|
domain: '',
|
|
|
|
loading: false,
|
|
|
|
loadingLarge: false,
|
|
|
|
error: false,
|
|
|
|
cancelLoad: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default buildReducer<DomainVisits, DomainVisitsCombinedAction>({
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/pending`]: () => ({ ...initialState, loading: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
2022-11-12 12:33:52 +03:00
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/fulfilled`]: (state, { payload }: DomainVisitsAction) => (
|
|
|
|
{ ...state, ...payload, loading: false, loadingLarge: false, error: false }
|
2022-04-24 19:36:25 +03:00
|
|
|
),
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/large`]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }),
|
2022-11-12 11:01:43 +03:00
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/progressChanged`]: (state, { payload: progress }) => ({ ...state, progress }),
|
|
|
|
[`${REDUCER_PREFIX}/getDomainVisits/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) => {
|
2022-04-24 19:36:25 +03:00
|
|
|
const { domain, visits, query = {} } = state;
|
|
|
|
const { startDate, endDate } = query;
|
2022-11-05 15:01:00 +03:00
|
|
|
const newVisits = payload.createdVisits
|
2022-04-24 19:36:25 +03:00
|
|
|
.filter(({ shortUrl, visit }) =>
|
|
|
|
shortUrl && domainMatches(shortUrl, domain) && isBetween(visit.date, startDate, endDate))
|
|
|
|
.map(({ visit }) => visit);
|
|
|
|
|
|
|
|
return { ...state, visits: [...newVisits, ...visits] };
|
|
|
|
},
|
|
|
|
}, initialState);
|
|
|
|
|
|
|
|
export const getDomainVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
2022-11-12 11:18:37 +03:00
|
|
|
{ domain, query = {}, doIntervalFallback = false }: LoadDomainVisits,
|
2022-04-24 19:36:25 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const { getDomainVisits: getVisits } = buildShlinkApiClient(getState);
|
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => getVisits(
|
|
|
|
domain,
|
|
|
|
{ ...query, page, itemsPerPage },
|
|
|
|
);
|
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, async (params) => getVisits(domain, params));
|
|
|
|
const shouldCancel = () => getState().domainVisits.cancelLoad;
|
2022-11-12 12:33:52 +03:00
|
|
|
const extraFinishActionData: Partial<VisitsLoaded<WithDomain>> = { domain, query };
|
2022-11-12 10:49:14 +03:00
|
|
|
const prefix = `${REDUCER_PREFIX}/getDomainVisits`;
|
2022-04-24 19:36:25 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel);
|
2022-04-24 19:36:25 +03:00
|
|
|
};
|
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
export const cancelGetDomainVisits = createAction<void>(`${REDUCER_PREFIX}/getDomainVisits/cancel`);
|