Skip to content

Commit

Permalink
refactor: productItem에 CartItem을 props로 넘겨주는 방식으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
xodms0309 committed Jun 8, 2023
1 parent 1917799 commit 656dc6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/components/Product/ProductItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import { Cart, Product } from 'types';
import { calculateSalePercentage, formatPrice } from 'utils';

interface ProductItemProps {
cartList: Cart[];
cartItem?: Cart;
product: Product;
}

const ProductItem = ({ cartList, product }: ProductItemProps) => {
const ProductItem = ({ cartItem, product }: ProductItemProps) => {
const { decreaseItemQuantity, addItem, increaseItemQuantity } = useCart();

const cartItem = cartList.find(
(cartItem) => cartItem.product.id === product.id
);

const increase = () => {
if (!cartItem) return;
increaseItemQuantity(cartItem.id);
Expand Down
11 changes: 8 additions & 3 deletions src/components/Product/ProductItemList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ const ProductItemList = () => {

const fetchedProductList =
data &&
data.map((product) => (
<ProductItem key={product.id} cartList={cartList} product={product} />
));
data.map((product) => {
const cartItem = cartList.find(
(cartItem) => cartItem.product.id === product.id
);
return (
<ProductItem key={product.id} cartItem={cartItem} product={product} />
);
});

return <S.ProductListWrapper>{fetchedProductList}</S.ProductListWrapper>;
};
Expand Down

0 comments on commit 656dc6d

Please sign in to comment.