2022-11-12 12:33:52 +03:00
|
|
|
import { OrphanVisit, OrphanVisitType } from '../types';
|
2021-02-27 22:03:51 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { isOrphanVisit } from '../types/helpers';
|
2021-10-24 11:31:32 +03:00
|
|
|
import { isBetween } from '../../utils/helpers/date';
|
2022-11-12 22:37:04 +03:00
|
|
|
import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common';
|
2022-11-12 21:15:41 +03:00
|
|
|
import { 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;
|
|
|
|
|
|
|
|
export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => createVisitsAsyncThunk({
|
2022-11-13 11:59:49 +03:00
|
|
|
typePrefix: `${REDUCER_PREFIX}/getOrphanVisits`,
|
2022-11-12 21:15:41 +03:00
|
|
|
createLoaders: ({ orphanVisitsType, query = {}, doIntervalFallback = false }: LoadOrphanVisits, getState) => {
|
|
|
|
const { getOrphanVisits: getVisits } = buildShlinkApiClient(getState);
|
|
|
|
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
|
|
|
});
|