File tree Expand file tree Collapse file tree 2 files changed +19
-12
lines changed
entities/products/components/hooks Expand file tree Collapse file tree 2 files changed +19
-12
lines changed Original file line number Diff line number Diff line change 11import { useEffect } from "react" ;
22import { loadProductDetailForPage } from "../../productUseCase" ;
3- import { BASE_URL } from "../../../../constants " ;
3+ import { useRouterParams } from "../../../../router " ;
44
55export 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 ( / ^ \/ p r o d u c t \/ ( \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} ;
Original file line number Diff line number Diff line change @@ -3,4 +3,17 @@ import { Router } from "@hanghae-plus/lib";
33import { BASE_URL } from "../constants" ;
44import 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 ( ) ;
You can’t perform that action at this time.
0 commit comments