Skip to content

Commit 1e82c57

Browse files
authored
Merge pull request #6 from boostcampwm-2024/feature/2
[FE] 베팅 관리자 페이지 리팩토링
2 parents 72d556c + b565f1e commit 1e82c57

File tree

3 files changed

+66
-59
lines changed

3 files changed

+66
-59
lines changed

frontend/src/features/betting-page-admin/index.tsx

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { BettingStatsDisplay } from "@/shared/components/BettingStatsDisplay/BettingStatsDisplay";
2-
import { useNavigate, useParams, useRouter } from "@tanstack/react-router";
2+
import {
3+
useNavigate,
4+
useRouteContext,
5+
useRouter,
6+
} from "@tanstack/react-router";
37
import React, { useCallback, useEffect, useRef, useState } from "react";
48
import { useSocketIO } from "@/shared/hooks/useSocketIo";
59
import { BettingTimer } from "@/shared/components/BettingTimer/BettingTimer";
@@ -9,27 +13,16 @@ import { endBetRoom, refund } from "./model/api";
913
import { useLayoutShift } from "@/shared/hooks/useLayoutShift";
1014
import { bettingRoomSchema } from "../betting-page/model/schema";
1115
import { DuckCoinIcon } from "@/shared/icons";
12-
import { useSuspenseQuery } from "@tanstack/react-query";
13-
import { bettingRoomQueryKey } from "@/shared/lib/bettingRoomInfo";
14-
import { getBettingRoomInfo } from "../betting-page/api/getBettingRoomInfo";
15-
import { responseBetRoomInfo } from "@betting-duck/shared";
16+
import BettingDetails from "./ui/BettingDetails";
1617

1718
function BettingPageAdmin() {
1819
useLayoutShift();
19-
const router = useRouter();
20-
const { roomId } = useParams({
21-
from: "/betting_/$roomId/vote",
22-
});
2320

24-
const { data } = useSuspenseQuery({
25-
queryKey: bettingRoomQueryKey(roomId),
26-
queryFn: () => getBettingRoomInfo(roomId),
27-
});
28-
const parsedData = responseBetRoomInfo.safeParse(data);
29-
if (!parsedData.success) {
30-
throw new Error("방 정보를 불러오는데 실패했습니다.");
31-
}
32-
const { channel } = parsedData.data;
21+
const context = useRouteContext({ from: "/betting_/$roomId/vote/admin" });
22+
const roomInfo = context.roomInfo;
23+
const channel = roomInfo.channel;
24+
25+
const router = useRouter();
3326

3427
const [status, setStatus] = useState(channel.status || "active");
3528
const [bettingInfo, setBettingInfo] = useState({
@@ -44,10 +37,12 @@ function BettingPageAdmin() {
4437
>(null);
4538

4639
// Room Information
47-
const option1 = channel.options.option1;
48-
const option2 = channel.options.option2;
49-
const defaultBetAmount = channel.settings.defaultBetAmount;
50-
const timer = channel.settings.duration;
40+
const {
41+
id: roomId,
42+
title,
43+
options: { option1, option2 },
44+
settings: { defaultBetAmount, duration: timer },
45+
} = channel;
5146

5247
const socket = useSocketIO({
5348
url: "/api/betting",
@@ -169,8 +164,8 @@ function BettingPageAdmin() {
169164

170165
const handleCancelClick = async () => {
171166
refund(roomId)
172-
.then((data) => {
173-
console.log("API 성공 결과:", data);
167+
.then(() => {
168+
sessionStorage.removeItem("userInfo");
174169
navigate({
175170
to: "/my-page",
176171
});
@@ -209,10 +204,6 @@ function BettingPageAdmin() {
209204
}
210205
};
211206

212-
const getTotalParticipants = () => {
213-
return bettingInfo.option1.participants + bettingInfo.option2.participants;
214-
};
215-
216207
const getTotalBetAmount = () => {
217208
return bettingInfo.option1.currentBets + bettingInfo.option2.currentBets;
218209
};
@@ -275,37 +266,14 @@ function BettingPageAdmin() {
275266
return (
276267
<div className="bg-layout-main flex h-full w-full flex-col justify-between">
277268
<div className="flex flex-col gap-5">
278-
<BettingTimer socket={socket} bettingRoomInfo={parsedData.data} />
269+
<BettingTimer socket={socket} bettingRoomInfo={roomInfo} />
279270
<div className="flex flex-col gap-6 p-5">
280-
<div className="bg-secondary mb-4 rounded-lg p-3 text-center shadow-inner">
281-
<h1 className="text-default-disabled text-md mb-1 font-bold">
282-
베팅 주제
283-
</h1>
284-
<h1 className="text-primary mb-1 pt-2 text-4xl font-extrabold">
285-
{channel.title}
286-
</h1>
287-
<p>
288-
{status === "active"
289-
? "투표가 진행 중입니다. 투표를 취소할 수 있습니다."
290-
: "투표가 종료되었습니다. 승부를 결정하세요!"}
291-
</p>
292-
<h1 className="text-default-disabled text-md mb-1 font-bold">
293-
베팅 정보
294-
</h1>
295-
<p>
296-
∙ 최소 베팅 금액:{" "}
297-
<span className="font-extrabold">{defaultBetAmount}</span>
298-
</p>
299-
<p>
300-
∙ 설정한 시간:{" "}
301-
<span className="font-extrabold">{timer / 60}</span>
302-
</p>
303-
<p>
304-
∙ 전체 베팅 참여자:{" "}
305-
<span className="font-extrabold">{getTotalParticipants()}</span>
306-
</p>
307-
</div>
308-
271+
<BettingDetails
272+
title={title}
273+
defaultBetAmount={defaultBetAmount}
274+
timer={timer}
275+
status={status}
276+
/>
309277
{status === "timeover" ? (
310278
<div className="flex w-full overflow-hidden rounded-xl border">
311279
<div className="relative flex w-1/2">
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function BettingDetails({
2+
title,
3+
defaultBetAmount,
4+
timer,
5+
status,
6+
}: {
7+
title: string;
8+
defaultBetAmount: number;
9+
timer: number;
10+
status: string;
11+
}) {
12+
return (
13+
<div className="bg-secondary mb-4 rounded-lg p-3 text-center shadow-inner">
14+
<h1 className="text-default-disabled text-md mb-1 font-bold">
15+
베팅 주제
16+
</h1>
17+
<h1 className="text-primary mb-1 pt-2 text-4xl font-extrabold">
18+
{title}
19+
</h1>
20+
<p>
21+
{status === "active"
22+
? "투표가 진행 중입니다. 투표를 취소할 수 있습니다."
23+
: "투표가 종료되었습니다. 승부를 결정하세요!"}
24+
</p>
25+
<h1 className="text-default-disabled text-md mb-1 font-bold">
26+
베팅 정보
27+
</h1>
28+
<p>
29+
∙ 최소 베팅 금액:{" "}
30+
<span className="font-extrabold">{defaultBetAmount}</span>
31+
</p>
32+
<p>
33+
∙ 설정한 시간: <span className="font-extrabold">{timer / 60}</span>
34+
</p>
35+
</div>
36+
);
37+
}
38+
39+
export default BettingDetails;

frontend/src/routes/betting_.$roomId.vote.admin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const Route = createFileRoute("/betting_/$roomId/vote/admin")({
1818
return { roomInfo };
1919
},
2020

21-
shouldReload: () => true,
21+
shouldReload: () => false,
2222
errorComponent: ({ error }) => {
2323
return <GlobalErrorComponent error={error} to="/" />;
2424
},

0 commit comments

Comments
 (0)