refactor api room endpoint to return unjoined game room data
This commit is contained in:
parent
41d64135e5
commit
a33fea6ba6
4 changed files with 75 additions and 33 deletions
|
@ -31,6 +31,7 @@ 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'));
|
||||||
|
|
|
@ -6,6 +6,9 @@ export const errorReducer = (state: state, action: action):state => {
|
||||||
case 'AUTH_ERROR':
|
case 'AUTH_ERROR':
|
||||||
return authErrorReducer(state, action);
|
return authErrorReducer(state, action);
|
||||||
|
|
||||||
|
case 'JOIN_ROOM_ERROR':
|
||||||
|
return joinRoomErrorReducer(state, action);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -15,3 +18,8 @@ function authErrorReducer(state: state, action: action): state {
|
||||||
const auth = action.body.authError;
|
const auth = action.body.authError;
|
||||||
return {...state, errors: {auth} };
|
return {...state, errors: {auth} };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function joinRoomErrorReducer(state: state, action: action): state {
|
||||||
|
const joinRoom = action.body.joinRoomError;
|
||||||
|
return { ...state, errors: {joinRoom} }
|
||||||
|
}
|
|
@ -9,23 +9,19 @@ export const roomsReducer = (state: state, action: action):state => {
|
||||||
const rooms = action.body;
|
const rooms = action.body;
|
||||||
return {...state, rooms};
|
return {...state, rooms};
|
||||||
|
|
||||||
|
case 'SET_CURRENT':
|
||||||
|
const currentRoom = action.body;
|
||||||
|
return {...state, currentRoom};
|
||||||
|
|
||||||
case 'JOIN_ROOM': {
|
case 'JOIN_ROOM': {
|
||||||
// SET MESSAGES
|
const stateWithRoom = setCurrentRoom(state, action);
|
||||||
const stateWithMessages = action.body.messages.length ? setMessages(state, action.body) : state;
|
const stateWithMessages = setMessages(stateWithRoom, action);
|
||||||
|
if (!action.body.roomGames) {
|
||||||
|
return setJoinRoomError(state);
|
||||||
|
}
|
||||||
|
const stateWithGames = setGames(stateWithMessages, action);
|
||||||
|
|
||||||
// SET CURRENT ROOM
|
return stateWithGames;
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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 = {
|
const messageAction = {
|
||||||
type: 'MESSAGES',
|
type: 'MESSAGES',
|
||||||
message: 'SET_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;
|
||||||
}
|
}
|
|
@ -11,18 +11,20 @@ const roomsData = [
|
||||||
]
|
]
|
||||||
|
|
||||||
const joinRoomData = {
|
const joinRoomData = {
|
||||||
"roomGames": [
|
currentRoom: {
|
||||||
|
id:1, name:"main",
|
||||||
|
description:"A general place to play Go", language:"EN"
|
||||||
|
},
|
||||||
|
roomGames: [
|
||||||
{
|
{
|
||||||
"id":1, "name":"main",
|
komi:6.5, handicap:0, board_size:19,
|
||||||
"description":"A general place to play Go",
|
player_black:"anon", player_white:"anon",
|
||||||
"language":"EN", "komi":6.5, "handicap":0, "board_size":19,
|
player_black_rank:"K3", player_white_rank:"K2"
|
||||||
"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', () => {
|
it('join room returns state with current room, games and messages all populated', () => {
|
||||||
const state = initState();
|
const state = initState();
|
||||||
const action = {type: 'ROOMS', message: 'JOIN_ROOM', body: joinRoomData};
|
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({
|
expect(stateReducer(state, action)).toEqual({
|
||||||
...state,
|
...state,
|
||||||
currentRoom: roomsData[0],
|
currentRoom: joinRoomData.currentRoom,
|
||||||
messages: joinRoomData.messages,
|
messages: joinRoomData.messages,
|
||||||
roomGames: normalizedRoomGames
|
games: joinRoomData.roomGames
|
||||||
|
|
||||||
})
|
})
|
||||||
});
|
});
|
Loading…
Reference in a new issue