Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / request / reducer.ts
blob0fa722482543802a630efe3a0ae6e06b72b79894
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) {
18             case 'start':
19                 nextState[request.id] = { status: 'start', progress: 0, data };
20                 return nextState;
22             case 'progress':
23                 const ongoing = nextState[request.id];
24                 if (ongoing?.status !== 'start') return state;
25                 nextState[request.id] = { ...ongoing, progress: request.progress, data };
26                 return nextState;
28             case 'failure':
29                 nextState[request.id] = { status: request.status, data };
30                 return nextState;
32             case 'success':
33                 const now = getEpoch();
34                 nextState[request.id] = { status: request.status, maxAge: request.maxAge, requestedAt: now, data };
35                 return nextState;
36         }
37     }
39     if (requestProgress.match(action) && state[action.payload.requestId]?.status === 'start') {
40         return partialMerge(state, { [action.payload.requestId]: { progress: action.payload.progress } });
41     }
43     if (requestInvalidate.match(action)) return objectDelete(state, action.payload.requestId);
45     return state;
48 export default requestReducer;