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

93 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-11-12 21:36:12 +03:00
import { createSlice } from '@reduxjs/toolkit';
import { shortUrlMatches } from '../../short-urls/helpers';
import { ShortUrlIdentifier } from '../../short-urls/data';
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { isBetween } from '../../utils/helpers/date';
2022-11-12 21:36:12 +03:00
import { createVisitsAsyncThunk, lastVisitLoaderForLoader } from './common';
import { createNewVisits, CreateVisitsAction } from './visitCreation';
2022-11-12 21:36:12 +03:00
import { LoadVisits, VisitsInfo } from './types';
import { parseApiError } from '../../api/utils';
2018-07-29 20:25:22 +03:00
const REDUCER_PREFIX = 'shlink/shortUrlVisits';
2018-07-29 20:25:22 +03:00
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
export interface LoadShortUrlVisits extends LoadVisits {
shortCode: string;
}
const initialState: ShortUrlVisits = {
2018-07-29 20:25:22 +03:00
visits: [],
shortCode: '',
domain: undefined, // Deprecated. Value from query params can be used instead
2018-07-29 20:25:22 +03:00
loading: false,
loadingLarge: false,
error: false,
cancelLoad: false,
progress: 0,
2018-07-29 20:25:22 +03:00
};
2022-11-12 21:36:12 +03:00
export const getShortUrlVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => createVisitsAsyncThunk({
actionsPrefix: `${REDUCER_PREFIX}/getShortUrlVisits`,
createLoaders: ({ shortCode, query = {}, doIntervalFallback = false }: LoadShortUrlVisits, getState) => {
const { getShortUrlVisits: shlinkGetShortUrlVisits } = buildShlinkApiClient(getState);
const visitsLoader = async (page: number, itemsPerPage: number) => shlinkGetShortUrlVisits(
shortCode,
{ ...query, page, itemsPerPage },
);
const lastVisitLoader = lastVisitLoaderForLoader(
doIntervalFallback,
async (params) => shlinkGetShortUrlVisits(shortCode, { ...params, domain: query.domain }),
);
2022-11-12 21:36:12 +03:00
return [visitsLoader, lastVisitLoader];
},
2022-11-12 21:36:12 +03:00
getExtraFulfilledPayload: ({ shortCode, query = {} }: LoadShortUrlVisits) => (
{ shortCode, query, domain: query.domain }
),
shouldCancel: (getState) => getState().shortUrlVisits.cancelLoad,
});
2018-07-29 20:25:22 +03:00
2022-11-12 21:36:12 +03:00
export const shortUrlVisitsReducerCreator = (
{ asyncThunk, largeAction, progressChangedAction, fallbackToIntervalAction }: ReturnType<typeof getShortUrlVisits>,
) => {
const { reducer, actions } = createSlice({
name: REDUCER_PREFIX,
initialState,
reducers: {
cancelGetShortUrlVisits: (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-11-12 21:36:12 +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 }: CreateVisitsAction) => {
const { shortCode, domain, visits, query = {} } = state;
const { startDate, endDate } = query;
const newVisits = payload.createdVisits
.filter(
({ shortUrl, visit }) =>
shortUrl && shortUrlMatches(shortUrl, shortCode, domain) && isBetween(visit.date, startDate, endDate),
)
.map(({ visit }) => visit);
2022-11-12 21:36:12 +03:00
return newVisits.length === 0 ? state : { ...state, visits: [...newVisits, ...visits] };
});
},
});
const { cancelGetShortUrlVisits } = actions;
return { reducer, cancelGetShortUrlVisits };
};