From 91f2a61a7f5376c742da791523814bb4839b01ee Mon Sep 17 00:00:00 2001 From: Victor Chabbert Date: Sun, 22 Jan 2017 13:45:26 +0100 Subject: Refactor user reducer to player reducer --- frontend/src/containers/app.js | 2 +- frontend/src/containers/home.js | 4 +++- frontend/src/reducers.js | 4 ++-- frontend/src/redux/players.js | 36 ++++++++++++++++++++++++++++++++++++ frontend/src/redux/user.js | 34 ---------------------------------- frontend/src/sagas/gameBrowser.js | 6 +++--- frontend/src/sagas/usernameChoice.js | 2 +- 7 files changed, 46 insertions(+), 42 deletions(-) create mode 100644 frontend/src/redux/players.js delete mode 100644 frontend/src/redux/user.js (limited to 'frontend/src') diff --git a/frontend/src/containers/app.js b/frontend/src/containers/app.js index adfc557f..40602efc 100644 --- a/frontend/src/containers/app.js +++ b/frontend/src/containers/app.js @@ -49,7 +49,7 @@ class App extends Component { } const mapStateToProps = (state) => ({ - username: state.user.get('displayName') + username: state.players.get('all').get(state.players.get('current')).get('username') }) diff --git a/frontend/src/containers/home.js b/frontend/src/containers/home.js index cc35f2bc..fe9bd373 100644 --- a/frontend/src/containers/home.js +++ b/frontend/src/containers/home.js @@ -22,6 +22,8 @@ class HomePage extends Component { onChange={(e) => this._username = e.target.value} onClick={this.play} /> + /games +
Take me somewhere ) @@ -32,7 +34,7 @@ const mapStateToProps = (state) => ({ }) -import { actions } from '../redux/user' +import { actions } from '../redux/players' const mapDispatchToProps = { chooseUsername: actions.chooseUsername } diff --git a/frontend/src/reducers.js b/frontend/src/reducers.js index 5cc01ec9..c280c8a7 100644 --- a/frontend/src/reducers.js +++ b/frontend/src/reducers.js @@ -2,12 +2,12 @@ import { combineReducers } from 'redux' import { routerReducer } from 'react-router-redux' import gamesReducer from './redux/game' -import userReducer from './redux/user' +import playersReducer from './redux/players' export default function createReducer() { return combineReducers({ games: gamesReducer, routing: routerReducer, - user: userReducer, + players: playersReducer, }) } diff --git a/frontend/src/redux/players.js b/frontend/src/redux/players.js new file mode 100644 index 00000000..e49d4bbf --- /dev/null +++ b/frontend/src/redux/players.js @@ -0,0 +1,36 @@ +import { fromJS } from 'immutable' + +export const types = { + SET_USERNAME: 'USER/SET_USERNAME', + CHOOSE_USERNAME: 'USER/CHOOSE_USERNAME' +} + +export const actions = { + setUsername: (username, displayName, index) => ({ + type: types.SET_USERNAME, + username, + index, + displayName + }), + chooseUsername: (username) => ({ type: types.CHOOSE_USERNAME, username }), +} + + +const initialState = fromJS({ + all: {}, + current: null +}) + +export default (state = initialState, action) => { + switch (action.type) { + case types.SET_USERNAME: + const user = fromJS({ + username: action.username, + displayName: action.displayName, + index: action.index, + }) + return state.setIn(['all', user.get('username')], user).set('current', user.get('username')) + default: + return state + } +} diff --git a/frontend/src/redux/user.js b/frontend/src/redux/user.js deleted file mode 100644 index 9046cc97..00000000 --- a/frontend/src/redux/user.js +++ /dev/null @@ -1,34 +0,0 @@ -import { fromJS } from 'immutable' - -export const types = { - SET_USERNAME: 'USER/SET_USERNAME', - CHOOSE_USERNAME: 'USER/CHOOSE_USERNAME' -} - -export const actions = { - setUsername: (username, displayName, index) => ({ - type: types.SET_USERNAME, - username, - index, - displayName - }), - chooseUsername: (username) => ({ type: types.CHOOSE_USERNAME, username }), -} - - -const initialState = fromJS({ - username: '', - displayName: '', - id: null -}) - -export default (state = initialState, action) => { - switch (action.type) { - case types.SET_USERNAME: - return state.set('username', action.username) - .set('displayName', action.displayName) - .set('index', action.index) - default: - return state - } -} diff --git a/frontend/src/sagas/gameBrowser.js b/frontend/src/sagas/gameBrowser.js index 19d52df9..5b7227df 100644 --- a/frontend/src/sagas/gameBrowser.js +++ b/frontend/src/sagas/gameBrowser.js @@ -25,7 +25,7 @@ function gameBrowserChannel(socket) { }) } -export function* watchGames({ socket }) { +export function *watchGames({ socket }) { const socketChannel = gameBrowserChannel(socket) try { @@ -50,13 +50,13 @@ export function* watchGames({ socket }) { } } -export function* createGame({ socket }) { +export function *createGame({ socket }) { const { name } = yield take(types.CREATE_GAME) socket.send("/app/lobby/create", JSON.stringify({ gameName: name }), {}) } -export function* gameBrowserSaga(socketConnection) { +export function *gameBrowserSaga(socketConnection) { yield [ call(watchGames, socketConnection), call(createGame, socketConnection) diff --git a/frontend/src/sagas/usernameChoice.js b/frontend/src/sagas/usernameChoice.js index d0d0cdea..e9812c72 100644 --- a/frontend/src/sagas/usernameChoice.js +++ b/frontend/src/sagas/usernameChoice.js @@ -2,7 +2,7 @@ import { call, take, put } from 'redux-saga/effects' import { eventChannel } from 'redux-saga' import { push } from 'react-router-redux' -import { actions, types } from '../redux/user' +import { actions, types } from '../redux/players' function usernameValidationChannel(socket) { return eventChannel(emitter => { -- cgit