2021-02-27 22:03:51 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2021-12-23 12:38:02 +03:00
|
|
|
import {
|
|
|
|
OrphanVisit,
|
|
|
|
OrphanVisitType,
|
|
|
|
Visit,
|
|
|
|
VisitsFallbackIntervalAction,
|
|
|
|
VisitsInfo,
|
|
|
|
VisitsLoadProgressChangedAction,
|
|
|
|
} from '../types';
|
2021-02-27 22:03:51 +03:00
|
|
|
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
|
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
|
|
|
import { GetState } from '../../container/types';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { ShlinkVisitsParams } from '../../api/types';
|
|
|
|
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';
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
export const GET_ORPHAN_VISITS_START = 'shlink/orphanVisits/GET_ORPHAN_VISITS_START';
|
|
|
|
export const GET_ORPHAN_VISITS_ERROR = 'shlink/orphanVisits/GET_ORPHAN_VISITS_ERROR';
|
|
|
|
export const GET_ORPHAN_VISITS = 'shlink/orphanVisits/GET_ORPHAN_VISITS';
|
|
|
|
export const GET_ORPHAN_VISITS_LARGE = 'shlink/orphanVisits/GET_ORPHAN_VISITS_LARGE';
|
|
|
|
export const GET_ORPHAN_VISITS_CANCEL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_CANCEL';
|
|
|
|
export const GET_ORPHAN_VISITS_PROGRESS_CHANGED = 'shlink/orphanVisits/GET_ORPHAN_VISITS_PROGRESS_CHANGED';
|
2021-12-23 12:38:02 +03:00
|
|
|
export const GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = 'shlink/orphanVisits/GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL';
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
export interface OrphanVisitsAction extends Action<string> {
|
|
|
|
visits: Visit[];
|
2021-10-24 11:31:32 +03:00
|
|
|
query?: ShlinkVisitsParams;
|
2021-02-27 22:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type OrphanVisitsCombinedAction = OrphanVisitsAction
|
|
|
|
& 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>({
|
|
|
|
[GET_ORPHAN_VISITS_START]: () => ({ ...initialState, loading: true }),
|
|
|
|
[GET_ORPHAN_VISITS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
2022-09-13 16:56:53 +03:00
|
|
|
[GET_ORPHAN_VISITS]: (state, { visits, query }) => (
|
|
|
|
{ ...state, visits, query, loading: false, loadingLarge: false, error: false }
|
|
|
|
),
|
2021-02-27 22:03:51 +03:00
|
|
|
[GET_ORPHAN_VISITS_LARGE]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[GET_ORPHAN_VISITS_CANCEL]: (state) => ({ ...state, cancelLoad: true }),
|
|
|
|
[GET_ORPHAN_VISITS_PROGRESS_CHANGED]: (state, { progress }) => ({ ...state, progress }),
|
2021-12-23 12:38:02 +03:00
|
|
|
[GET_ORPHAN_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 { 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) => (
|
|
|
|
query: ShlinkVisitsParams = {},
|
|
|
|
orphanVisitsType?: OrphanVisitType,
|
2021-12-23 12:51:13 +03:00
|
|
|
doIntervalFallback = false,
|
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;
|
2021-10-24 11:31:32 +03:00
|
|
|
const extraFinishActionData: Partial<OrphanVisitsAction> = { query };
|
2021-02-27 22:03:51 +03:00
|
|
|
const actionMap = {
|
|
|
|
start: GET_ORPHAN_VISITS_START,
|
|
|
|
large: GET_ORPHAN_VISITS_LARGE,
|
|
|
|
finish: GET_ORPHAN_VISITS,
|
|
|
|
error: GET_ORPHAN_VISITS_ERROR,
|
|
|
|
progress: GET_ORPHAN_VISITS_PROGRESS_CHANGED,
|
2021-12-23 12:38:02 +03:00
|
|
|
fallbackToInterval: GET_ORPHAN_VISITS_FALLBACK_TO_INTERVAL,
|
2021-02-27 22:03:51 +03:00
|
|
|
};
|
|
|
|
|
2021-12-23 12:38:02 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, actionMap, dispatch, shouldCancel);
|
2021-02-27 22:03:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const cancelGetOrphanVisits = buildActionCreator(GET_ORPHAN_VISITS_CANCEL);
|