Last active
December 15, 2017 20:47
-
-
Save codeBelt/7d19e5ed7163d691edcf7f6312fac179 to your computer and use it in GitHub Desktop.
TypeScript Redux Sample Action & Reducer | https://github.com/codeBelt/typescript-hapi-react-hot-loader-example
This file contains hidden or 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
/** | |
* https://github.com/acdlite/flux-standard-action | |
*/ | |
export interface IAction<T> { | |
type: string; | |
payload?: T; | |
error?: boolean; | |
meta?: any; | |
} |
This file contains hidden or 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 ISampleReducerState { | |
readonly isLoading: boolean; | |
} |
This file contains hidden or 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 {IAction} from '../IAction'; | |
export class SampleAction { | |
public static readonly SET_LOADING: string = 'SampleAction.SET_LOADING'; | |
public static setLoading(isLoading: boolean): IAction<boolean> { | |
return { | |
type: SampleAction.SET_LOADING, | |
payload: isLoading, | |
}; | |
} | |
} |
This file contains hidden or 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 {IAction} from '../IAction'; | |
import {ISampleReducerState} from './ISampleReducerState'; | |
import {SampleAction} from './SampleAction'; | |
export class SampleReducer { | |
private static readonly _initialState: ISampleReducerState = { | |
isLoading: false, | |
}; | |
public static reducer(state: ISampleReducerState = SampleReducer._initialState, action: IAction<any>): ISampleReducerState { | |
switch (action.type) { | |
case SampleAction.SET_LOADING: | |
return SampleReducer._setLoading(state, action); | |
default: | |
return state; | |
} | |
} | |
private static _setLoading(state: ISampleReducerState, action: IAction<boolean>): ISampleReducerState { | |
return { | |
...state, | |
isLoading: action.payload, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment