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-02-05 15:53:07 +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-11-12 12:33:52 +03:00
|
|
|
import {
|
|
|
|
LoadVisits,
|
|
|
|
VisitsFallbackIntervalAction,
|
|
|
|
VisitsInfo,
|
|
|
|
VisitsLoaded,
|
|
|
|
VisitsLoadedAction,
|
|
|
|
VisitsLoadProgressChangedAction,
|
|
|
|
} from './types';
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/orphanVisits';
|
|
|
|
export const GET_NON_ORPHAN_VISITS_START = `${REDUCER_PREFIX}/getNonOrphanVisits/pending`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS_ERROR = `${REDUCER_PREFIX}/getNonOrphanVisits/rejected`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS = `${REDUCER_PREFIX}/getNonOrphanVisits/fulfilled`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS_LARGE = `${REDUCER_PREFIX}/getNonOrphanVisits/large`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS_CANCEL = `${REDUCER_PREFIX}/getNonOrphanVisits/cancel`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS_PROGRESS_CHANGED = `${REDUCER_PREFIX}/getNonOrphanVisits/progressChanged`;
|
|
|
|
export const GET_NON_ORPHAN_VISITS_FALLBACK_TO_INTERVAL = `${REDUCER_PREFIX}/getNonOrphanVisits/fallbackToInterval`;
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 12:33:52 +03:00
|
|
|
type NonOrphanVisitsCombinedAction = VisitsLoadedAction
|
2022-02-05 15:53:07 +03:00
|
|
|
& VisitsLoadProgressChangedAction
|
|
|
|
& VisitsFallbackIntervalAction
|
|
|
|
& CreateVisitsAction
|
|
|
|
& ApiErrorAction;
|
|
|
|
|
|
|
|
const initialState: VisitsInfo = {
|
|
|
|
visits: [],
|
|
|
|
loading: false,
|
|
|
|
loadingLarge: false,
|
|
|
|
error: false,
|
|
|
|
cancelLoad: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default buildReducer<VisitsInfo, NonOrphanVisitsCombinedAction>({
|
2022-11-12 10:49:14 +03:00
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/pending`]: () => ({ ...initialState, loading: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/rejected`]: (_, { errorData }) => (
|
|
|
|
{ ...initialState, error: true, errorData }
|
|
|
|
),
|
2022-11-12 12:33:52 +03:00
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/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}/getNonOrphanVisits/large`]: (state) => ({ ...state, loadingLarge: true }),
|
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/cancel`]: (state) => ({ ...state, cancelLoad: true }),
|
2022-11-12 11:01:43 +03:00
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/progressChanged`]: (state, { payload: progress }) => ({ ...state, progress }),
|
|
|
|
[`${REDUCER_PREFIX}/getNonOrphanVisits/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-02-05 15:53:07 +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 }) => isBetween(visit.date, startDate, endDate))
|
|
|
|
.map(({ visit }) => visit);
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return { ...state, visits: [...newVisits, ...visits] };
|
2022-02-05 15:53:07 +03:00
|
|
|
},
|
|
|
|
}, initialState);
|
|
|
|
|
|
|
|
export const getNonOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
2022-11-12 11:21:23 +03:00
|
|
|
{ query = {}, doIntervalFallback = false }: LoadVisits,
|
2022-02-05 15:53:07 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const { getNonOrphanVisits: shlinkGetNonOrphanVisits } = buildShlinkApiClient(getState);
|
2022-02-05 15:53:07 +03:00
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) =>
|
2022-03-26 14:17:42 +03:00
|
|
|
shlinkGetNonOrphanVisits({ ...query, page, itemsPerPage });
|
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, shlinkGetNonOrphanVisits);
|
2022-02-05 15:53:07 +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}/getNonOrphanVisits`;
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
return getVisitsWithLoader(visitsLoader, lastVisitLoader, extraFinishActionData, prefix, dispatch, shouldCancel);
|
2022-02-05 15:53:07 +03:00
|
|
|
};
|
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
export const cancelGetNonOrphanVisits = createAction<void>(`${REDUCER_PREFIX}/getNonOrphanVisits/cancel`);
|