From 0bf423172ffb2e4030b521b0985d133cb5c61dd9 Mon Sep 17 00:00:00 2001 From: Victor Chabbert Date: Sun, 28 May 2017 20:42:21 +0200 Subject: Move to immutable with Records --- frontend/src/redux/players.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'frontend/src/redux/players.js') diff --git a/frontend/src/redux/players.js b/frontend/src/redux/players.js index b11e920f..85b579f3 100644 --- a/frontend/src/redux/players.js +++ b/frontend/src/redux/players.js @@ -1,4 +1,4 @@ -import Immutable from 'seamless-immutable'; +import PlayerState, { Player } from '../models/players'; export const types = { REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME', @@ -21,24 +21,17 @@ export const actions = { }), }; -const initialState = Immutable.from({ - all: {}, - current: '', -}); - -export default (state = initialState, action) => { +export default (state = new PlayerState(), action) => { switch (action.type) { case types.SET_CURRENT_PLAYER: - const player = action.player; - const withNewPlayer = state.setIn(['all', player.username], player); - return Immutable.set(withNewPlayer, 'current', player.username); + return state.addPlayer(action.player); case types.UPDATE_PLAYERS: - return Immutable.merge(state, { all: action.players }, { deep: true }); + return state.addPlayers(action.players); default: return state; } }; -export const getCurrentPlayer = state => state.players.all && state.players.all[state.players.current]; -export const getPlayer = (state, username) => state.players.all[username]; -export const getPlayers = (state, usernames) => usernames.map(u => getPlayer(state, u)); +export const getCurrentPlayer = players => players.all.get(players.current, new Player({ displayName: '[ERROR]' })); +export const getPlayer = (players, username) => players.all.get(username); +export const getPlayers = (players, usernames) => usernames.map(u => getPlayer(players, u)); -- cgit