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';
|
|
|
|
import { OrphanVisit, OrphanVisitType } from '../types';
|
2022-11-12 10:49:14 +03:00
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
2021-02-27 22:03:51 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
|
|
|
import { GetState } from '../../container/types';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { isOrphanVisit } from '../types/helpers';
|
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';
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/orphanVisits';
|
|
|
|
export const GET_ORPHAN_VISITS_START = `${REDUCER_PREFIX}/getOrphanVisits/pending`;
|
|
|
|
export const GET_ORPHAN_VISITS_ERROR = `${REDUCER_PREFIX}/getOrphanVisits/rejected`;
|
|
|
|
export const GET_ORPHAN_VISITS = `${REDUCER_PREFIX}/getOrphanVisits/fulfilled`;
|
|
|
|
export const GET_ORPHAN_VISITS_LARGE = `${REDUCER_PREFIX}/getOrphanVisits/large`;
|
|
|
|
export const GET_ORPHAN_VISITS_CANCEL = `${REDUCER_PREFIX}/getOrphanVisits/cancel`;
|
|
|
|
export const GET_ORPHAN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getOrphanVisits/progressChanged`;
|
|
|
|
export const GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getOrphanVisits/fallbackToInterval`;
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 11:32:52 +03:00
|
|
|
export interface LoadOrphanVisits extends LoadVisits {
|
|
|
|
orphanVisitsType?: OrphanVisitType;
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
type OrphanVisitsCombinedAction = VisitsLoadedAction
|
2021-02-27 22:03:51 +03:00
|
|
|
& VisitsLoadProgressChangedAction
|
2021-12-23 12:38:02 +03:00
|
|
|
& VisitsFallbackIntervalAction
|
2021-02-27 22:03:51 +03:00
|
|
|
& CreateVisitsAction
|
2021-08-21 18:53:06 +03:00
|
|
|
& ApiErrorAction;
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
const initialState: VisitsInfo = {
|
|
|
|
visits: [],
|
|
|
|
loading: false,
|
|
|
|
loadingLarge: false,
|
|
|
|
error: false,
|
|
|
|
cancelLoad: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default buildReducer<VisitsInfo, OrphanVisitsCombinedAction>({
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/pending`]: () => ({ ...initialState, loading: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/rejected`]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
2022-11-12 12:33:52 +03:00
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/fulfilled`]: (state, { payload }: VisitsLoadedAction) => (
|
|
|
|
{ ...state, ...payload, loading: false, loadingLarge: false, error: false }
|
2022-09-13 16:56:53 +03:00
|
|
|
),
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/large`]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }),
|
2022-11-12 11:01:43 +03:00
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/progressChanged`]: (state, { payload: progress }) => ({ ...state, progress }),
|
|
|
|
[`${REDUCER_PREFIX}/getOrphanVisits/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 { visits, query = {} } = state;
|
|
|
|
const { startDate, endDate } = query;
|
2022-11-05 15:01:00 +03:00
|
|
|
const newVisits = payload.createdVisits
|
2022-02-05 15:53:07 +03:00
|
|
|
.filter(({ visit, shortUrl }) => !shortUrl && isBetween(visit.date, startDate, endDate))
|
2021-10-24 11:31:32 +03:00
|
|
|
.map(({ visit }) => visit);
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return { ...state, visits: [...newVisits, ...visits] };
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
|
|
|
}, initialState);
|
|
|
|
|
2021-06-30 04:23:45 +03:00
|
|
|
const matchesType = (visit: OrphanVisit, orphanVisitsType?: OrphanVisitType) =>
|
|
|
|
!orphanVisitsType || orphanVisitsType === visit.type;
|
|
|
|
|
|
|
|
export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
2022-11-12 11:32:52 +03:00
|
|
|
{ orphanVisitsType, query = {}, doIntervalFallback = false }: LoadOrphanVisits,
|
2021-06-30 04:23:45 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const { getOrphanVisits: getVisits } = buildShlinkApiClient(getState);
|
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => getVisits({ ...query, page, itemsPerPage })
|
2021-06-30 04:23:45 +03:00
|
|
|
.then((result) => {
|
|
|
|
const visits = result.data.filter((visit) => isOrphanVisit(visit) && matchesType(visit, orphanVisitsType));
|
|
|
|
|
|
|
|
return { ...result, data: visits };
|
|
|
|
});
|
2022-03-26 14:17:42 +03:00
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, getVisits);
|
2021-02-27 22:03:51 +03:00
|
|
|
const shouldCancel = () => getState().orphanVisits.cancelLoad;
|
2022-11-12 12:33:52 +03:00
|
|
|
const extraFinishActionData: Partial<VisitsLoaded> = { query };
|
2022-11-12 10:49:14 +03:00
|
|
|
const prefix = `${REDUCER_PREFIX}/getOrphanVisits`;
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel);
|
2021-02-27 22:03:51 +03:00
|
|
|
};
|
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
export const cancelGetOrphanVisits = createAction<void>(`${REDUCER_PREFIX}/getOrphanVisits/cancel`);
|