Last active
October 19, 2017 09:29
-
-
Save ahushh/104a4d16c89d2efb1363e4a87a3ffd70 to your computer and use it in GitHub Desktop.
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
import {IActionStatus} from '../models/common' | |
export default function updateStatus(status: {[action: string]: IActionStatus}, | |
field: string, | |
type: 'init' | 'success' | 'fail', | |
payload: any) { | |
switch (type) { | |
case 'init': | |
return { | |
...status, | |
[field]: { | |
...status[field], | |
loading: true, | |
loaded: false, | |
success: false, | |
error: null | |
} | |
} | |
case 'success': | |
return { | |
...status, | |
[field]: { | |
...status[field], | |
loading: false, | |
loaded: true, | |
success: true, | |
error: null | |
} | |
} | |
case 'fail': | |
return { | |
...status, | |
[field]: { | |
...status[field], | |
loading: false, | |
loaded: false, | |
success: false, | |
error: payload.error | |
} | |
} | |
} | |
} | |
/* Example | |
case actions.LOGIN: | |
return {...state, status: updateStatus(state.status, 'login', 'init', action.payload)} | |
case actions.LOGIN_SUCCESS: | |
return {...state, status: updateStatus(state.status, 'login', 'success', action.payload)} | |
case actions.LOGIN_FAIL: | |
return {...state, status: updateStatus(state.status, 'login', 'fail', action.payload)} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment