Created
February 8, 2019 10:38
-
-
Save vvscode/1217a30f324807841bfd8a321ea87e11 to your computer and use it in GitHub Desktop.
handle-api-call-state-nicely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// базовый редуктор должен обновлять только не loadable состояния | |
function baseNewsReducer(state: News = createDefaultNews(), action: NewsActions): News { | |
switch (action.type) { | |
case NewsActionsTypes.LoadSuccess: | |
return { | |
...state, | |
entities: action.payload.entities | |
}; | |
default: | |
return state; | |
} | |
} | |
// withLoadable расширяет baseReducer для обновления состояния loadable | |
export function newsReducer(state: News, action: NewsActions): News { | |
return withLoadable(baseNewsReducer, { | |
loadingActionType: NewsActionsTypes.Load, | |
successActionType: NewsActionsTypes.LoadSuccess, | |
errorActionType: NewsActionsTypes.LoadError, | |
})(state, action); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment