Skip to content

Instantly share code, notes, and snippets.

@xvaldetaro
Created February 5, 2017 06:01
Show Gist options
  • Save xvaldetaro/0a167e840235575d1c831237623392ad to your computer and use it in GitHub Desktop.
Save xvaldetaro/0a167e840235575d1c831237623392ad to your computer and use it in GitHub Desktop.
import type {
MyShape,
MyOtherShape,
} from 'MyTypes';
import type {
ForeignFoo,
} from 'ForeignTypes';
const MyHandlers = require('MyHandlers');
export type Action = (
'ACT_Action1' |
'ACT_Action2'
);
const ActionEnum:
= {[key: Action]: Action} {
'ACT_Action1': ('ACT_Action1': 'ACT_Action1'), |
'ACT_Action2': ('ACT_Action2': 'ACT_Action2'),
};
type Action1Obj = {
type: 'ACT_Action1',
param1_1: string,
param1_2: MyType,
};
type Action2Obj = {
type: 'ACT_Action2',
param2_1: boolean,
param2_2: Object,
};
type ActionObj = (
Action1Obj |
Action2Obj
);
function createAction1(
param1_1: string,
param1_2: MyType,
): Action1 {
return {
type: 'ACT_Action1',
param1_1,
param1_2,
};
}
function createAction2(
param2_1: boolean,
param2_2: Object,
): Action2 {
return {
type: 'ACT_Action2',
param2_1,
param2_2,
};
}
type State = {
simpleFlag: string,
valueField: number,
manualField: MyOtherShape,
};
type INITIAL_STATE: State = {
simpleFlag: '',
valueField: 0,
manualField: {},
};
function reducer(
state: State = INITIAL_STATE,
action: ActionObj,
): State {
switch (action.type) {
case 'ACT_Action1':
return Object.assign(state, {
simpleFlag: action.simpleFlag,
valueField: 1234,
});
case 'ACT_Action2':
return MyHandlers.Action2Handler(state, action);
default:
return state;
}
}
export type PresmaCTAAction = Action;
export type PresmaCTAActionObj = ActionObj;
export type PresmaCTAState = State;
export type PresmaCTAAction1Obj = Action1Obj;
export type PresmaCTAAction2Obj = Action2Obj;
module.exports = {
PresmaCTAReducer: reducer,
PresmaCTAActionEnum: ActionEnum,
creators: {
createAction1,
createAction2,
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment