Created
October 23, 2018 00:51
-
-
Save lexeek/0bd7cc1c957f72c3b2a208fb194ca000 to your computer and use it in GitHub Desktop.
reusable reducer pattern
This file contains 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
interface IUserState { | |
"users": IUser[], | |
"selected_user": IUser, | |
"sort": ISort | |
} | |
const initial_state: IState { | |
"users": [], | |
"selected_user": {}, | |
"sort": { | |
"name": "desc" | |
} | |
} | |
const createUserReducer = (type: string) => { | |
return (state = initial_state, action: any): IUserState => { | |
case `FETCH_COMPLETE_${type}`: { | |
return { | |
"users": action.payload, | |
"selected_user": state.selected_user, | |
"sort": state.sort | |
} | |
} | |
case `SELECT_COMPLETE_${type}`: { | |
return { | |
"users": state.users, | |
"selected_user": action.payload, | |
"sort": state.sort | |
} | |
} | |
case `SORT_${type}`: { | |
return { | |
"users": state.users, | |
"selected_user": state.selected_users, | |
"sort": action.payload | |
} | |
} | |
case `SORT_COMPLETE_${type}`: { | |
return { | |
"users": action.payload, | |
"selected_user": state.selected_users, | |
"sort": state.sort | |
} | |
} | |
} | |
} | |
export interface IStoreState { | |
"adminstrators": IUserState | |
"editors": IUserState | |
} | |
const rootReducer = combineReducers({ | |
"adminstrators": createUserReducer("administrators"), | |
"editors": createUserReducer("editors") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment