UNPKG

@rootstrap/redux-tools

Version:

Redux tools we use in both react bases

40 lines (32 loc) 1.05 kB
import imm from 'immer' import { NOT_STARTED, LOADING, SUCCESS, ERROR } from './constants' const handleAction = (state, action) => { const { type, payload } = action const matchesStart = /(.*)_REQUEST/.exec(type) const matchesError = /(.*)_ERROR/.exec(type) const matchesReset = /(.*)_RESET/.exec(type) const matchesSuccess = /(.*)_SUCCESS/.exec(type) let status = NOT_STARTED let key = null if (matchesStart) { const [, requestName] = matchesStart key = requestName status = LOADING } else if (matchesReset) { const [, requestName] = matchesReset key = requestName status = NOT_STARTED } else if (matchesError) { const [, requestName] = matchesError key = requestName status = ERROR } else if (matchesSuccess) { const [, requestName] = matchesSuccess key = requestName status = SUCCESS } if (key) state[key] = { status, error: matchesError ? payload : undefined } return state } export default (state = {}, action) => imm(state, draft => handleAction(draft, action))