2023-02-18 12:40:37 +03:00
|
|
|
import type { PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
2023-07-16 09:47:10 +03:00
|
|
|
import { createAsyncThunk } from '../../../utils/helpers/redux';
|
2023-07-24 18:30:58 +03:00
|
|
|
import type { ShlinkApiClient, ShlinkVisitsOverview } from '../../api-contract';
|
2023-03-18 12:54:14 +03:00
|
|
|
import type { CreateVisit } from '../types';
|
2021-02-27 22:03:51 +03:00
|
|
|
import { groupNewVisitsByType } from '../types/helpers';
|
2022-11-11 22:23:19 +03:00
|
|
|
import { createNewVisits } from './visitCreation';
|
2020-12-07 14:12:39 +03:00
|
|
|
|
2022-11-11 22:23:19 +03:00
|
|
|
const REDUCER_PREFIX = 'shlink/visitsOverview';
|
2020-12-07 14:12:39 +03:00
|
|
|
|
2023-03-18 12:54:14 +03:00
|
|
|
export type PartialVisitsSummary = {
|
|
|
|
total: number;
|
|
|
|
nonBots?: number;
|
|
|
|
bots?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ParsedVisitsOverview = {
|
|
|
|
nonOrphanVisits: PartialVisitsSummary;
|
|
|
|
orphanVisits: PartialVisitsSummary;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface VisitsOverview extends ParsedVisitsOverview {
|
2020-12-07 14:12:39 +03:00
|
|
|
loading: boolean;
|
|
|
|
error: boolean;
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:23:19 +03:00
|
|
|
export type GetVisitsOverviewAction = PayloadAction<ShlinkVisitsOverview>;
|
2020-12-07 14:12:39 +03:00
|
|
|
|
|
|
|
const initialState: VisitsOverview = {
|
2023-03-18 12:54:14 +03:00
|
|
|
nonOrphanVisits: {
|
|
|
|
total: 0,
|
|
|
|
},
|
|
|
|
orphanVisits: {
|
|
|
|
total: 0,
|
|
|
|
},
|
2020-12-07 14:12:39 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2023-03-18 12:54:14 +03:00
|
|
|
const countBots = (visits: CreateVisit[]) => visits.filter(({ visit }) => visit.potentialBot).length;
|
|
|
|
|
2023-07-24 10:48:41 +03:00
|
|
|
export const loadVisitsOverview = (apiClient: ShlinkApiClient) => createAsyncThunk(
|
2022-11-11 22:23:19 +03:00
|
|
|
`${REDUCER_PREFIX}/loadVisitsOverview`,
|
2023-07-24 10:48:41 +03:00
|
|
|
(): Promise<ParsedVisitsOverview> => apiClient.getVisitsOverview().then(
|
|
|
|
({ nonOrphanVisits, visitsCount, orphanVisits, orphanVisitsCount }) => ({
|
2023-03-18 12:54:14 +03:00
|
|
|
nonOrphanVisits: {
|
2023-07-24 10:48:41 +03:00
|
|
|
total: nonOrphanVisits?.total ?? visitsCount,
|
|
|
|
nonBots: nonOrphanVisits?.nonBots,
|
|
|
|
bots: nonOrphanVisits?.bots,
|
2023-03-18 12:54:14 +03:00
|
|
|
},
|
|
|
|
orphanVisits: {
|
2023-07-24 10:48:41 +03:00
|
|
|
total: orphanVisits?.total ?? orphanVisitsCount,
|
|
|
|
nonBots: orphanVisits?.nonBots,
|
|
|
|
bots: orphanVisits?.bots,
|
2023-03-18 12:54:14 +03:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
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 }));
|
|
|
|
|
2023-03-18 12:54:14 +03:00
|
|
|
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,
|
2023-03-18 12:54:14 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
});
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
2022-11-11 22:23:19 +03:00
|
|
|
});
|