2020-01-22 19:43:36 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { useParams } from 'react-router-dom';
|
2020-01-09 01:17:10 +00:00
|
|
|
import './Room.scss';
|
2020-01-22 19:43:36 +00:00
|
|
|
import socketIOClient from 'socket.io-client';
|
|
|
|
import config from '../../config';
|
2020-01-22 23:16:43 +00:00
|
|
|
import roomsServices from '../../services/api/roomsServices';
|
2020-01-23 22:45:48 +00:00
|
|
|
import GameButton from '../../components/Button/Game/Game';
|
|
|
|
import Message from '../../components/Display/Message/Message';
|
2020-01-24 05:41:25 +00:00
|
|
|
import ActionError from '../../components/Error/ActionError/ActionError';
|
2020-01-23 22:45:48 +00:00
|
|
|
|
|
|
|
import Development from '../../components/Display/Development/Development';
|
2020-01-22 19:43:36 +00:00
|
|
|
|
|
|
|
const Room = (props) => {
|
2020-01-23 00:40:56 +00:00
|
|
|
const state = props.state;
|
|
|
|
const dispatch = props.dispatch;
|
2020-01-22 23:16:43 +00:00
|
|
|
const roomId = parseInt(useParams().id) || 0;
|
2020-01-24 05:41:25 +00:00
|
|
|
const [ socketData, setSocketData ] = useState(false);
|
2020-01-22 23:16:43 +00:00
|
|
|
|
|
|
|
const fetchRoomAPI = async () => {
|
|
|
|
const response = await roomsServices.getRoomService(roomId);
|
|
|
|
if (response) {
|
2020-01-23 00:40:56 +00:00
|
|
|
const action = {
|
|
|
|
type: 'ROOMS',
|
|
|
|
message: 'JOIN_ROOM',
|
|
|
|
body: response
|
|
|
|
}
|
|
|
|
return dispatch(action);
|
2020-01-22 23:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-22 19:43:36 +00:00
|
|
|
|
2020-01-22 23:16:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
fetchRoomAPI();
|
|
|
|
}, [])
|
|
|
|
|
2020-01-22 19:43:36 +00:00
|
|
|
// ! [start] roomSocket
|
|
|
|
const roomSocket = socketIOClient(`${config.socketAddress}/${roomId}`)
|
|
|
|
|
|
|
|
const roomSocketConnect = () => {
|
|
|
|
roomSocket.emit('connect');
|
|
|
|
// ! dispatch data
|
2020-01-24 05:41:25 +00:00
|
|
|
roomSocket.on('connect', socket => {
|
|
|
|
setSocketData(true)
|
|
|
|
});
|
|
|
|
roomSocket.on('join_game_request', data => {
|
|
|
|
console.log(data)
|
|
|
|
})
|
|
|
|
roomSocket.on('connect_error', err => {
|
|
|
|
setSocketData(false)
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
roomSocket.on('error', err => {
|
|
|
|
setSocketData(false)
|
|
|
|
console.log(err);
|
|
|
|
});
|
2020-01-22 19:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
roomSocketConnect();
|
|
|
|
}, [])
|
2020-01-22 23:16:43 +00:00
|
|
|
|
2020-01-24 05:41:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const data = {
|
|
|
|
user: state.user,
|
|
|
|
game: state.joinGame
|
|
|
|
};
|
|
|
|
console.log('emitting request')
|
|
|
|
console.log(data)
|
|
|
|
roomSocket.emit('join_game_request', data)
|
|
|
|
}, [state.joinGame])
|
|
|
|
|
2020-01-22 19:43:36 +00:00
|
|
|
// ! [end]
|
2020-01-08 21:37:44 +00:00
|
|
|
|
2020-01-23 22:45:48 +00:00
|
|
|
const renderGames = () => {
|
|
|
|
const games = state.games || [];
|
|
|
|
if (games.length) {
|
|
|
|
return games.map(gameData => (
|
|
|
|
<GameButton
|
|
|
|
key={`game-${gameData.id}`}
|
|
|
|
game={gameData}
|
2020-01-24 05:41:25 +00:00
|
|
|
dispatch={dispatch}
|
2020-01-23 22:45:48 +00:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return <p>Loading Games...</p>
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderMessages = () => {
|
|
|
|
const messages = state.messages || [];
|
|
|
|
if (messages.length) {
|
|
|
|
return messages.map((messageData, idx) => (
|
|
|
|
<Message
|
|
|
|
key={`message-${idx}`}
|
|
|
|
message={messageData}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return <p>Loading Messages...</p>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-08 21:37:44 +00:00
|
|
|
return (
|
2020-01-16 05:59:05 +00:00
|
|
|
<div className="Room" data-testid="Room">
|
2020-01-24 05:41:25 +00:00
|
|
|
<div className="Room__heading">
|
|
|
|
<h2>{state.currentRoom ? state.currentRoom.name : 'Loading'}</h2>
|
|
|
|
<span className="Room__connection">{socketData ? '✓' : ' ⃠'}</span>
|
|
|
|
{state.errors.joinGame ? <ActionError error={state.errors.joinGame}/> : <></>}
|
|
|
|
</div>
|
2020-01-23 22:45:48 +00:00
|
|
|
|
|
|
|
<div className="Room__game-container">
|
|
|
|
{renderGames()}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="Room__message-container">
|
|
|
|
{renderMessages()}
|
|
|
|
<Development />
|
|
|
|
</div>
|
|
|
|
|
2020-01-16 05:59:05 +00:00
|
|
|
</div>
|
2020-01-08 21:37:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Room;
|