Showing
1 changed file
with
12 additions
and
10 deletions
| 1 | import React, { useContext, useEffect, useState } from 'react'; | 1 | import React, { useContext, useEffect, useState } from 'react'; |
| 2 | import { RouteComponentProps } from 'react-router'; | 2 | import { RouteComponentProps } from 'react-router'; |
| 3 | +import { MessageResponse, MessageType } from './Message'; | ||
| 3 | import SocketContext from './SocketContext'; | 4 | import SocketContext from './SocketContext'; |
| 4 | 5 | ||
| 5 | -interface room { | 6 | +interface Room { |
| 6 | uuid: string; | 7 | uuid: string; |
| 7 | name: string; | 8 | name: string; |
| 8 | currentUsers: number; | 9 | currentUsers: number; |
| ... | @@ -11,19 +12,20 @@ interface room { | ... | @@ -11,19 +12,20 @@ interface room { |
| 11 | 12 | ||
| 12 | export const Rooms: React.FC<RouteComponentProps> = ({ history }) => { | 13 | export const Rooms: React.FC<RouteComponentProps> = ({ history }) => { |
| 13 | const socket = useContext(SocketContext); | 14 | const socket = useContext(SocketContext); |
| 14 | - const [ rooms, setRooms ] = useState<room[]>([]); | 15 | + const [ rooms, setRooms ] = useState<Room[]>([]); |
| 15 | 16 | ||
| 16 | - useEffect(() => { | 17 | + const refreshRooms = () => { |
| 17 | - socket.emit('room_list', ({ ok } : { ok: boolean }) => { | 18 | + socket.emit(MessageType.ROOM_LIST_REQUEST, (response: MessageResponse<Room[]>) => { |
| 18 | - if (ok) { | 19 | + if (response.ok) { |
| 19 | - history.push('/rooms'); | 20 | + setRooms(response.result!); |
| 20 | } else { | 21 | } else { |
| 21 | - console.error('login error!'); // TODO: 팝업 에러? | 22 | + // TODO: 에러 핸들링 |
| 23 | + console.log("방 목록을 수신하지 못함"); | ||
| 24 | + } | ||
| 25 | + }); | ||
| 22 | } | 26 | } |
| 23 | - }); // TODO: interval마다 emit하거나 새로고침 버튼을 만들자 | ||
| 24 | 27 | ||
| 25 | - // socket.on('') | 28 | + useEffect(refreshRooms, []); |
| 26 | - }, []); | ||
| 27 | 29 | ||
| 28 | return ( | 30 | return ( |
| 29 | <div> | 31 | <div> | ... | ... |
-
Please register or login to post a comment