diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2019-05-03 01:27:22 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-05-06 18:33:14 +0200 |
commit | 0d09d870d9adf789232a5e6165f44c605839833a (patch) | |
tree | 304cabb01f2b3a9b9afd338f784d957a5a55dd48 /frontend/src/redux/user.ts | |
parent | Convert redux actions to typescript (diff) | |
download | seven-wonders-0d09d870d9adf789232a5e6165f44c605839833a.tar.gz seven-wonders-0d09d870d9adf789232a5e6165f44c605839833a.tar.bz2 seven-wonders-0d09d870d9adf789232a5e6165f44c605839833a.zip |
Convert reducers to typescript
Diffstat (limited to 'frontend/src/redux/user.ts')
-rw-r--r-- | frontend/src/redux/user.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/frontend/src/redux/user.ts b/frontend/src/redux/user.ts new file mode 100644 index 00000000..2cc25cc0 --- /dev/null +++ b/frontend/src/redux/user.ts @@ -0,0 +1,43 @@ +import { ApiPlayer } from '../api/model'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; +import { SET_CURRENT_PLAYER } from './actions/user'; +import { getCurrentGame } from './games'; + +export type User = { + username: string, + displayName: string, +} + +export const currentUserReducer = (state: User | null = null, action: Action) => { + switch (action.type) { + case SET_CURRENT_PLAYER: + return { + username: action.player.username, + displayName: action.player.displayName + }; + default: + return state; + } +}; + +export function getCurrentUser(state: GlobalState): User | null { + return state.currentUser +} + +export function getCurrentPlayer(state: GlobalState): ApiPlayer | null { + if (state.currentUser == null) { + return null; + } + let game = getCurrentGame(state); + if (game == null) { + return null; + } + for (let i = 0; i < game.players.length; i++) { + let player = game.players[i]; + if (player.username === state.currentUser.username) { + return player; + } + } + return null; +} |