Skip to content

Commit a42e2e7

Browse files
committed
refactor: 주문 정보 계산 로직을 useOrderInfo 훅으로 분리
1 parent ce502de commit a42e2e7

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/features/Cart/components/OrderConfirm.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@ import { Text } from '@/shared/components/Text';
88
import Back from '../../../../public/Back.png';
99
import { StepProps } from '../../../shared/types/funnel';
1010
import { CartItem } from '../types/Cart.types';
11+
import { useOrderInfo } from '../hooks/useOrderInfo';
1112

1213
type OrderConfirmProps = {
1314
cartItems: CartItem[];
1415
} & StepProps;
1516

1617
export const OrderConfirm = ({ cartItems, onPrev }: OrderConfirmProps) => {
17-
const hasCheckCartLength = cartItems?.filter((item) => item.isChecked).length;
18-
const totalQuantity = cartItems?.reduce(
19-
(acc, item) => acc + (item.isChecked ? item.quantity : 0),
20-
0
21-
);
22-
const totalPrice = cartItems?.reduce(
23-
(acc, item) => acc + (item.isChecked ? item.product.price * item.quantity : 0),
24-
0
25-
);
18+
const { hasCheckCartLength, totalQuantity, totalPrice } = useOrderInfo(cartItems);
2619

2720
return (
2821
<>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CartItem } from '../types/Cart.types';
2+
3+
export const useOrderInfo = (cartItems: CartItem[] = []) => {
4+
const hasCheckCartLength = cartItems.filter((item) => item.isChecked).length;
5+
6+
const totalQuantity = cartItems.reduce(
7+
(acc, item) => acc + (item.isChecked ? item.quantity : 0),
8+
0
9+
);
10+
11+
const totalPrice = cartItems.reduce(
12+
(acc, item) => acc + (item.isChecked ? item.product.price * item.quantity : 0),
13+
0
14+
);
15+
16+
return {
17+
hasCheckCartLength,
18+
totalQuantity,
19+
totalPrice,
20+
};
21+
};

0 commit comments

Comments
 (0)