shlink-web-client/src/visits/reducers/domainVisits.ts

88 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-11-12 19:51:37 +03:00
import { createSlice } from '@reduxjs/toolkit';
2022-04-24 19:36:25 +03:00
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { isBetween } from '../../utils/helpers/date';
2022-11-12 19:51:37 +03:00
import { createVisitsAsyncThunk, lastVisitLoaderForLoader } from './common';
import { createNewVisits } from './visitCreation';
2022-04-24 19:36:25 +03:00
import { domainMatches } from '../../short-urls/helpers';
2022-11-12 19:51:37 +03:00
import { LoadVisits, VisitsInfo } from './types';
import { parseApiError } from '../../api/utils';
2022-04-24 19:36:25 +03:00
const REDUCER_PREFIX = 'shlink/domainVisits';
2022-04-24 19:36:25 +03:00
export const DEFAULT_DOMAIN = 'DEFAULT';
interface WithDomain {
2022-04-24 19:36:25 +03:00
domain: string;
}
export interface DomainVisits extends VisitsInfo, WithDomain {}
export interface LoadDomainVisits extends LoadVisits, WithDomain {}
2022-04-24 19:36:25 +03:00
const initialState: DomainVisits = {
visits: [],
domain: '',
loading: false,
loadingLarge: false,
error: false,
cancelLoad: false,
progress: 0,
};
2022-11-12 19:51:37 +03:00
export const getDomainVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => createVisitsAsyncThunk({
actionsPrefix: `${REDUCER_PREFIX}/getDomainVisits`,
createLoaders: ({ domain, query = {}, doIntervalFallback = false }: LoadDomainVisits, getState) => {
const { getDomainVisits: getVisits } = buildShlinkApiClient(getState);
const visitsLoader = async (page: number, itemsPerPage: number) => getVisits(
domain,
{ ...query, page, itemsPerPage },
);
const lastVisitLoader = lastVisitLoaderForLoader(doIntervalFallback, async (params) => getVisits(domain, params));
2022-04-24 19:36:25 +03:00
2022-11-12 19:51:37 +03:00
return [visitsLoader, lastVisitLoader];
2022-04-24 19:36:25 +03:00
},
2022-11-12 19:51:37 +03:00
getExtraFulfilledPayload: ({ domain, query = {} }: LoadDomainVisits) => ({ domain, query }),
shouldCancel: (getState) => getState().domainVisits.cancelLoad,
});
2022-04-24 19:36:25 +03:00
2022-11-12 19:51:37 +03:00
export const domainVisitsReducerCreator = (
{ asyncThunk, largeAction, progressChangedAction, fallbackToIntervalAction }: ReturnType<typeof getDomainVisits>,
) => {
const { reducer, actions } = createSlice({
name: REDUCER_PREFIX,
initialState,
reducers: {
cancelGetDomainVisits: (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-04-24 19:36:25 +03:00
2022-11-12 19:51:37 +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 }
));
2022-11-12 19:51:37 +03:00
builder.addCase(createNewVisits, (state, { payload }) => {
const { domain, visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = payload.createdVisits
.filter(({ shortUrl, visit }) =>
shortUrl && domainMatches(shortUrl, domain) && isBetween(visit.date, startDate, endDate))
.map(({ visit }) => visit);
2022-04-24 19:36:25 +03:00
2022-11-12 19:51:37 +03:00
return { ...state, visits: [...newVisits, ...visits] };
});
},
});
const { cancelGetDomainVisits } = actions;
return { reducer, cancelGetDomainVisits };
};