diff --git a/play-node-go/src/pages/Room/Room.js b/play-node-go/src/pages/Room/Room.js index 51073cb..c6c0719 100644 --- a/play-node-go/src/pages/Room/Room.js +++ b/play-node-go/src/pages/Room/Room.js @@ -6,6 +6,8 @@ import config from '../../config'; import roomsServices from '../../services/api/roomsServices'; const Room = (props) => { + const state = props.state; + const dispatch = props.dispatch; const roomId = parseInt(useParams().id) || 0; const [ socketData, setSocketData ] = useState(); const [ messages, setMessages ] = useState(); @@ -13,13 +15,12 @@ const Room = (props) => { const fetchRoomAPI = async () => { const response = await roomsServices.getRoomService(roomId); if (response) { - console.log(response); - // const action = { - // type: 'ROOMS', - // message: 'JOIN_ROOM', - // body: response - // } - // return dispatch(action); + const action = { + type: 'ROOMS', + message: 'JOIN_ROOM', + body: response + } + return dispatch(action); } } @@ -31,7 +32,6 @@ const Room = (props) => { const roomSocket = socketIOClient(`${config.socketAddress}/${roomId}`) const roomSocketConnect = () => { - console.log(roomId) roomSocket.emit('connect'); // ! dispatch data roomSocket.on('connected', data => setSocketData('room socket connected')); diff --git a/play-node-go/src/reducers/games/stateReducer.games.js b/play-node-go/src/reducers/games/stateReducer.games.js new file mode 100644 index 0000000..47d462e --- /dev/null +++ b/play-node-go/src/reducers/games/stateReducer.games.js @@ -0,0 +1,16 @@ +// @flow +import type { state, action } from '../stateReducer'; +import { stateReducer } from '../stateReducer'; + +export const gamesReducer = (state: state, action: action):state => { + switch(action.message) { + + case 'SET_GAMES': + const games = action.body; + return {...state, games}; + + + default: + return state; + } +} \ No newline at end of file diff --git a/play-node-go/src/reducers/games/stateReducer.games.test.js b/play-node-go/src/reducers/games/stateReducer.games.test.js new file mode 100644 index 0000000..5b7e966 --- /dev/null +++ b/play-node-go/src/reducers/games/stateReducer.games.test.js @@ -0,0 +1,22 @@ +import {stateReducer} from '../stateReducer'; +import { initState } from '../init/stateReducer.init'; + +const gamesData = [ + { + komi:6.5, handicap:0, board_size:19, + player_black:"anon", player_white:"anon", + player_black_rank:"K3", player_white_rank:"K2" + } +]; + +it('default returns state unaltered', () => { + const state = initState(); + const action = {type: 'GAMES', message: '', body: gamesData}; + expect(stateReducer(state, action)).toEqual(state); +}) + +it('set games returns state with games', () => { + const state = initState(); + const action = {type: 'GAMES', message: 'SET_GAMES', body: gamesData}; + expect(stateReducer(state, action)).toEqual({...state, games: gamesData}); +}) \ No newline at end of file diff --git a/play-node-go/src/reducers/messages/stateReducer.messages.test.js b/play-node-go/src/reducers/messages/stateReducer.messages.test.js index 25b45cd..2601efc 100644 --- a/play-node-go/src/reducers/messages/stateReducer.messages.test.js +++ b/play-node-go/src/reducers/messages/stateReducer.messages.test.js @@ -13,7 +13,7 @@ it('default returns state unaltered', () => { expect(stateReducer(state, action)).toEqual(state); }) -it('default returns state unaltered', () => { +it('set messages returns state with messages', () => { const state = initState(); const action = {type: 'MESSAGES', message: 'SET_MESSAGES', body: messagesData}; expect(stateReducer(state, action)).toEqual({...state, messages: messagesData}); diff --git a/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js b/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js index 8403fbd..bb78a0e 100644 --- a/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js +++ b/play-node-go/src/reducers/rooms/stateReducer.rooms.test.js @@ -49,6 +49,5 @@ it('join room returns state with current room, games and messages all populated' currentRoom: joinRoomData.currentRoom, messages: joinRoomData.messages, games: joinRoomData.roomGames - }) }); \ No newline at end of file diff --git a/play-node-go/src/reducers/stateReducer.js b/play-node-go/src/reducers/stateReducer.js index 1b27ee7..e642161 100644 --- a/play-node-go/src/reducers/stateReducer.js +++ b/play-node-go/src/reducers/stateReducer.js @@ -5,6 +5,7 @@ import { errorReducer } from './err/stateReducer.err'; import { indexReducer } from './index/stateReducer.index'; import { roomsReducer } from './rooms/stateReducer.rooms'; import { messagesReducer } from './messages/stateReducer.messages'; +import { gamesReducer } from './games/stateReducer.games'; export type state = { user: {}, @@ -27,6 +28,9 @@ export const stateReducer = (state: state, action: action): state => { case 'AUTH': return authReducer(errorStrippedState, action); + case 'GAMES': + return gamesReducer(errorStrippedState, action); + case 'INDEX': return indexReducer(errorStrippedState, action);