1 import type { Action, Reducer } from 'redux';
3 import { objectDelete } from '@proton/pass/utils/object/delete';
4 import { partialMerge } from '@proton/pass/utils/object/merge';
5 import { getEpoch } from '@proton/pass/utils/time/epoch';
7 import { requestInvalidate, requestProgress } from './actions';
8 import type { RequestState } from './types';
9 import { isActionWithRequest } from './utils';
11 const requestReducer: Reducer<RequestState> = (state = {}, action: Action) => {
12 if (isActionWithRequest(action)) {
13 const { request } = action.meta;
14 const nextState = { ...state };
15 const data = request.data && 'payload' in action ? action.payload : undefined;
17 switch (request.status) {
19 nextState[request.id] = { status: 'start', progress: 0, data };
23 const ongoing = nextState[request.id];
24 if (ongoing?.status !== 'start') return state;
25 nextState[request.id] = { ...ongoing, progress: request.progress, data };
29 nextState[request.id] = { status: request.status, data };
33 const now = getEpoch();
34 nextState[request.id] = { status: request.status, maxAge: request.maxAge, requestedAt: now, data };
39 if (requestProgress.match(action) && state[action.payload.requestId]?.status === 'start') {
40 return partialMerge(state, { [action.payload.requestId]: { progress: action.payload.progress } });
43 if (requestInvalidate.match(action)) return objectDelete(state, action.payload.requestId);
48 export default requestReducer;