Skip to content

Commit b273ff0

Browse files
committed
feat: 페이지 내비게이션 로직을 usePageNavigation 훅으로 분리 및 개선
1 parent 59fb48a commit b273ff0

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

src/pages/cart/CartContent/CartContent.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import PriceContainer from './PriceContainer/PriceContainer';
33
import CartList from './CartList/CartList';
44
import CheckBox from '@/shared/components/CheckBox/CheckBox';
55
import * as S from './CartContent.styled';
6-
import { useNavigate } from 'react-router-dom';
7-
import { ROUTES } from '@/shared/config/routes';
86
import LoadingContainer from '@/shared/components/LoadingContainer/LoadingContainer';
97
import ErrorContainer from '@/shared/components/ErrorContainer/ErrorContainer';
108
import { useCartContext } from '../contexts/CartContext';
119
import { useOrderSelection } from '../hooks/useOrderSelection';
1210
import { useOrderCalculation } from '../hooks/useOrderCalculation';
11+
import { usePageNavigation } from '@/shared/hooks/usePageNavigation';
12+
import { ROUTES } from '@/shared/config/routes';
1313

1414
export default function CartContent() {
1515
const { cartItems, isLoading, errorMessage } = useCartContext();
@@ -20,7 +20,7 @@ export default function CartContent() {
2020
orderIdList,
2121
);
2222

23-
const navigate = useNavigate();
23+
const { navigateTo } = usePageNavigation();
2424

2525
if (isLoading && !cartItems?.length) {
2626
return <LoadingContainer />;
@@ -36,13 +36,7 @@ export default function CartContent() {
3636

3737
const handleOrderConfirmButtonClick = () => {
3838
const orderList = cartItems?.filter((item) => orderIdList.includes(item.id)) ?? [];
39-
40-
navigate(ROUTES.ORDER_SUCCESS, {
41-
state: {
42-
orderList,
43-
paymentAmount,
44-
},
45-
});
39+
navigateTo(ROUTES.ORDER_SUCCESS, { orderList, paymentAmount });
4640
};
4741

4842
return (

src/pages/order/OrderSuccessPage.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import BackButton from '@/shared/components/BackButton/BackButton';
22
import * as S from './OrderSuccessPage.styled';
33
import Header from '@/shared/components/Header/Header';
4-
import { useLocation, useNavigate } from 'react-router-dom';
54
import { useEffect } from 'react';
5+
import { usePageNavigation } from '@/shared/hooks/usePageNavigation';
66
import { ROUTES } from '@/shared/config/routes';
7-
import { CartItemType } from '@/apis/cartItems/cartItem.type';
8-
9-
interface OrderSuccessState {
10-
orderList: CartItemType[];
11-
paymentAmount: number;
12-
}
137

148
export default function OrderSuccessPage() {
15-
const navigate = useNavigate();
16-
const location = useLocation();
17-
const { orderList, paymentAmount } = location.state as OrderSuccessState;
18-
const orderListType = orderList.length;
19-
const orderQuantity = orderList.reduce((acc, { quantity }) => (acc += quantity), 0);
9+
const { validateOrderSuccessState, navigateTo } = usePageNavigation();
2010

2111
useEffect(() => {
22-
if (!location.state) {
23-
navigate(ROUTES.CART);
12+
const state = validateOrderSuccessState();
13+
if (!state) {
14+
navigateTo(ROUTES.CART);
15+
return;
2416
}
2517
}, []);
2618

19+
const state = validateOrderSuccessState();
20+
if (!state) return null;
21+
22+
const { orderList, paymentAmount } = state;
23+
const orderListType = orderList.length;
24+
const orderQuantity = orderList.reduce((acc, { quantity }) => (acc += quantity), 0);
25+
2726
return (
2827
<>
2928
<Header>

src/shared/hooks/usePageNavigation.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useLocation, useNavigate } from 'react-router-dom';
2+
import { CartItemType } from '@/apis/cartItems/cartItem.type';
3+
4+
interface OrderSuccessState {
5+
orderList: CartItemType[];
6+
paymentAmount: number;
7+
}
8+
9+
type RouteState = OrderSuccessState | undefined;
10+
11+
export const usePageNavigation = () => {
12+
const navigate = useNavigate();
13+
const location = useLocation();
14+
15+
const navigateTo = <T extends RouteState>(route: string, state?: T) => {
16+
navigate(route, state ? { state } : undefined);
17+
};
18+
19+
const validateOrderSuccessState = (): OrderSuccessState | null => {
20+
if (!location.state) return null;
21+
22+
const state = location.state as OrderSuccessState;
23+
if (
24+
!state.orderList ||
25+
!Array.isArray(state.orderList) ||
26+
typeof state.paymentAmount !== 'number'
27+
) {
28+
return null;
29+
}
30+
31+
return state;
32+
};
33+
34+
return {
35+
navigateTo,
36+
validateOrderSuccessState,
37+
};
38+
};

0 commit comments

Comments
 (0)