2023-07-24 21:14:59 +03:00
|
|
|
import { isBetween } from '../../../src/utils/helpers/date';
|
2023-07-24 18:30:58 +03:00
|
|
|
import type { ShlinkApiClient } from '../../api-contract';
|
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-26 21:04:50 +03:00
|
|
|
export const getNonOrphanVisits = (apiClientFactory: () => 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 }) => {
|
2023-07-26 21:04:50 +03:00
|
|
|
const { getNonOrphanVisits: shlinkGetNonOrphanVisits } = apiClientFactory();
|
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
|
|
|
});
|