import useTitle from "../../utils/hooks/TitleHook";
import React from "react";

const ROOM_URL = `http://${process.env.REACT_APP_API_URL}/room`;

const Rooms = () => {

    useTitle('Mau-Mau Rooms');

    const [rooms, setRooms] = React.useState<string[]>([]);

    React.useEffect(() => {
        fetch(ROOM_URL)
            .then(r => r.json())
            .then(data => setRooms(data));
    }, []);

    const gotoRoom = (room: string) => {
        window.location.host = `/room/${room}`;
    }

    return (
        <div>
            <h1>Rooms</h1>
            <ul>
                {
                    rooms.map((room, index) => {
                        return <li key={index} className={"clickable"}
                                   onClick={() => gotoRoom(room)}>{room}</li>
                    })
                }
            </ul>
        </div>
    );
};

export default Rooms;
