(
state: { [key: string]: { [idx: number]: LoadingStatus } },
action: BaseAction,
)
| 14 | }; |
| 15 | |
| 16 | function reduceLoadingStatuses( |
| 17 | state: { [key: string]: { [idx: number]: LoadingStatus } }, |
| 18 | action: BaseAction, |
| 19 | ): { [key: string]: { [idx: number]: LoadingStatus } } { |
| 20 | const startMatch = action.type.match(/(.*)_STARTED/); |
| 21 | if (startMatch && fetchKeyRegistry.has(action.type)) { |
| 22 | if (!action.loadingInfo || typeof action.loadingInfo !== 'object') { |
| 23 | return state; |
| 24 | } |
| 25 | const { loadingInfo } = action; |
| 26 | if (typeof loadingInfo.fetchIndex !== 'number') { |
| 27 | return state; |
| 28 | } |
| 29 | const keyName: string = |
| 30 | loadingInfo.customKeyName && typeof loadingInfo.customKeyName === 'string' |
| 31 | ? loadingInfo.customKeyName |
| 32 | : startMatch[1]; |
| 33 | if (loadingInfo.trackMultipleRequests) { |
| 34 | return { |
| 35 | ...state, |
| 36 | [keyName]: { |
| 37 | ...state[keyName], |
| 38 | [loadingInfo.fetchIndex]: 'loading', |
| 39 | }, |
| 40 | }; |
| 41 | } else { |
| 42 | return { |
| 43 | ...state, |
| 44 | [keyName]: { |
| 45 | [loadingInfo.fetchIndex]: 'loading', |
| 46 | }, |
| 47 | }; |
| 48 | } |
| 49 | } |
| 50 | const failMatch = action.type.match(/(.*)_FAILED/); |
| 51 | if (failMatch && fetchKeyRegistry.has(action.type)) { |
| 52 | if (!action.loadingInfo || typeof action.loadingInfo !== 'object') { |
| 53 | return state; |
| 54 | } |
| 55 | const { loadingInfo } = action; |
| 56 | if (typeof loadingInfo.fetchIndex !== 'number') { |
| 57 | return state; |
| 58 | } |
| 59 | const keyName: string = |
| 60 | loadingInfo.customKeyName && typeof loadingInfo.customKeyName === 'string' |
| 61 | ? loadingInfo.customKeyName |
| 62 | : failMatch[1]; |
| 63 | if (state[keyName] && state[keyName][loadingInfo.fetchIndex]) { |
| 64 | return { |
| 65 | ...state, |
| 66 | [keyName]: { |
| 67 | ...state[keyName], |
| 68 | [loadingInfo.fetchIndex]: 'error', |
| 69 | }, |
| 70 | }; |
| 71 | } |
| 72 | return state; |
| 73 | } |
no test coverage detected