2020-12-07 14:12:39 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ShlinkVisitsOverview } from '../../api/types';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2020-12-07 14:12:39 +03:00
|
|
|
import { GetState } from '../../container/types';
|
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
2021-02-27 22:03:51 +03:00
|
|
|
import { groupNewVisitsByType } from '../types/helpers';
|
2020-12-07 14:12:39 +03:00
|
|
|
import { CREATE_VISITS, CreateVisitsAction } from './visitCreation';
|
|
|
|
|
|
|
|
export const GET_OVERVIEW_START = 'shlink/visitsOverview/GET_OVERVIEW_START';
|
|
|
|
export const GET_OVERVIEW_ERROR = 'shlink/visitsOverview/GET_OVERVIEW_ERROR';
|
|
|
|
export const GET_OVERVIEW = 'shlink/visitsOverview/GET_OVERVIEW';
|
|
|
|
|
|
|
|
export interface VisitsOverview {
|
|
|
|
visitsCount: number;
|
2022-05-02 20:28:07 +03:00
|
|
|
orphanVisitsCount: number;
|
2020-12-07 14:12:39 +03:00
|
|
|
loading: boolean;
|
|
|
|
error: boolean;
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:19:37 +03:00
|
|
|
export type GetVisitsOverviewAction = ShlinkVisitsOverview & Action<string>;
|
2020-12-07 14:12:39 +03:00
|
|
|
|
|
|
|
const initialState: VisitsOverview = {
|
|
|
|
visitsCount: 0,
|
2021-02-21 22:55:39 +03:00
|
|
|
orphanVisitsCount: 0,
|
2020-12-07 14:12:39 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default buildReducer<VisitsOverview, GetVisitsOverviewAction & CreateVisitsAction>({
|
|
|
|
[GET_OVERVIEW_START]: () => ({ ...initialState, loading: true }),
|
|
|
|
[GET_OVERVIEW_ERROR]: () => ({ ...initialState, error: true }),
|
2021-02-21 22:55:39 +03:00
|
|
|
[GET_OVERVIEW]: (_, { visitsCount, orphanVisitsCount }) => ({ ...initialState, visitsCount, orphanVisitsCount }),
|
2022-11-05 15:01:00 +03:00
|
|
|
[CREATE_VISITS]: ({ visitsCount, orphanVisitsCount = 0, ...rest }, { payload }) => {
|
|
|
|
const { regularVisits, orphanVisits } = groupNewVisitsByType(payload.createdVisits);
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
visitsCount: visitsCount + regularVisits.length,
|
|
|
|
orphanVisitsCount: orphanVisitsCount + orphanVisits.length,
|
|
|
|
};
|
|
|
|
},
|
2020-12-07 14:12:39 +03:00
|
|
|
}, initialState);
|
|
|
|
|
|
|
|
export const loadVisitsOverview = (buildShlinkApiClient: ShlinkApiClientBuilder) => () => async (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState,
|
|
|
|
) => {
|
|
|
|
dispatch({ type: GET_OVERVIEW_START });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { getVisitsOverview } = buildShlinkApiClient(getState);
|
|
|
|
const result = await getVisitsOverview();
|
|
|
|
|
|
|
|
dispatch({ type: GET_OVERVIEW, ...result });
|
|
|
|
} catch (e) {
|
|
|
|
dispatch({ type: GET_OVERVIEW_ERROR });
|
|
|
|
}
|
|
|
|
};
|