Skip to content

Instantly share code, notes, and snippets.

@ahushh
Last active October 19, 2017 09:29
Show Gist options
  • Save ahushh/104a4d16c89d2efb1363e4a87a3ffd70 to your computer and use it in GitHub Desktop.
Save ahushh/104a4d16c89d2efb1363e4a87a3ffd70 to your computer and use it in GitHub Desktop.
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