Skip to content

Commit 8ba0a0e

Browse files
committed
refactor : 로직이 반복되는 부가정보 map 객체로 리팩토링
1 parent 87a7abb commit 8ba0a0e

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/pages/product-info/components/product-main-info/product-main-info.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { addComma } from "@/shared/utils/add-comma";
44
import { MEMBERSHIP_DATA, PAY_BENEFITS_DATA } from "@/shared/constants/benefit";
55
import { PayBenefit } from "@/shared/components/benefit/pay-benefit";
66
import { MoreBenefit } from "@/shared/components/benefit/more-benefit";
7-
import { ChevronRightRounded, Share, Star } from "@/assets/svg";
7+
import { ChevronRightRounded, Share } from "@/assets/svg";
88
import { Text } from "../text/text";
9-
interface ProductMainInfoProps {
9+
import { getProductStatsList } from "../../utils/get-product-stats";
10+
export interface ProductMainInfoProps {
1011
authorName: string;
1112
productName: string;
1213
originalPrice: number;
@@ -18,6 +19,8 @@ interface ProductMainInfoProps {
1819
}
1920

2021
export const ProductMainInfo = ({ data }: { data: ProductMainInfoProps }) => {
22+
const statsList = getProductStatsList(data); // 제품 부가정보 데이터 포맷팅 함수
23+
2124
return (
2225
<div className={styles.container}>
2326
{/** 제품 메인 정보 */}
@@ -80,28 +83,20 @@ export const ProductMainInfo = ({ data }: { data: ProductMainInfoProps }) => {
8083
{/** 제품 부가 정보 */}
8184
<div className={styles.extraInfo}>
8285
<div className={styles.flexRow}>
83-
<div className={styles.extraInfoItem}>
84-
<Star />
85-
<Text type="caption" color="black-200">
86-
{data.averageScore}
87-
</Text>
88-
</div>
89-
<div className={styles.extraInfoItem}>
90-
<Text type="caption" color="gray-300">
91-
후기
92-
</Text>
93-
<Text type="caption" color="gray-100">
94-
{addComma(String(data.reviewCount))}
95-
</Text>
96-
</div>
97-
<div className={styles.extraInfoItem}>
98-
<Text type="caption" color="gray-300">
99-
구매
100-
</Text>
101-
<Text type="caption" color="gray-100">
102-
{addComma(String(data.salesCount))}
103-
</Text>
104-
</div>
86+
{statsList.map((stat, index) => (
87+
<div key={index} className={styles.extraInfoItem}>
88+
{typeof stat.label === "string" ? (
89+
<Text type="caption" color="gray-300">
90+
{stat.label}
91+
</Text>
92+
) : (
93+
stat.label
94+
)}
95+
<Text type="caption" color={stat.color}>
96+
{stat.value}
97+
</Text>
98+
</div>
99+
))}
105100
</div>
106101
<button type="button" aria-label="공유하기">
107102
<Share />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { ReactNode } from "react";
2+
import { Star } from "@/assets/svg";
3+
import { addComma } from "@/shared/utils/add-comma";
4+
import type { ProductMainInfoProps as ProductData } from "../components/product-main-info/product-main-info";
5+
6+
interface StatItem {
7+
label: string | ReactNode;
8+
value: string | number;
9+
color: "black-200" | "gray-100";
10+
}
11+
12+
// 별점, 후기, 구매 수 동적 데이터 반환
13+
export const getProductStatsList = (data: ProductData): StatItem[] => {
14+
return [
15+
{
16+
label: <Star />,
17+
value: data.averageScore,
18+
color: "black-200",
19+
},
20+
{
21+
label: "후기",
22+
value: addComma(String(data.reviewCount)),
23+
color: "gray-100",
24+
},
25+
{
26+
label: "구매",
27+
value: addComma(String(data.salesCount)),
28+
color: "gray-100",
29+
},
30+
];
31+
};

0 commit comments

Comments
 (0)