2021-10-17 10:36:14 +02:00
|
|
|
import { handleActions } from 'util/redux-utils';
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
fetchingViewCount: false,
|
|
|
|
viewCountError: undefined,
|
|
|
|
viewCountById: {},
|
|
|
|
fetchingSubCount: false,
|
|
|
|
subCountError: undefined,
|
|
|
|
subCountById: {},
|
2021-10-28 06:50:17 +02:00
|
|
|
subCountLastFetchedById: {},
|
2021-10-17 10:36:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const statsReducer = handleActions(
|
|
|
|
{
|
2021-10-28 06:50:17 +02:00
|
|
|
[ACTIONS.FETCH_VIEW_COUNT_STARTED]: (state) => ({ ...state, fetchingViewCount: true }),
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
[ACTIONS.FETCH_VIEW_COUNT_FAILED]: (state, action) => ({
|
|
|
|
...state,
|
|
|
|
viewCountError: action.data,
|
|
|
|
}),
|
2021-10-28 06:50:17 +02:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
[ACTIONS.FETCH_VIEW_COUNT_COMPLETED]: (state, action) => {
|
|
|
|
const { claimIdCsv, viewCounts } = action.data;
|
|
|
|
|
|
|
|
const viewCountById = Object.assign({}, state.viewCountById);
|
|
|
|
const claimIds = claimIdCsv.split(',');
|
|
|
|
|
|
|
|
if (claimIds.length === viewCounts.length) {
|
|
|
|
claimIds.forEach((claimId, index) => {
|
|
|
|
viewCountById[claimId] = viewCounts[index];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
fetchingViewCount: false,
|
|
|
|
viewCountById,
|
|
|
|
};
|
|
|
|
},
|
2021-10-28 06:50:17 +02:00
|
|
|
|
|
|
|
[ACTIONS.FETCH_SUB_COUNT_STARTED]: (state) => ({ ...state, fetchingSubCount: true }),
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
[ACTIONS.FETCH_SUB_COUNT_FAILED]: (state, action) => ({
|
|
|
|
...state,
|
|
|
|
subCountError: action.data,
|
|
|
|
}),
|
2021-10-28 06:50:17 +02:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
[ACTIONS.FETCH_SUB_COUNT_COMPLETED]: (state, action) => {
|
2022-03-01 15:25:52 +01:00
|
|
|
const { claimIds, subCounts, fetchDate } = action.data;
|
2021-10-17 10:36:14 +02:00
|
|
|
|
2021-10-28 06:50:17 +02:00
|
|
|
const subCountById = Object.assign({}, state.subCountById);
|
|
|
|
const subCountLastFetchedById = Object.assign({}, state.subCountLastFetchedById);
|
|
|
|
let dataChanged = false;
|
|
|
|
|
|
|
|
if (claimIds.length === subCounts.length) {
|
|
|
|
claimIds.forEach((claimId, index) => {
|
|
|
|
if (subCountById[claimId] !== subCounts[index]) {
|
|
|
|
subCountById[claimId] = subCounts[index];
|
|
|
|
dataChanged = true;
|
|
|
|
}
|
|
|
|
subCountLastFetchedById[claimId] = fetchDate;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const newState = {
|
2021-10-17 10:36:14 +02:00
|
|
|
...state,
|
|
|
|
fetchingSubCount: false,
|
2021-10-28 06:50:17 +02:00
|
|
|
subCountLastFetchedById,
|
2021-10-17 10:36:14 +02:00
|
|
|
};
|
2021-10-28 06:50:17 +02:00
|
|
|
|
|
|
|
if (dataChanged) {
|
|
|
|
newState.subCountById = subCountById;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
2021-10-17 10:36:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|