2023-07-24 10:48:41 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../api/services/ShlinkApiClient';
|
2023-07-16 09:47:10 +03:00
|
|
|
import { isBetween } from '../../../utils/helpers/date';
|
2022-11-12 22:37:04 +03:00
|
|
|
import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { VisitsInfo } from './types';
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/orphanVisits';
|
2022-02-05 15:53:07 +03:00
|
|
|
|
|
|
|
const initialState: VisitsInfo = {
|
|
|
|
visits: [],
|
|
|
|
loading: false,
|
|
|
|
loadingLarge: false,
|
|
|
|
error: false,
|
|
|
|
cancelLoad: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
|
2023-07-24 10:48:41 +03:00
|
|
|
export const getNonOrphanVisits = (apiClient: ShlinkApiClient) => createVisitsAsyncThunk({
|
2022-11-13 11:59:49 +03:00
|
|
|
typePrefix: `${REDUCER_PREFIX}/getNonOrphanVisits`,
|
2023-07-24 10:48:41 +03:00
|
|
|
createLoaders: ({ query = {}, doIntervalFallback = false }) => {
|
|
|
|
const { getNonOrphanVisits: shlinkGetNonOrphanVisits } = apiClient;
|
2022-11-12 20:18:16 +03:00
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) =>
|
|
|
|
shlinkGetNonOrphanVisits({ ...query, page, itemsPerPage });
|
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, shlinkGetNonOrphanVisits);
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 20:18:16 +03:00
|
|
|
return [visitsLoader, lastVisitLoader];
|
2022-02-05 15:53:07 +03:00
|
|
|
},
|
2022-11-12 20:18:16 +03:00
|
|
|
getExtraFulfilledPayload: ({ query = {} }) => ({ query }),
|
|
|
|
shouldCancel: (getState) => getState().orphanVisits.cancelLoad,
|
|
|
|
});
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 20:18:16 +03:00
|
|
|
export const nonOrphanVisitsReducerCreator = (
|
2022-11-13 11:59:49 +03:00
|
|
|
asyncThunkCreator: ReturnType<typeof getNonOrphanVisits>,
|
|
|
|
) => createVisitsReducer({
|
|
|
|
name: REDUCER_PREFIX,
|
2022-11-12 22:37:04 +03:00
|
|
|
initialState,
|
2022-11-13 11:59:49 +03:00
|
|
|
asyncThunkCreator,
|
|
|
|
filterCreatedVisits: ({ query = {} }, createdVisits) => {
|
2022-11-12 22:37:04 +03:00
|
|
|
const { startDate, endDate } = query;
|
|
|
|
return createdVisits.filter(({ visit }) => isBetween(visit.date, startDate, endDate));
|
|
|
|
},
|
2022-11-13 11:59:49 +03:00
|
|
|
});
|