shlink-web-client/src/shlink-web-component/visits/reducers/visitsOverview.ts

100 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import { createAsyncThunk } from '../../../utils/helpers/redux';
2023-07-24 18:30:58 +03:00
import type { ShlinkApiClient, ShlinkVisitsOverview } from '../../api-contract';
import type { CreateVisit } from '../types';
import { groupNewVisitsByType } from '../types/helpers';
2022-11-11 22:23:19 +03:00
import { createNewVisits } from './visitCreation';
2022-11-11 22:23:19 +03:00
const REDUCER_PREFIX = 'shlink/visitsOverview';
export type PartialVisitsSummary = {
total: number;
nonBots?: number;
bots?: number;
};
export type ParsedVisitsOverview = {
nonOrphanVisits: PartialVisitsSummary;
orphanVisits: PartialVisitsSummary;
};
export interface VisitsOverview extends ParsedVisitsOverview {
loading: boolean;
error: boolean;
}
2022-11-11 22:23:19 +03:00
export type GetVisitsOverviewAction = PayloadAction<ShlinkVisitsOverview>;
const initialState: VisitsOverview = {
nonOrphanVisits: {
total: 0,
},
orphanVisits: {
total: 0,
},
loading: false,
error: false,
};
const countBots = (visits: CreateVisit[]) => visits.filter(({ visit }) => visit.potentialBot).length;
export const loadVisitsOverview = (apiClient: ShlinkApiClient) => createAsyncThunk(
2022-11-11 22:23:19 +03:00
`${REDUCER_PREFIX}/loadVisitsOverview`,
(): Promise<ParsedVisitsOverview> => apiClient.getVisitsOverview().then(
({ nonOrphanVisits, visitsCount, orphanVisits, orphanVisitsCount }) => ({
nonOrphanVisits: {
total: nonOrphanVisits?.total ?? visitsCount,
nonBots: nonOrphanVisits?.nonBots,
bots: nonOrphanVisits?.bots,
},
orphanVisits: {
total: orphanVisits?.total ?? orphanVisitsCount,
nonBots: orphanVisits?.nonBots,
bots: orphanVisits?.bots,
},
}),
),
2022-11-11 22:23:19 +03:00
);
export const visitsOverviewReducerCreator = (
loadVisitsOverviewThunk: ReturnType<typeof loadVisitsOverview>,
) => createSlice({
name: REDUCER_PREFIX,
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(loadVisitsOverviewThunk.pending, () => ({ ...initialState, loading: true }));
builder.addCase(loadVisitsOverviewThunk.rejected, () => ({ ...initialState, error: true }));
builder.addCase(loadVisitsOverviewThunk.fulfilled, (_, { payload }) => ({ ...initialState, ...payload }));
builder.addCase(createNewVisits, ({ nonOrphanVisits, orphanVisits, ...rest }, { payload }) => {
const { nonOrphanVisits: newNonOrphanVisits, orphanVisits: newOrphanVisits } = groupNewVisitsByType(
payload.createdVisits,
);
const newNonOrphanTotalVisits = newNonOrphanVisits.length;
const newNonOrphanBotVisits = countBots(newNonOrphanVisits);
const newNonOrphanNonBotVisits = newNonOrphanTotalVisits - newNonOrphanBotVisits;
const newOrphanTotalVisits = newOrphanVisits.length;
const newOrphanBotVisits = countBots(newOrphanVisits);
const newOrphanNonBotVisits = newOrphanTotalVisits - newOrphanBotVisits;
2022-11-11 22:23:19 +03:00
return {
...rest,
nonOrphanVisits: {
total: nonOrphanVisits.total + newNonOrphanTotalVisits,
bots: nonOrphanVisits.bots && nonOrphanVisits.bots + newNonOrphanBotVisits,
nonBots: nonOrphanVisits.nonBots && nonOrphanVisits.nonBots + newNonOrphanNonBotVisits,
},
orphanVisits: {
total: orphanVisits.total + newOrphanTotalVisits,
bots: orphanVisits.bots && orphanVisits.bots + newOrphanBotVisits,
nonBots: orphanVisits.nonBots && orphanVisits.nonBots + newOrphanNonBotVisits,
},
2022-11-11 22:23:19 +03:00
};
});
},
2022-11-11 22:23:19 +03:00
});