Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Last active November 9, 2017 10:45
Show Gist options
  • Select an option

  • Save tlaitinen/c6ec07fc87a3e0176712e82569f43842 to your computer and use it in GitHub Desktop.

Select an option

Save tlaitinen/c6ec07fc87a3e0176712e82569f43842 to your computer and use it in GitHub Desktop.
import {ActionCreator} from 'react-redux-typescript';
export interface CrudResult {
results: string[];
totalCount: number;
}
export interface CrudState<E,Q> {
readonly queries: Readonly<{[queryName:string]: Q}>;
readonly results: Readonly<{[queryName:string]: CrudResult}>;
readonly loading: Readonly<{[queryName:string]: boolean}>;
readonly busy: Readonly<{[entityId:string]: boolean}>;
readonly entities: Readonly<{[entityId:string]: E}>;
readonly selected: Readonly<{[entityId:string]: boolean}>;
}
function mkActionCreators<N,E,Q>(names) {
return {
SetBusy: new ActionCreator<N['SET_BUSY'], {entityId:string, busy:boolean}>(names('SET_BUSY')),
ClearSelected: new ActionCreator<N['CLEAR_SELECTED', {queryName:string}>(names('CLEAR_SELECTED')),
SetSelected: new ActionCreator<N['SET_SELECTED', {queryName:string, entityIdList:string[], selected:boolean}>(names('SET_SELECTED')),
SetLoading: new ActionCreator<N['SET_LOADING', {queryName:string, loading:boolean}>(names('SET_LOADING')),
SetQuery: new ActionCreator<N['SET_QUERY', {queryName:string, query:Q}>(names('SET_QUERY')),
Receive: new ActionCreator<N['RECEIVE', {queryName:string, results:E[], totalCount:number}>(names('RECEIVE')),
LocalUpdate: new ActionCreator<N['LOCAL_UPDATE'], {entityId:string, entity:Entity}>(names('LOCAL_UPDATE'))
};
}
export type Action<N,E,Q> = ActionCreator<N['SET_BUSY'], {entityId:string, busy:boolean}>
| ActionCreator<N['CLEAR_SELECTED'], {queryName:string}>
| ActionCreator<N['SET_SELECTED'], {queryName:string,entityIdList:string[], selected:boolean}>
| ActionCreator<N['SET_LOADING'], {queryName:string, loading:boolean}>
| ActionCreator<N['SET_QUERY'], {queryName:string, query:Q}>
| ActionCreator<N['RECEIVE'], {queryName:string, results:E[], totalCount:number}>
| ActionCreator<N['LOCAL_UPDATE'], {entityId:string, entity:E}>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment