refactor api room endpoint to return unjoined game room data

This commit is contained in:
Sorrel Bri 2020-01-22 16:30:28 -08:00 committed by sorrelbri
parent 695d1ea2b1
commit f94f4fd2c0
4 changed files with 75 additions and 33 deletions

View file

@ -31,6 +31,7 @@ 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

@ -6,6 +6,9 @@ export const errorReducer = (state: state, action: action):state => {
case 'AUTH_ERROR':
return authErrorReducer(state, action);
case 'JOIN_ROOM_ERROR':
return joinRoomErrorReducer(state, action);
default:
return state;
}
@ -15,3 +18,8 @@ function authErrorReducer(state: state, action: action): state {
const auth = action.body.authError;
return {...state, errors: {auth} };
}
function joinRoomErrorReducer(state: state, action: action): state {
const joinRoom = action.body.joinRoomError;
return { ...state, errors: {joinRoom} }
}

View file

@ -9,23 +9,19 @@ export const roomsReducer = (state: state, action: action):state => {
const rooms = action.body;
return {...state, rooms};
case 'SET_CURRENT':
const currentRoom = action.body;
return {...state, currentRoom};
case 'JOIN_ROOM': {
// SET MESSAGES
const stateWithMessages = action.body.messages.length ? setMessages(state, action.body) : state;
const stateWithRoom = setCurrentRoom(state, action);
const stateWithMessages = setMessages(stateWithRoom, action);
if (!action.body.roomGames) {
return setJoinRoomError(state);
}
const stateWithGames = setGames(stateWithMessages, action);
// SET CURRENT ROOM
// if (!data.roomGames.length) {
// const errorAction = {
// type: 'ERR',
// message: 'JOIN_ROOM',
// body: { joinRoomError: 'Game room has no games' }
// }
// return stateReducer(stateWithMessages, errorAction);
// }
// SET GAMES
return stateWithMessages;
return stateWithGames;
}
@ -34,11 +30,47 @@ export const roomsReducer = (state: state, action: action):state => {
}
}
function setMessages(state, body) {
function setMessages(state, action) {
if(action.body.messages.length) {
const messages = action.body.messages;
const messageAction = {
type: 'MESSAGES',
message: 'SET_MESSAGES',
body: body.messages
body: messages
}
return stateReducer(state, messageAction)
return stateReducer(state, messageAction);
}
return state;
}
function setJoinRoomError(state, body) {
const errorAction = {
type: 'ERR',
message: 'JOIN_ROOM_ERROR',
body: { joinRoomError: 'Game room has no games' }
}
return stateReducer(state, errorAction);
}
function setCurrentRoom(state, action) {
const currentRoom = action.body.currentRoom;
const roomAction = {
type: 'ROOMS',
message: 'SET_CURRENT',
body: currentRoom
}
return stateReducer(state, roomAction);
}
function setGames(state, action) {
if (action.body.roomGames.length) {
const games = action.body.roomGames;
const gamesAction = {
type: 'GAMES',
message: 'SET_GAMES',
body: games
}
return stateReducer(state, gamesAction);
}
return state;
}

View file

@ -11,18 +11,20 @@ const roomsData = [
]
const joinRoomData = {
"roomGames": [
currentRoom: {
id:1, name:"main",
description:"A general place to play Go", language:"EN"
},
roomGames: [
{
"id":1, "name":"main",
"description":"A general place to play Go",
"language":"EN", "komi":6.5, "handicap":0, "board_size":19,
"player_black":"anon", "player_white":"anon",
"player_black_rank":"K3", "player_white_rank":"K2"
komi:6.5, handicap:0, board_size:19,
player_black:"anon", player_white:"anon",
player_black_rank:"K3", player_white_rank:"K2"
}
],
"messages": [
messages: [
{
"content": "Hey! Welcome to the general room!", "username": "userOne", "admin":true
content: "Hey! Welcome to the general room!", username: "userOne", admin: true
}
]
}
@ -42,12 +44,11 @@ it('set rooms returns state with rooms added', () => {
it('join room returns state with current room, games and messages all populated', () => {
const state = initState();
const action = {type: 'ROOMS', message: 'JOIN_ROOM', body: joinRoomData};
const normalizedRoomGames = joinRoomData.roomGames.map(game => {delete game.id; delete game.name; delete game.description; return game});
expect(stateReducer(state, action)).toEqual({
...state,
currentRoom: roomsData[0],
currentRoom: joinRoomData.currentRoom,
messages: joinRoomData.messages,
roomGames: normalizedRoomGames
games: joinRoomData.roomGames
})
});