// @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 { ENTER_LOBBY, UPDATE_GAMES } from './actions/lobby'; export type GamesState = { all: Map, current: string | void }; export const createGamesReducer = () => { return combineReducers({ all: allGamesReducer, current: currentGameIdReducer }) }; export const allGamesReducer = (state: Map = Map(), action: Action) => { switch (action.type) { case UPDATE_GAMES: let newGames = {}; action.games.forEach(g => newGames[g.id] = g); return state.merge(Map(newGames)); default: return state; } }; export const currentGameIdReducer = (state: string | void = null, action: Action) => { switch (action.type) { case ENTER_LOBBY: return `${action.gameId}`; default: return state; } }; export const getAllGames = (state: GlobalState): List => state.games.all.toList(); export const getCurrentGame = (state: GlobalState): ApiLobby | null => state.games.all.get(state.games.current);