summaryrefslogtreecommitdiff
path: root/frontend/src/redux/user.js
blob: 0876d4c227dceedbc417a94df57baffd2298919a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { ApiPlayer } from '../api/model';
import type { GlobalState } from '../reducers';
import type { 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, 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 {
  return state.currentUser
}

export function getCurrentPlayer(state: GlobalState): ApiPlayer {
  let game = getCurrentGame(state);
  for (let i = 0; i < game.players.length; i++) {
    let player = game.players[i];
    if (player.username === state.currentUser.username) {
      return player;
    }
  }
  return null;
}
bgstack15