diff --git a/src/components/views/Lobby.tsx b/src/components/views/Lobby.tsx index 4b3e3f4..fbaad30 100644 --- a/src/components/views/Lobby.tsx +++ b/src/components/views/Lobby.tsx @@ -9,6 +9,8 @@ import "styles/views/Lobby.scss"; import { User, Room } from "types"; import Popup from "components/ui/Popup"; import { Dropdown } from "components/ui/Dropdown"; + + type PlayerProps = { user: User; }; @@ -133,7 +135,9 @@ const Lobby = () => { const [user, setUser] = useState(mockRoomPlayers[0]); const [username, setUsername] = useState(null); const [avatar, setAvatar] = useState(null); - + const [roomName, setRoomName] = useState(''); + const [numRounds, setNumRounds] = useState(0); + const [roomTheme, setRoomTheme] = useState(''); const logout = async () => { const id = sessionStorage.getItem("id"); sessionStorage.removeItem("token"); @@ -196,6 +200,26 @@ const Lobby = () => { } }; + const createRoom = async () => { + try { + const ownerId = sessionStorage.getItem('id'); // 假设ownerId存储在sessionStorage中 + const requestBody = JSON.stringify({ + name: roomName, + num: numRounds, + ownerId: ownerId, + theme: roomTheme + }); + + const response = await api.post('/games', requestBody); + console.log('Room created successfully:', response); + toggleRoomCreationPop(); // 关闭创建房间的弹窗 + } catch (error) { + console.error('Error creating room:', handleError(error)); + alert(`Error creating room: ${handleError(error)}`); + } + }; + + const toggleRoomCreationPop = () => { // if the ref is not set, do nothing if (!roomCreationPopRef.current) { @@ -218,7 +242,14 @@ const Lobby = () => { : profilePopRef.current.showModal(); }; - + async function enterRoom(roomId, userId) { + try { + const requestBody = JSON.stringify({ userId, roomId }); + await api.put('/games', requestBody); + } catch (error) { + console.error(`Something went wrong during the enterRoom: \n${handleError(error)}`); + } + } const toggleAvatarPop = () => { // if the ref is not set, do nothing @@ -277,9 +308,23 @@ const Lobby = () => { const userinfo = () => { return; }; + + const renderRoomLists = () => { return mockRooms.map((Room) => ( -
+
{ + e.preventDefault(); + const currentId = sessionStorage.getItem('id'); + // const isPlayerInRoom = Room.roomPlayersList.join().includes(currentId); + enterRoom(Room.id, currentId) + .then(() => { + navigate(`/room=${Room.id}`); + }) + .catch(error => { + console.error(`Something went wrong during the enterRoom: \n${handleError(error)}`); + alert(`Something went wrong during the enterRoom: \n${handleError(error)}`); + }); + }}>
{Room.roomPlayersList?.map((user, index) => (
@@ -332,14 +377,25 @@ const Lobby = () => { }}>
- setUsername(un)}*/} + {/* disabled={false}*/} + {/*/>*/} +
+ + setUsername(un)} - disabled={false} + onChange={e => setUsername(e)} /> +
Name: {user.name}
Status: {user.status}
@@ -385,23 +441,24 @@ const Lobby = () => { >
Create Room
- - + setRoomName(e.target.value)} /> + setNumRounds(parseInt(e.target.value, 10))} /> setRoomTheme(selectedOption.value)} />
- - + +
+