Skip to content

Commit a4e79ac

Browse files
committed
feat: csr에서만 loadProductDetail
1 parent 03b4503 commit a4e79ac

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { useEffect } from "react";
22
import { loadProductDetailForPage } from "../../productUseCase";
3-
import { BASE_URL } from "../../../../constants";
3+
import { useRouterParams } from "../../../../router";
44

55
export const useLoadProductDetail = () => {
6+
const productId = useRouterParams((params) => params.id);
67
useEffect(() => {
7-
if (typeof window === "undefined") return;
8-
const rawPath = window.location.pathname;
9-
const base = BASE_URL.replace(/\/$/, "");
10-
const normalizedPath = base && rawPath.startsWith(base) ? rawPath.slice(base.length) || "/" : rawPath;
11-
const match = normalizedPath.match(/^\/product\/(\d+)(?:\/?|\?|$)/);
12-
const productId = match ? match[1] : undefined;
13-
if (productId) {
14-
loadProductDetailForPage(productId);
15-
}
16-
}, []);
8+
if (!productId) return;
9+
loadProductDetailForPage(productId);
10+
}, [productId]);
1711
};

packages/react/src/router/router.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,17 @@ import { Router } from "@hanghae-plus/lib";
33
import { BASE_URL } from "../constants";
44
import type { FunctionComponent } from "react";
55

6-
export const router = new Router<FunctionComponent>(BASE_URL);
6+
const createRouter = () => ({
7+
push: () => {},
8+
addRoute: () => {},
9+
start: () => {},
10+
subscribe: () => () => {},
11+
get route() {
12+
return { path: "/" } as const;
13+
},
14+
get query() {
15+
return {} as const;
16+
},
17+
});
18+
19+
export const router = typeof window !== "undefined" ? new Router<FunctionComponent>(BASE_URL) : createRouter();

0 commit comments

Comments
 (0)