Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active December 15, 2017 20:47
Show Gist options
  • Save codeBelt/7d19e5ed7163d691edcf7f6312fac179 to your computer and use it in GitHub Desktop.
Save codeBelt/7d19e5ed7163d691edcf7f6312fac179 to your computer and use it in GitHub Desktop.
/**
* https://github.com/acdlite/flux-standard-action
*/
export interface IAction<T> {
type: string;
payload?: T;
error?: boolean;
meta?: any;
}
export interface ISampleReducerState {
readonly isLoading: boolean;
}
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,
};
}
}
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