Skip to content

Commit 72d556c

Browse files
authored
Merge pull request #5 from boostcampwm-2024/feature/2
�[FE] 베팅 관리자 페이지 라우팅 코드 리팩토링
2 parents 264d4a4 + 11337bc commit 72d556c

File tree

5 files changed

+75
-23
lines changed

5 files changed

+75
-23
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { BettingPageAdmin } from "@/features/betting-page-admin";
22
import { createFileRoute, redirect } from "@tanstack/react-router";
3-
import { getBettingRoomInfo } from "@/features/betting-page/api/getBettingRoomInfo";
43
import { GlobalErrorComponent } from "@/shared/components/Error/GlobalError";
4+
import { validateRoomAccess } from "@/shared/utils/roomValidation";
55

66
export const Route = createFileRoute("/betting_/$roomId/vote/admin")({
77
component: BettingPageAdmin,
88
beforeLoad: async ({ params }) => {
99
const { roomId } = params;
10-
const roomInfo = await getBettingRoomInfo(roomId);
11-
if (!roomInfo) {
12-
throw new Error("방 정보를 불러오는데 실패했습니다.");
13-
}
10+
const roomInfo = await validateRoomAccess(roomId);
1411

1512
if (!roomInfo.channel.isAdmin) {
1613
throw redirect({
1714
to: `/betting/${roomId}/vote/voting`,
1815
});
1916
}
17+
18+
return { roomInfo };
2019
},
2120

2221
shouldReload: () => true,

frontend/src/shared/api/responseBettingRoomInfo.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { responseBetRoomInfo } from "@betting-duck/shared";
2+
import { summaryResponseSchema } from "@betting-duck/shared/schemas/bet/socket/response";
23
import { EnsureQueryDataOptions } from "@tanstack/react-query";
34
import { z } from "zod";
45

@@ -26,6 +27,25 @@ async function responseBettingRoomInfo(roomId: string) {
2627
}
2728
}
2829

30+
async function responseSummaryBettingRoomInfo(roomId: string) {
31+
try {
32+
const response = await fetch(`/api/betrooms/${roomId}/summary`);
33+
if (!response.ok) {
34+
throw new Error("베팅 방 요약 정보를 불러오는데 실패했습니다.");
35+
}
36+
37+
const { data } = await response.json();
38+
const result = summaryResponseSchema.safeParse(data);
39+
if (!result.success) {
40+
console.error(result.error.errors);
41+
throw new Error("베팅 방 요약 정보를 파싱하는데 실패했습니다.");
42+
}
43+
return result.data;
44+
} catch (error) {
45+
console.error(error);
46+
}
47+
}
48+
2949
const betRoomQueries = (roomId: string): BetRoomInfo => ({
3050
roomInfo: {
3151
queryKey: ["betRoom", "info", roomId],
@@ -40,4 +60,8 @@ const betRoomQueries = (roomId: string): BetRoomInfo => ({
4060
},
4161
});
4262

43-
export { responseBettingRoomInfo, betRoomQueries };
63+
export {
64+
responseBettingRoomInfo,
65+
betRoomQueries,
66+
responseSummaryBettingRoomInfo,
67+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { responseBettingRoomInfo } from "../api/responseBettingRoomInfo";
2+
3+
export async function validateRoomAccess(roomId: string) {
4+
const roomInfo = await responseBettingRoomInfo(roomId);
5+
if (!roomInfo) throw new Error("방 정보를 불러올 수 없습니다.");
6+
return roomInfo;
7+
}

frontend/vite.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ export default defineConfig(({ mode }) => {
2727
changeOrigin: true,
2828
secure: false,
2929
ws: true,
30-
configure: (proxy) => {
31-
proxy.on("proxyRes", (proxyRes) => {
32-
proxyRes.headers["cache-control"] = "public, max-age=31536000";
33-
});
34-
},
3530
},
3631
},
3732
middlewareMode: env.NODE_ENV === "development" ? false : true,

shared/schemas/bet/socket/response.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { z } from "zod";
22
import { nicknameSchema } from "../../shared";
33

4+
const creatorSchema = z.object({
5+
id: z.number().int().positive(),
6+
});
7+
8+
const optionSchema = z.object({
9+
name: z.string(),
10+
});
11+
12+
const settingsSchema = z.object({
13+
defaultBetAmount: z.number().int().positive(),
14+
duration: z.number().int().positive(),
15+
});
16+
417
const responseFetchBetRoomInfoSchema = z.object({
518
channel: z.object({
619
id: z.string(),
@@ -19,14 +32,6 @@ const responseFetchBetRoomInfoSchema = z.object({
1932
}),
2033
});
2134

22-
const creatorSchema = z.object({
23-
id: z.number().int().positive(),
24-
});
25-
26-
const optionSchema = z.object({
27-
name: z.string(),
28-
});
29-
3035
const channelSchema = z.object({
3136
id: z.string(),
3237
title: z.string(),
@@ -36,10 +41,7 @@ const channelSchema = z.object({
3641
option2: optionSchema,
3742
}),
3843
status: z.enum(["waiting", "active", "timeover", "finished"]),
39-
settings: z.object({
40-
defaultBetAmount: z.number().int().positive(),
41-
duration: z.number().int().positive(),
42-
}),
44+
settings: settingsSchema,
4345
metadata: z.object({
4446
createdAt: z.string().datetime(),
4547
startAt: z.string().datetime().nullable(),
@@ -51,6 +53,26 @@ const channelSchema = z.object({
5153
isAdmin: z.boolean(),
5254
});
5355

56+
const summaryChannelSchema = z.object({
57+
title: z.string(),
58+
options: z.object({
59+
option1: optionSchema,
60+
option2: optionSchema,
61+
}),
62+
status: z.enum(["waiting", "active", "timeover", "finished"], {
63+
message: "status는 waiting, active, timeover, finished 중 하나여야 합니다.",
64+
}),
65+
settings: settingsSchema,
66+
});
67+
68+
const summaryResponseSchema = z.object({
69+
status: z.number().int().positive(),
70+
data: z.object({
71+
channel: channelSchema,
72+
message: z.string(),
73+
}),
74+
});
75+
5476
const responseBetRoomInfo = z.object({
5577
channel: channelSchema,
5678
message: z.string(),
@@ -60,9 +82,14 @@ type responseFetchBetRoomInfoType = z.infer<
6082
typeof responseFetchBetRoomInfoSchema
6183
>;
6284

85+
type summaryResponseType = z.infer<typeof summaryResponseSchema>;
86+
6387
export {
6488
channelSchema,
6589
responseFetchBetRoomInfoSchema,
6690
responseBetRoomInfo,
91+
summaryChannelSchema,
92+
summaryResponseSchema,
6793
type responseFetchBetRoomInfoType,
94+
type summaryResponseType,
6895
};

0 commit comments

Comments
 (0)