Last active
November 21, 2019 08:41
-
-
Save zzdjk6/995f2332bd2db7c9c10ecaf567984682 to your computer and use it in GitHub Desktop.
Listing 1: Loading Reducer
This file contains hidden or 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
| // Reducer | |
| export default (state: LoadingState = {}, action: Action<any>): LoadingState => { | |
| const { type } = action; | |
| const matches = /(.*)\/(REQUEST|SUCCESS|FAILURE)/.exec(type); | |
| // Ignore non-routine actions: | |
| // A routine action should have one of three suffixes: | |
| // ['/REQUEST', '/SUCCESS', '/FAILURE'] | |
| if (!matches) return state; | |
| const [, routineType, status] = matches; | |
| return { | |
| ...state, | |
| // Set loading state to true only when the status is "REQUEST" | |
| // Otherwise set the loading state to false | |
| [routineType]: status === 'REQUEST' | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment