diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-12-21 12:06:41 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-12-21 12:06:41 +0100 |
commit | f0c8cf0e79f003514fd65a70def5820205955a77 (patch) | |
tree | cb66d325fb5d16d8b7fa0f14c91ad17dd4ff7c6c /src/client/react/users.ts | |
parent | 569b2969d530f08e55798c5cb3079948c7c037cd (diff) |
Move to typescript
Diffstat (limited to 'src/client/react/users.ts')
-rw-r--r-- | src/client/react/users.ts | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/client/react/users.ts b/src/client/react/users.ts new file mode 100644 index 0000000..a80a1c5 --- /dev/null +++ b/src/client/react/users.ts @@ -0,0 +1,89 @@ +/* global USERS */ + +import { combineReducers, createStore } from 'redux'; + +export interface User { + type: string, + value: string, + id: string, +} + +type Action = { + type: 'USERS/ADD_USER', + user: User, +} + +declare global { + interface Window { + USERS: User[]; + } +} + +const getId = ({ type, value }: User) => `${type}/${value}`; + +const byId = (state = {}, action: Action) => { + switch (action.type) { + case 'USERS/ADD_USER': + return { + ...state, + [action.user.id]: { + ...action.user, + }, + }; + default: + return state; + } +}; + +const allIds = (state : any[] = [], action : Action) => { + switch (action.type) { + case 'USERS/ADD_USER': + return [ + ...state, + action.user.id, + ]; + default: + return state; + } +}; + +const allUsers = (state : any[] = [], action : Action) => { + switch (action.type) { + case 'USERS/ADD_USER': + return [ + ...state, + { + ...action.user, + }, + ]; + default: + return state; + } +}; + +interface State { + byId: any, + allIds: string[], + allUsers: User[] +} + +const store = createStore(combineReducers<State>({ + byId, + allIds, + allUsers, +})); + +window.USERS.forEach((user) => { + store.dispatch({ + type: 'USERS/ADD_USER', + user: { + type: user.type, + value: user.value, + id: getId(user), + }, + }); +}); + +const users = store.getState(); + +export default users; |