강동현

Merge branch 'master' of http://khuhub.khu.ac.kr/2020105578/nodejs-game into master

1 +import React from 'react';
2 +import { Footer } from './Footer';
3 +
4 +export const Main: React.FC = ({ children }) => {
5 + return (
6 + <div className="flex flex-col items-center w-screen h-screen">
7 + {children}
8 + <Footer/>
9 + </div>
10 + );
11 +}
...\ No newline at end of file ...\ No newline at end of file
1 +export interface RoomData {
2 + uuid: string;
3 + name: string;
4 + maxUsers: number;
5 + users: string[];
6 +}
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 }
......
1 import React, { useContext, useState } from 'react'; 1 import React, { useContext, useState } from 'react';
2 -import { RouteComponentProps } from 'react-router'; 2 +import { useHistory } from 'react-router';
3 -import { Footer } from '../components/common/Footer'; 3 +import { Main } from '../components/common/Main';
4 import { MessageResponse, MessageType } from '../components/common/types'; 4 import { MessageResponse, MessageType } from '../components/common/types';
5 import SocketContext from '../contexts/SocketContext'; 5 import SocketContext from '../contexts/SocketContext';
6 6
7 -export const Login: React.FC<RouteComponentProps> = ({ history }) => { 7 +export const Login: React.FC = () => {
8 + const history = useHistory();
8 const socket = useContext(SocketContext); 9 const socket = useContext(SocketContext);
9 const [ username, setUsername ] = useState(""); 10 const [ username, setUsername ] = useState("");
10 11
...@@ -19,7 +20,7 @@ export const Login: React.FC<RouteComponentProps> = ({ history }) => { ...@@ -19,7 +20,7 @@ export const Login: React.FC<RouteComponentProps> = ({ history }) => {
19 } 20 }
20 21
21 return ( 22 return (
22 - <div className="flex flex-col items-center w-screen h-screen"> 23 + <Main>
23 <div className="mt-auto flex flex-col items-center"> 24 <div className="mt-auto flex flex-col items-center">
24 <img className="m-7" src="./logo192.png"/> 25 <img className="m-7" src="./logo192.png"/>
25 <div> 26 <div>
...@@ -40,7 +41,6 @@ export const Login: React.FC<RouteComponentProps> = ({ history }) => { ...@@ -40,7 +41,6 @@ export const Login: React.FC<RouteComponentProps> = ({ history }) => {
40 onClick={() => login()}>Login</button> 41 onClick={() => login()}>Login</button>
41 </div> 42 </div>
42 </div> 43 </div>
43 - <Footer/> 44 + </Main>
44 - </div>
45 ) 45 )
46 } 46 }
......
1 import React, { useContext, useEffect, useState } from 'react'; 1 import React, { useContext, useEffect, useState } from 'react';
2 -import { RouteComponentProps } from 'react-router'; 2 +import { Main } from '../components/common/Main';
3 -import { Footer } from '../components/common/Footer';
4 import { MessageResponse, MessageType } from '../components/common/types'; 3 import { MessageResponse, MessageType } from '../components/common/types';
5 import { RoomInfo } from '../components/rooms/RoomInfo'; 4 import { RoomInfo } from '../components/rooms/RoomInfo';
6 import { Room } from '../components/rooms/types'; 5 import { Room } from '../components/rooms/types';
7 import SocketContext from '../contexts/SocketContext'; 6 import SocketContext from '../contexts/SocketContext';
8 7
9 -export const Rooms: React.FC<RouteComponentProps> = ({ history }) => { 8 +export const Rooms: React.FC = () => {
10 const socket = useContext(SocketContext); 9 const socket = useContext(SocketContext);
11 const [ rooms, setRooms ] = useState<Room[]>([]); 10 const [ rooms, setRooms ] = useState<Room[]>([]);
12 11
...@@ -42,11 +41,10 @@ export const Rooms: React.FC<RouteComponentProps> = ({ history }) => { ...@@ -42,11 +41,10 @@ export const Rooms: React.FC<RouteComponentProps> = ({ history }) => {
42 // useEffect(refreshRooms, []); 41 // useEffect(refreshRooms, []);
43 42
44 return ( 43 return (
45 - <div className='flex flex-col items-center w-screen h-screen'> 44 + <Main>
46 <div className='mt-auto w-screen flex flex-col items-center'> 45 <div className='mt-auto w-screen flex flex-col items-center'>
47 {rooms.map((room) => (<RoomInfo key={room.uuid} room={room} />))} 46 {rooms.map((room) => (<RoomInfo key={room.uuid} room={room} />))}
48 </div> 47 </div>
49 - <Footer /> 48 + </Main>
50 - </div>
51 ) 49 )
52 } 50 }
......