Last active
January 14, 2018 07:15
-
-
Save JonathanDn/f7f646cd990721e2a55f8368e835b340 to your computer and use it in GitHub Desktop.
Redux Typescript - UI Reducers - TOGGLE / UPDATE for common UI States
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
export module UIConsts { | |
export const UI_UPDATE = 'UI_UPDATE'; | |
export const TOGGLE_UI_STATE = 'TOGGLE_UI_STATE'; | |
} |
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
export interface StateAction { | |
type: string; | |
payload: any | |
} |
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 {StateAction} from '../app.interfaces'; | |
import {UIConsts} from '../app.consts'; | |
import * as _ from 'lodash'; | |
// Examples: | |
export const INITIAL_STATE = { | |
isItemListShowing: false, | |
isNewItemCreated: false, | |
isItemListLoading: false | |
} | |
export const UIReducer = (state: any = INITIAL_STATE, {type, payload}: StateAction) => { | |
switch (type) { | |
case UIConsts.UI_UPDATE: | |
return _.assign({}, state, payload); | |
case UIConsts.TOGGLE_UI_STATE: | |
if (!payload.key) | |
return state; | |
let newState = {}; | |
newState[payload.key] = !_.get(state, payload.key); | |
return _.assign({}, state, newState); | |
default: | |
return state; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment