Skip to content

Instantly share code, notes, and snippets.

@JonathanDn
Last active January 14, 2018 07:15
Show Gist options
  • Save JonathanDn/f7f646cd990721e2a55f8368e835b340 to your computer and use it in GitHub Desktop.
Save JonathanDn/f7f646cd990721e2a55f8368e835b340 to your computer and use it in GitHub Desktop.
Redux Typescript - UI Reducers - TOGGLE / UPDATE for common UI States
export module UIConsts {
export const UI_UPDATE = 'UI_UPDATE';
export const TOGGLE_UI_STATE = 'TOGGLE_UI_STATE';
}
export interface StateAction {
type: string;
payload: any
}
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