2022-11-12 20:18:16 +03:00
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
2022-02-05 15:53:07 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
|
|
|
import { isBetween } from '../../utils/helpers/date';
|
2022-11-12 20:18:16 +03:00
|
|
|
import { createVisitsAsyncThunk, lastVisitLoaderForLoader } from './common';
|
|
|
|
import { createNewVisits } from './visitCreation';
|
|
|
|
import { VisitsInfo } from './types';
|
|
|
|
import { parseApiError } from '../../api/utils';
|
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,
|
|
|
|
};
|
|
|
|
|
2022-11-12 20:18:16 +03:00
|
|
|
export const getNonOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => createVisitsAsyncThunk({
|
|
|
|
actionsPrefix: `${REDUCER_PREFIX}/getNonOrphanVisits`,
|
|
|
|
createLoaders: ({ query = {}, doIntervalFallback = false }, getState) => {
|
|
|
|
const { getNonOrphanVisits: shlinkGetNonOrphanVisits } = buildShlinkApiClient(getState);
|
|
|
|
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 = (
|
|
|
|
{ asyncThunk, largeAction, progressChangedAction, fallbackToIntervalAction }: ReturnType<typeof getNonOrphanVisits>,
|
|
|
|
) => {
|
|
|
|
const { reducer, actions } = createSlice({
|
|
|
|
name: REDUCER_PREFIX,
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
cancelGetNonOrphanVisits: (state) => ({ ...state, cancelLoad: true }),
|
|
|
|
},
|
|
|
|
extraReducers: (builder) => {
|
|
|
|
builder.addCase(asyncThunk.pending, () => ({ ...initialState, loading: true }));
|
|
|
|
builder.addCase(asyncThunk.rejected, (_, { error }) => (
|
|
|
|
{ ...initialState, error: true, errorData: parseApiError(error) }
|
|
|
|
));
|
|
|
|
builder.addCase(asyncThunk.fulfilled, (state, { payload }) => (
|
|
|
|
{ ...state, ...payload, loading: false, loadingLarge: false, error: false }
|
|
|
|
));
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 20:18:16 +03:00
|
|
|
builder.addCase(largeAction, (state) => ({ ...state, loadingLarge: true }));
|
|
|
|
builder.addCase(progressChangedAction, (state, { payload: progress }) => ({ ...state, progress }));
|
|
|
|
builder.addCase(fallbackToIntervalAction, (state, { payload: fallbackInterval }) => (
|
|
|
|
{ ...state, fallbackInterval }
|
|
|
|
));
|
|
|
|
|
|
|
|
builder.addCase(createNewVisits, (state, { payload }) => {
|
|
|
|
const { visits, query = {} } = state;
|
|
|
|
const { startDate, endDate } = query;
|
|
|
|
const newVisits = payload.createdVisits
|
|
|
|
.filter(({ visit }) => isBetween(visit.date, startDate, endDate))
|
|
|
|
.map(({ visit }) => visit);
|
2022-02-05 15:53:07 +03:00
|
|
|
|
2022-11-12 20:18:16 +03:00
|
|
|
return { ...state, visits: [...newVisits, ...visits] };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { cancelGetNonOrphanVisits } = actions;
|
|
|
|
|
|
|
|
return { reducer, cancelGetNonOrphanVisits };
|
|
|
|
};
|