diff options
author | Victor Chabbert <chabbertvi@eisti.eu> | 2017-05-28 20:42:21 +0200 |
---|---|---|
committer | Victor Chabbert <chabbertvi@eisti.eu> | 2017-05-28 20:42:21 +0200 |
commit | 0bf423172ffb2e4030b521b0985d133cb5c61dd9 (patch) | |
tree | 0c24821770274a414f80b3092d5be54902c042d9 /frontend/src/redux | |
parent | Fix proxy not working since CRA upgrade (diff) | |
download | seven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.tar.gz seven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.tar.bz2 seven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.zip |
Move to immutable with Records
Diffstat (limited to 'frontend/src/redux')
-rw-r--r-- | frontend/src/redux/app.js | 2 | ||||
-rw-r--r-- | frontend/src/redux/errors.js | 22 | ||||
-rw-r--r-- | frontend/src/redux/games.js | 29 | ||||
-rw-r--r-- | frontend/src/redux/players.js | 21 |
4 files changed, 22 insertions, 52 deletions
diff --git a/frontend/src/redux/app.js b/frontend/src/redux/app.js index d24fbbfd..614e7d93 100644 --- a/frontend/src/redux/app.js +++ b/frontend/src/redux/app.js @@ -1,5 +1,5 @@ export const makeSelectLocationState = () => { return state => { - return state.routing; + return state.get('routing'); }; }; diff --git a/frontend/src/redux/errors.js b/frontend/src/redux/errors.js index ec1e30b6..ad1e2795 100644 --- a/frontend/src/redux/errors.js +++ b/frontend/src/redux/errors.js @@ -1,4 +1,4 @@ -import Immutable from 'seamless-immutable'; +import ErrorsState from '../models/errors'; export const types = { ERROR_RECEIVED_ON_WS: 'ERROR/RECEIVED_ON_WS', @@ -11,27 +11,11 @@ export const actions = { }), }; -const initialState = Immutable.from({ - nextId: 0, - history: [], -}); - -export default (state = initialState, action) => { +export default (state = new ErrorsState(), action) => { switch (action.type) { case types.ERROR_RECEIVED_ON_WS: - let error = Object.assign({ id: state.nextId, timestamp: new Date() }, action.error); - let newState = state.set('nextId', state.nextId + 1); - newState = addErrorToHistory(newState, error); - return newState; + return state.addError(action.error); default: return state; } }; - -function addErrorToHistory(state, error) { - return addToArray(state, 'history', error); -} - -function addToArray(state, arrayKey, element) { - return state.set(arrayKey, state[arrayKey].concat([element])); -} diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.js index 9ef0e7cd..d5953db1 100644 --- a/frontend/src/redux/games.js +++ b/frontend/src/redux/games.js @@ -1,4 +1,5 @@ -import Immutable from 'seamless-immutable'; +import { fromJS } from 'immutable'; +import GamesState from '../models/games'; export const types = { UPDATE_GAMES: 'GAME/UPDATE_GAMES', @@ -10,37 +11,29 @@ export const types = { }; export const actions = { - updateGames: games => ({ type: types.UPDATE_GAMES, games: Immutable(games) }), + updateGames: games => ({ type: types.UPDATE_GAMES, games: fromJS(games) }), requestJoinGame: gameId => ({ type: types.REQUEST_JOIN_GAME, gameId }), requestCreateGame: gameName => ({ type: types.REQUEST_CREATE_GAME, gameName, }), requestStartGame: () => ({ type: types.REQUEST_START_GAME }), - enterLobby: lobby => ({ type: types.ENTER_LOBBY, lobby: Immutable(lobby) }), + enterLobby: lobby => ({ type: types.ENTER_LOBBY, lobby: fromJS(lobby) }), enterGame: () => ({ type: types.ENTER_GAME }), }; -const initialState = Immutable.from({ - all: {}, - current: '', -}); - -export default (state = initialState, action) => { +export default (state = new GamesState(), action) => { switch (action.type) { case types.UPDATE_GAMES: - return Immutable.merge(state, { all: action.games }, { deep: true }); + return state.addGames(action.games); case types.ENTER_LOBBY: - return state.set('current', action.lobby.id); + return state.set('current', action.lobby.get('id')); default: return state; } }; -export const getAllGamesById = state => state.games.all; -export const getAllGames = state => { - let gamesById = getAllGamesById(state); - return Object.keys(gamesById).map(k => gamesById[k]); -}; -export const getGame = (state, id) => getAllGamesById(state)[id]; -export const getCurrentGame = state => getGame(state, state.games.current); +export const getAllGamesById = games => games.all; +export const getAllGames = games => getAllGamesById(games).toList(); +export const getGame = (games, id) => getAllGamesById(games).get(`${id}`); +export const getCurrentGame = games => getGame(games, games.current); 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)); |