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';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { OrphanVisit, OrphanVisitType } from '../types';
|
|
|
|
import { isOrphanVisit } from '../types/helpers';
|
2022-11-12 22:37:04 +03:00
|
|
|
import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { LoadVisits, VisitsInfo } from './types';
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/orphanVisits';
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 11:32:52 +03:00
|
|
|
export interface LoadOrphanVisits extends LoadVisits {
|
|
|
|
orphanVisitsType?: OrphanVisitType;
|
|
|
|
}
|
|
|
|
|
2021-02-27 22:03:51 +03:00
|
|
|
const initialState: VisitsInfo = {
|
|
|
|
visits: [],
|
|
|
|
loading: false,
|
|
|
|
loadingLarge: false,
|
|
|
|
error: false,
|
|
|
|
cancelLoad: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
|
2022-11-12 21:15:41 +03:00
|
|
|
const matchesType = (visit: OrphanVisit, orphanVisitsType?: OrphanVisitType) =>
|
|
|
|
!orphanVisitsType || orphanVisitsType === visit.type;
|
|
|
|
|
2023-07-26 21:04:50 +03:00
|
|
|
export const getOrphanVisits = (apiClientFactory: () => ShlinkApiClient) => createVisitsAsyncThunk({
|
2022-11-13 11:59:49 +03:00
|
|
|
typePrefix: `${REDUCER_PREFIX}/getOrphanVisits`,
|
2023-07-24 10:48:41 +03:00
|
|
|
createLoaders: ({ orphanVisitsType, query = {}, doIntervalFallback = false }: LoadOrphanVisits) => {
|
2023-07-26 21:04:50 +03:00
|
|
|
const { getOrphanVisits: getVisits } = apiClientFactory();
|
2022-11-12 21:15:41 +03:00
|
|
|
const visitsLoader = async (page: number, itemsPerPage: number) => getVisits({ ...query, page, itemsPerPage })
|
|
|
|
.then((result) => {
|
|
|
|
const visits = result.data.filter((visit) => isOrphanVisit(visit) && matchesType(visit, orphanVisitsType));
|
|
|
|
return { ...result, data: visits };
|
|
|
|
});
|
|
|
|
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, getVisits);
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 21:15:41 +03:00
|
|
|
return [visitsLoader, lastVisitLoader];
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
2022-11-12 21:15:41 +03:00
|
|
|
getExtraFulfilledPayload: ({ query = {} }: LoadOrphanVisits) => ({ query }),
|
|
|
|
shouldCancel: (getState) => getState().orphanVisits.cancelLoad,
|
|
|
|
});
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2022-11-12 21:15:41 +03:00
|
|
|
export const orphanVisitsReducerCreator = (
|
2022-11-13 11:59:49 +03:00
|
|
|
asyncThunkCreator: ReturnType<typeof getOrphanVisits>,
|
|
|
|
) => 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, shortUrl }) => !shortUrl && isBetween(visit.date, startDate, endDate));
|
|
|
|
},
|
2022-11-13 11:59:49 +03:00
|
|
|
});
|