Rooms.tsx 1.35 KB
import React, { useContext, useEffect, useState } from 'react';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType } from '../components/common/types';
import { RoomBlock } from '../components/rooms/RoomBlock';
import { Room } from '../components/rooms/types';
import SocketContext from '../contexts/SocketContext';

export const Rooms: React.FC = () => {
  const socket = useContext(SocketContext);
  const [ rooms, setRooms ] = useState<Room[]>([]);

  // for test
  const testingRoom1: Room = {
    uuid: '23525',
    name: 'Hello World!',
    currentUsers: 3,
    maxUsers: 4
  }
  const testingRoom2: Room = {
    uuid: '235252134',
    name: 'Bonjour World!',
    currentUsers: 6,
    maxUsers: 6
  }
  useEffect(() => {
    setRooms([testingRoom1, testingRoom2]);
    // refreshRooms()
  }, []);

  const refreshRooms = () => {
    socket.emit(MessageType.ROOM_LIST_REQUEST, (response: MessageResponse<Room[]>) => {
      if (response.ok) {
        setRooms(response.result!);
      } else {
        // TODO: 에러 핸들링
        console.log("방 목록을 수신하지 못함");
      }
    });
  }

  // useEffect(refreshRooms, []);

  return (
    <Main>
      <div className='mt-auto w-screen flex flex-col items-center'>
        {rooms.map((room) => (<RoomBlock key={room.uuid} room={room} />))}
      </div>
    </Main>
  )
}