From 683299ce77e69cbeedba9751ed6eeec9ab2757a6 Mon Sep 17 00:00:00 2001 From: Johnson Mao Date: Fri, 4 Oct 2024 22:37:41 +0800 Subject: [PATCH] refactor: room page logic --- containers/room/RoomListView.tsx | 61 ++++++++++++-------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/containers/room/RoomListView.tsx b/containers/room/RoomListView.tsx index c0fb30f9..8549430f 100644 --- a/containers/room/RoomListView.tsx +++ b/containers/room/RoomListView.tsx @@ -4,8 +4,6 @@ import { AxiosError } from "axios"; import { useTranslation } from "next-i18next"; import { - Room, - RoomEntryError, RoomType, getRooms, postRoomEntry, @@ -34,26 +32,16 @@ const RoomsListView: FC = ({ status }) => { fetch(getRooms({ page, perPage, status })), defaultPerPage: 20, }); - const [selectedRoom, setSelectedRoom] = useState(); + const [roomId, setRoomId] = useState(null); const [passwordValues, setPasswordValues] = useState(INIT_PASSWORD); const [isLoading, setIsLoading] = useState(false); - const router = useRouter(); const { Popup, firePopup } = usePopup(); - - const onSelectedRoom = (id: string) => { - const targetRoom = data.find((room) => room.id === id); - - if (targetRoom?.currentPlayers === targetRoom?.maxPlayers) { - firePopup({ title: t("room_is_full") }); - return; - } - - setSelectedRoom(targetRoom); - }; + const router = useRouter(); + const isLocked = data.find((room) => room.id === roomId)?.isLocked; const handleClose = () => { setPasswordValues(INIT_PASSWORD); - setSelectedRoom(undefined); + setRoomId(null); }; const handlePaste = (e: ClipboardEvent) => { @@ -67,10 +55,6 @@ const RoomsListView: FC = ({ status }) => { setPasswordValues(Array.from(pastePassword, (text) => text ?? "")); }; - const handleValues = (values: string[]) => { - setPasswordValues(values); - }; - useEffect(() => { async function fetchRoomEntry(_roomId: string) { setIsLoading(true); @@ -80,27 +64,26 @@ const RoomsListView: FC = ({ status }) => { return; } - fetch(postRoomEntry(_roomId, passwordValues.join(""))) - .then(() => { - router.push(`/rooms/${_roomId}`); - }) - .catch((err: AxiosError) => { + try { + await fetch(postRoomEntry(_roomId, passwordValues.join(""))); + router.push(`/rooms/${_roomId}`); + } catch (err) { + if (err instanceof AxiosError) { const msg = err.response?.data.message.replaceAll(" ", "_"); if (!msg) return firePopup({ title: "error!" }); firePopup({ title: t(msg) }); - }) - .finally(() => { - // setSelectedRoom(undefined); - setPasswordValues(INIT_PASSWORD); - isLoading && setIsLoading(false); - }); + } + } finally { + setPasswordValues(INIT_PASSWORD); + setIsLoading(false); + } } - if (!selectedRoom || isLoading) return; - if (!selectedRoom.isLocked || passwordValues.every((char) => char !== "")) { - fetchRoomEntry(selectedRoom.id); + if (!roomId || isLoading) return; + if (!isLocked || passwordValues.every((char) => char !== "")) { + fetchRoomEntry(roomId); } - }, [selectedRoom, passwordValues, isLoading, router, fetch, firePopup, t]); + }, [roomId, passwordValues, isLoading, router, fetch, firePopup, t]); const Pagination = () => { return ( @@ -129,8 +112,8 @@ const RoomsListView: FC = ({ status }) => { ))} @@ -138,10 +121,10 @@ const RoomsListView: FC = ({ status }) => {