Created
July 25, 2017 22:17
-
-
Save robwormald/c941830216ee4d3faa484a8d6ad96388 to your computer and use it in GitHub Desktop.
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
interface Action { | |
type: string; | |
} | |
enum UserActions { | |
ADD_USER = 'ADD_USER', | |
DELETE_USER = 'DELETE_USER' | |
} | |
interface User { | |
id: number; | |
name: string; | |
} | |
interface AddUserAction extends Action { | |
type: UserActions.ADD_USER, | |
payload: User | |
} | |
interface DeleteUserAction extends Action { | |
type: UserActions.DELETE_USER, | |
payload: number; | |
} | |
type UserAction = | |
AddUserAction | | |
DeleteUserAction | |
function userReducer(state: User[] = [], action: UserAction): User[] { | |
switch (action.type) { | |
case UserActions.ADD_USER: | |
return state.concat([action.payload]); | |
case UserActions.DELETE_USER: | |
return state.filter(user => user.id === action.payload); | |
default: | |
return state; | |
} | |
} | |
userReducer([], {type: UserActions.ADD_USER, payload: {id: 1, name: 'foo'}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment