dispatch all room data to state upon rooms/:id

This commit is contained in:
Sorrel Bri 2020-01-22 16:40:56 -08:00 committed by sorrelbri
parent 40dbe487e1
commit 9c3fbb9572
6 changed files with 51 additions and 10 deletions

View file

@ -6,6 +6,8 @@ import config from '../../config';
import roomsServices from '../../services/api/roomsServices'; import roomsServices from '../../services/api/roomsServices';
const Room = (props) => { const Room = (props) => {
const state = props.state;
const dispatch = props.dispatch;
const roomId = parseInt(useParams().id) || 0; const roomId = parseInt(useParams().id) || 0;
const [ socketData, setSocketData ] = useState(); const [ socketData, setSocketData ] = useState();
const [ messages, setMessages ] = useState(); const [ messages, setMessages ] = useState();
@ -13,13 +15,12 @@ const Room = (props) => {
const fetchRoomAPI = async () => { const fetchRoomAPI = async () => {
const response = await roomsServices.getRoomService(roomId); const response = await roomsServices.getRoomService(roomId);
if (response) { if (response) {
console.log(response); const action = {
// const action = { type: 'ROOMS',
// type: 'ROOMS', message: 'JOIN_ROOM',
// message: 'JOIN_ROOM', body: response
// body: response }
// } return dispatch(action);
// return dispatch(action);
} }
} }
@ -31,7 +32,6 @@ const Room = (props) => {
const roomSocket = socketIOClient(`${config.socketAddress}/${roomId}`) const roomSocket = socketIOClient(`${config.socketAddress}/${roomId}`)
const roomSocketConnect = () => { const roomSocketConnect = () => {
console.log(roomId)
roomSocket.emit('connect'); roomSocket.emit('connect');
// ! dispatch data // ! dispatch data
roomSocket.on('connected', data => setSocketData('room socket connected')); roomSocket.on('connected', data => setSocketData('room socket connected'));

View file

@ -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;
}
}

View file

@ -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});
})

View file

@ -13,7 +13,7 @@ it('default returns state unaltered', () => {
expect(stateReducer(state, action)).toEqual(state); expect(stateReducer(state, action)).toEqual(state);
}) })
it('default returns state unaltered', () => { it('set messages returns state with messages', () => {
const state = initState(); const state = initState();
const action = {type: 'MESSAGES', message: 'SET_MESSAGES', body: messagesData}; const action = {type: 'MESSAGES', message: 'SET_MESSAGES', body: messagesData};
expect(stateReducer(state, action)).toEqual({...state, messages: messagesData}); expect(stateReducer(state, action)).toEqual({...state, messages: messagesData});

View file

@ -49,6 +49,5 @@ it('join room returns state with current room, games and messages all populated'
currentRoom: joinRoomData.currentRoom, currentRoom: joinRoomData.currentRoom,
messages: joinRoomData.messages, messages: joinRoomData.messages,
games: joinRoomData.roomGames games: joinRoomData.roomGames
}) })
}); });

View file

@ -5,6 +5,7 @@ import { errorReducer } from './err/stateReducer.err';
import { indexReducer } from './index/stateReducer.index'; import { indexReducer } from './index/stateReducer.index';
import { roomsReducer } from './rooms/stateReducer.rooms'; import { roomsReducer } from './rooms/stateReducer.rooms';
import { messagesReducer } from './messages/stateReducer.messages'; import { messagesReducer } from './messages/stateReducer.messages';
import { gamesReducer } from './games/stateReducer.games';
export type state = { export type state = {
user: {}, user: {},
@ -27,6 +28,9 @@ export const stateReducer = (state: state, action: action): state => {
case 'AUTH': case 'AUTH':
return authReducer(errorStrippedState, action); return authReducer(errorStrippedState, action);
case 'GAMES':
return gamesReducer(errorStrippedState, action);
case 'INDEX': case 'INDEX':
return indexReducer(errorStrippedState, action); return indexReducer(errorStrippedState, action);