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 f94f4fd2c0
commit 38014ac497
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';
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'));

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

View file

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

View file

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