diff --git a/src/components/views/Lobby.tsx b/src/components/views/Lobby.tsx index 349aebc..4a8a3fe 100644 --- a/src/components/views/Lobby.tsx +++ b/src/components/views/Lobby.tsx @@ -9,12 +9,15 @@ 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; }; type RoomComponentProps = { room: Room; }; + type RoomListProps = { rooms: Room[]; }; @@ -128,12 +131,14 @@ const Lobby = () => { const roomCreationPopRef = useRef(null); const profilePopRef = useRef(null); const changeAvatarPopRef = useRef(null); + const infoPopRef = useRef(null); const [rooms, setRooms] = useState(mockRooms); const [user, setUser] = useState(mockRoomPlayers[0]); const [username, setUsername] = useState(null); const [avatar, setAvatar] = useState(null); - const [avatarToBeChanged, setAvatarToBeChanged] = useState(null); - const [avatarOrigin, setAvatarOrigin] = 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"); @@ -190,12 +195,34 @@ const Lobby = () => { if (error.response && error.response.data) { alert(error.response.data.message); } else { - console.error('Error:', error.message); - alert('An unexpected error occurred.'); + console.error("Error:", error.message); + alert("An unexpected error occurred."); } } }; + 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); + const roomId = response.roomId; + navigate(`/room=${roomId}`); + //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,6 +245,15 @@ 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 if (!changeAvatarPopRef.current) { @@ -229,10 +265,40 @@ const Lobby = () => { : changeAvatarPopRef.current.showModal(); }; - const changeAvatar = (avatar) =>{ - setAvatarToBeChanged(avatar); - updateAvatar(avatar); - toggleAvatarPop(); + const toggleInfoPop = () => { + // if the ref is not set, do nothing + if (!infoPopRef.current) { + return; + } + // if the dialog is open, close it. Otherwise, open it. + infoPopRef.current.hasAttribute("open") + ? infoPopRef.current.close() + : infoPopRef.current.showModal(); + }; + + const changeAvatar = async (newAvatar) =>{ + try { + // 更新本地状态 + setAvatar(newAvatar); + + // 构造请求体,只包含 avatar 更改 + const requestBody = JSON.stringify({ username, avatar: newAvatar }); + const id = sessionStorage.getItem("id"); + console.log("Request body:", requestBody); + // 执行更新请求 + await api.put(`/users/${id}`, requestBody); + + // 可能需要关闭弹窗或执行其他 UI 反馈 + + console.log("Avatar changed successfully"); + } catch (error) { + if (error.response && error.response.data) { + alert(error.response.data.message); + } else { + console.error("Error:", error.message); + alert("An unexpected error occurred."); + } + } } const updateAvatar = (newAvatar) => { @@ -245,9 +311,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) => (
@@ -271,6 +351,7 @@ const Lobby = () => { )); }; + return (
@@ -278,7 +359,7 @@ const Lobby = () => {
{user.username}
Kaeps
-
i
+
i
{/* for clip the scrollbar inside the border */}
@@ -296,19 +377,21 @@ const Lobby = () => {
{ toggleAvatarPop(); - toggleProfilePop(); - setAvatarOrigin(user.avatar); + // toggleProfilePop(); }}>
- + + setUsername(un)} - disabled={false} + onChange={e => setUsername(e)} /> +
Name: {user.name}
Status: {user.status}
@@ -319,7 +402,6 @@ const Lobby = () => {
@@ -340,8 +422,8 @@ const Lobby = () => { {avatarList?.map((avatar,index) => (
{ - changeAvatar(avatar); - toggleProfilePop(); + changeAvatar(avatar).then(r => toggleProfilePop()); + }}/>
))} @@ -355,23 +437,33 @@ const Lobby = () => { >
Create Room
- - + setRoomName(e.target.value)} /> + setNumRounds(parseInt(e.target.value, 10))} /> setRoomTheme(selectedOption.value)} />
- - + +
+ + + + +
Here is some Guidelines....
+
+ +
); diff --git a/src/styles/ui/Popup.scss b/src/styles/ui/Popup.scss index dfc32ca..0732e27 100644 --- a/src/styles/ui/Popup.scss +++ b/src/styles/ui/Popup.scss @@ -23,7 +23,7 @@ } } .popup-content { - height: 100%; // fill the height of the popup + height: 100%; padding: 20px; display: flex; flex-direction: column; diff --git a/src/styles/views/Lobby.scss b/src/styles/views/Lobby.scss index 594c4e1..b3cf60d 100644 --- a/src/styles/views/Lobby.scss +++ b/src/styles/views/Lobby.scss @@ -341,11 +341,11 @@ &.input { height: 35px; padding-left: 15px; - margin-left: -4px; + margin-left: 4px; border: none; border-radius: 0.75em; - //margin-top: 20px; - background: transparentize(white, 0.85); + margin-top: 5px; + background: #FFF3CF; color: $textColor; }