summaryrefslogtreecommitdiff
path: root/frontend/src/redux/currentGame.ts
blob: 5e015d6082d2c49c44bc0b631227feac6e3291dd (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
38
39
40
41
42
43
44
45
46
import { combineReducers } from 'redux';
import { ApiPlayerTurnInfo, ApiTable } from '../api/model';
import { GlobalState } from '../reducers';
import { Action } from './actions/all';
import { TABLE_UPDATE_RECEIVED, TURN_INFO_RECEIVED } from './actions/game';

export type CurrentGameState = {
  turnInfo: ApiPlayerTurnInfo | null;
  table: ApiTable | null;
}

export const EMPTY_CURRENT_GAME: CurrentGameState = {
  turnInfo: null,
  table: null,
};

export function createCurrentGameReducer() {
  return combineReducers({
    turnInfo: turnInfoReducer,
    table: tableUpdatesReducer,
  });
}

const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => {
  switch (action.type) {
    case TURN_INFO_RECEIVED:
      return action.turnInfo;
    case TABLE_UPDATE_RECEIVED:
      return null;
    default:
      return state;
  }
};

const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => {
  switch (action.type) {
    case TURN_INFO_RECEIVED:
      return action.turnInfo.table;
    case TABLE_UPDATE_RECEIVED:
      return action.table;
    default:
      return state;
  }
};

export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo | null => state.currentGame.turnInfo;
bgstack15