diff options
Diffstat (limited to 'frontend/src')
-rw-r--r-- | frontend/src/reducers.ts (renamed from frontend/src/reducers.js) | 8 | ||||
-rw-r--r-- | frontend/src/redux/currentGame.ts (renamed from frontend/src/redux/currentGame.js) | 9 | ||||
-rw-r--r-- | frontend/src/redux/games.ts (renamed from frontend/src/redux/games.js) | 29 | ||||
-rw-r--r-- | frontend/src/redux/user.ts (renamed from frontend/src/redux/user.js) | 16 |
4 files changed, 38 insertions, 24 deletions
diff --git a/frontend/src/reducers.js b/frontend/src/reducers.ts index 612bd0c5..679bffe1 100644 --- a/frontend/src/reducers.js +++ b/frontend/src/reducers.ts @@ -1,16 +1,16 @@ // @flow import { routerReducer } from 'react-router-redux'; import { combineReducers } from 'redux'; -import type { ApiPlayer } from './api/model'; -import type { CurrentGameState } from './redux/currentGame'; +import { ApiPlayer } from './api/model'; +import { CurrentGameState } from './redux/currentGame'; import { createCurrentGameReducer } from './redux/currentGame'; -import type { GamesState } from './redux/games'; +import { GamesState } from './redux/games'; import { createGamesReducer } from './redux/games'; import { currentUserReducer } from './redux/user'; export type GlobalState = { currentGame: CurrentGameState; - currentUser: ApiPlayer; + currentUser: ApiPlayer | null; games: GamesState; routing: any; } diff --git a/frontend/src/redux/currentGame.js b/frontend/src/redux/currentGame.ts index 21b7108e..76006677 100644 --- a/frontend/src/redux/currentGame.js +++ b/frontend/src/redux/currentGame.ts @@ -1,8 +1,7 @@ -// @flow import { combineReducers } from 'redux'; -import type { ApiPlayerTurnInfo, ApiTable } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; +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 = { @@ -39,4 +38,4 @@ const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => { } }; -export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo => state.currentGame.turnInfo; +export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo | null => state.currentGame.turnInfo; diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.ts index b5b0c7fc..5e21211a 100644 --- a/frontend/src/redux/games.js +++ b/frontend/src/redux/games.ts @@ -1,14 +1,13 @@ -// @flow import { List, Map } from 'immutable'; import { combineReducers } from 'redux'; -import type { ApiLobby } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; +import { ApiLobby } from '../api/model'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; import { ENTER_LOBBY, UPDATE_GAMES } from './actions/lobby'; export type GamesState = { all: Map<string, ApiLobby>, - current: string | void + current: string | null }; export const createGamesReducer = () => { @@ -21,15 +20,20 @@ export const createGamesReducer = () => { export const allGamesReducer = (state: Map<string, ApiLobby> = Map(), action: Action) => { switch (action.type) { case UPDATE_GAMES: - let newGames = {}; - action.games.forEach(g => newGames[g.id] = g); - return state.merge(Map(newGames)); + const newGames = mapify(action.games); + return state.merge(newGames); default: return state; } }; -export const currentGameIdReducer = (state: string | void = null, action: Action) => { +function mapify(games: ApiLobby[]): Map<string, ApiLobby> { + let newGames: {[id:string]:ApiLobby} = {}; + games.forEach(g => newGames[`${g.id}`] = g); + return Map(newGames); +} + +export const currentGameIdReducer = (state: string | null = null, action: Action) => { switch (action.type) { case ENTER_LOBBY: return `${action.gameId}`; @@ -39,4 +43,9 @@ export const currentGameIdReducer = (state: string | void = null, action: Action }; export const getAllGames = (state: GlobalState): List<ApiLobby> => state.games.all.toList(); -export const getCurrentGame = (state: GlobalState): ApiLobby | null => state.games.all.get(state.games.current); +export const getCurrentGame = (state: GlobalState): ApiLobby | null => { + if (state.games.current == null) { + return null; + } + return state.games.all.get(state.games.current) || null; +}; diff --git a/frontend/src/redux/user.js b/frontend/src/redux/user.ts index 0876d4c2..2cc25cc0 100644 --- a/frontend/src/redux/user.js +++ b/frontend/src/redux/user.ts @@ -1,6 +1,6 @@ import { ApiPlayer } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; import { SET_CURRENT_PLAYER } from './actions/user'; import { getCurrentGame } from './games'; @@ -9,7 +9,7 @@ export type User = { displayName: string, } -export const currentUserReducer = (state: ?User = null, action: Action) => { +export const currentUserReducer = (state: User | null = null, action: Action) => { switch (action.type) { case SET_CURRENT_PLAYER: return { @@ -21,12 +21,18 @@ export const currentUserReducer = (state: ?User = null, action: Action) => { } }; -export function getCurrentUser(state: GlobalState): ?User { +export function getCurrentUser(state: GlobalState): User | null { return state.currentUser } -export function getCurrentPlayer(state: GlobalState): ApiPlayer { +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) { |