Overnap

방 접속 구현

1 -import React from 'react'; 1 +import React, { useContext } from 'react';
2 +import { useHistory } from 'react-router';
3 +import SocketContext from '../../contexts/SocketContext';
4 +import { MessageResponse, MessageType } from '../common/types';
5 +import { RoomData } from '../room/types';
2 import { Room } from './types'; 6 import { Room } from './types';
3 7
4 interface RoomProps { 8 interface RoomProps {
...@@ -6,16 +10,37 @@ interface RoomProps { ...@@ -6,16 +10,37 @@ interface RoomProps {
6 } 10 }
7 11
8 export const RoomInfo: React.FC<RoomProps> = ({ room }) => { 12 export const RoomInfo: React.FC<RoomProps> = ({ room }) => {
13 + const history = useHistory();
14 + const socket = useContext(SocketContext);
15 + const joinRoom = () => {
16 + if (room.currentUsers < room.maxUsers) {
17 + socket.emit(MessageType.ROOM_JOIN, (response: MessageResponse<RoomData>) => {
18 + if (response.ok) {
19 + history.push({
20 + pathname: '/' + room.uuid,
21 + state: {roomData: response.result!}
22 + });
23 + } else {
24 + //TODO: 에러 MODAL을 어케띄우지? 하위컴포넌트에서 훅을 쓰면 어떻게 되는지 확인
25 + }
26 + })
27 + } else {
28 + //TODO: 자리 꽉찼다는 MODAL
29 + }
30 + }
31 +
9 return ( 32 return (
10 - <div className={`flex items-center place-content-between m-2 w-5/6 33 + <button className={`flex items-center place-content-between m-2 w-5/6
11 ${room.currentUsers < room.maxUsers ? 34 ${room.currentUsers < room.maxUsers ?
12 'bg-white active:bg-green-100' : 35 'bg-white active:bg-green-100' :
13 'bg-gray-200 active:bg-gray-200'} 36 'bg-gray-200 active:bg-gray-200'}
14 - rounded shadow hover:shadow-md`}> 37 + rounded shadow hover:shadow-md
38 + outline-none focus:outline-none`}
39 + onClick={joinRoom}>
15 <span className='mt-2 mb-2 ml-3 text-gray-600 text-lg'>{room.name}</span> 40 <span className='mt-2 mb-2 ml-3 text-gray-600 text-lg'>{room.name}</span>
16 <span className='mt-2 mb-2 mr-3 text-gray-500 text-right'> 41 <span className='mt-2 mb-2 mr-3 text-gray-500 text-right'>
17 {room.currentUsers}/{room.maxUsers} 42 {room.currentUsers}/{room.maxUsers}
18 </span> 43 </span>
19 - </div> 44 + </button>
20 ); 45 );
21 } 46 }
......