2022-11-12 21:36:12 +03:00
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
2020-04-18 11:50:01 +03:00
|
|
|
import { shortUrlMatches } from '../../short-urls/helpers';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { ShortUrlIdentifier } from '../../short-urls/data';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2021-10-24 11:31:32 +03:00
|
|
|
import { isBetween } from '../../utils/helpers/date';
|
2022-11-12 21:36:12 +03:00
|
|
|
import { createVisitsAsyncThunk, lastVisitLoaderForLoader } from './common';
|
2022-11-08 00:29:15 +03:00
|
|
|
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
|
|
|
|
2022-11-12 10:49:14 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/shortUrlVisits';
|
2018-07-29 20:25:22 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
export interface ShortUrlVisits extends VisitsInfo, ShortUrlIdentifier {}
|
|
|
|
|
2022-11-12 11:38:24 +03:00
|
|
|
export interface LoadShortUrlVisits extends LoadVisits {
|
|
|
|
shortCode: string;
|
|
|
|
}
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const initialState: ShortUrlVisits = {
|
2018-07-29 20:25:22 +03:00
|
|
|
visits: [],
|
2020-04-18 11:50:01 +03:00
|
|
|
shortCode: '',
|
2021-10-24 11:31:32 +03:00
|
|
|
domain: undefined, // Deprecated. Value from query params can be used instead
|
2018-07-29 20:25:22 +03:00
|
|
|
loading: false,
|
2019-03-04 21:21:46 +03:00
|
|
|
loadingLarge: false,
|
2018-08-26 00:39:27 +03:00
|
|
|
error: false,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelLoad: false,
|
2020-05-11 20:32:42 +03:00
|
|
|
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 }),
|
|
|
|
);
|
2020-04-18 11:50:01 +03:00
|
|
|
|
2022-11-12 21:36:12 +03:00
|
|
|
return [visitsLoader, lastVisitLoader];
|
2020-04-18 11:50:01 +03:00
|
|
|
},
|
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 }
|
|
|
|
));
|
2018-12-18 12:14:25 +03:00
|
|
|
|
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);
|
2019-03-08 21:40:43 +03:00
|
|
|
|
2022-11-12 21:36:12 +03:00
|
|
|
return newVisits.length === 0 ? state : { ...state, visits: [...newVisits, ...visits] };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { cancelGetShortUrlVisits } = actions;
|
|
|
|
|
|
|
|
return { reducer, cancelGetShortUrlVisits };
|
|
|
|
};
|