diff --git a/src/refactoring/pages/Cart/features/ProductList/ProductList.tsx "b/src/refactoring/pages/Cart/ProductList/\bindex.tsx"
similarity index 84%
rename from src/refactoring/pages/Cart/features/ProductList/ProductList.tsx
rename to "src/refactoring/pages/Cart/ProductList/\bindex.tsx"
index 8fe619cc..dc38f535 100644
--- a/src/refactoring/pages/Cart/features/ProductList/ProductList.tsx
+++ "b/src/refactoring/pages/Cart/ProductList/\bindex.tsx"
@@ -1,4 +1,4 @@
-import { ProductItem } from '@/refactoring/pages/Cart/features/ProductList/components/ProductItem';
+import { ProductItem } from '@/refactoring/pages/Cart/ProductList/components/ProductItem';
import type { CartItem, Product } from '@/types';
interface ProductListProps {
diff --git a/src/refactoring/pages/Cart/features/ProductList/components/ProductItem.tsx b/src/refactoring/pages/Cart/ProductList/components/ProductItem.tsx
similarity index 100%
rename from src/refactoring/pages/Cart/features/ProductList/components/ProductItem.tsx
rename to src/refactoring/pages/Cart/ProductList/components/ProductItem.tsx
diff --git a/src/refactoring/pages/Cart/features/ShoppingCart/components/CartItem.tsx b/src/refactoring/pages/Cart/ShoppingCart/components/CartItem.tsx
similarity index 100%
rename from src/refactoring/pages/Cart/features/ShoppingCart/components/CartItem.tsx
rename to src/refactoring/pages/Cart/ShoppingCart/components/CartItem.tsx
diff --git a/src/refactoring/pages/Cart/features/ShoppingCart/components/AppliedCoupon.tsx b/src/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/AppliedCoupon.tsx
similarity index 100%
rename from src/refactoring/pages/Cart/features/ShoppingCart/components/AppliedCoupon.tsx
rename to src/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/AppliedCoupon.tsx
diff --git a/src/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/index.tsx b/src/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/index.tsx
new file mode 100644
index 00000000..c1f3f3a7
--- /dev/null
+++ b/src/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/index.tsx
@@ -0,0 +1,30 @@
+import { AppliedCoupon } from '@/refactoring/pages/Cart/ShoppingCart/components/CouponSelect/AppliedCoupon';
+import type { Coupon } from '@/types';
+
+type CouponSelectProps = {
+ selectedCoupon: Coupon | null;
+ coupons: Coupon[];
+ onCouponApply: (coupon: Coupon) => void;
+};
+
+export const CouponSelect = ({ selectedCoupon, coupons, onCouponApply }: CouponSelectProps) => {
+ return (
+ <>
+
+
+ {selectedCoupon &&
}
+ >
+ );
+};
diff --git a/src/refactoring/pages/Cart/features/ShoppingCart/components/OrderSummary.tsx b/src/refactoring/pages/Cart/ShoppingCart/components/OrderSummary.tsx
similarity index 100%
rename from src/refactoring/pages/Cart/features/ShoppingCart/components/OrderSummary.tsx
rename to src/refactoring/pages/Cart/ShoppingCart/components/OrderSummary.tsx
diff --git a/src/refactoring/pages/Cart/features/ShoppingCart/ShoppingCart.tsx b/src/refactoring/pages/Cart/ShoppingCart/index.tsx
similarity index 74%
rename from src/refactoring/pages/Cart/features/ShoppingCart/ShoppingCart.tsx
rename to src/refactoring/pages/Cart/ShoppingCart/index.tsx
index 3a013b1e..08a12d2a 100644
--- a/src/refactoring/pages/Cart/features/ShoppingCart/ShoppingCart.tsx
+++ b/src/refactoring/pages/Cart/ShoppingCart/index.tsx
@@ -1,7 +1,6 @@
-import { AppliedCoupon } from '@/refactoring/pages/Cart/features/ShoppingCart/components/AppliedCoupon';
-import { CartItem } from '@/refactoring/pages/Cart/features/ShoppingCart/components/CartItem';
-import { CouponSelect } from '@/refactoring/pages/Cart/features/ShoppingCart/components/CouponSelect';
-import { OrderSummary } from '@/refactoring/pages/Cart/features/ShoppingCart/components/OrderSummary';
+import { CartItem } from '@/refactoring/pages/Cart/ShoppingCart/components/CartItem';
+import { CouponSelect } from '@/refactoring/pages/Cart/ShoppingCart/components/CouponSelect';
+import { OrderSummary } from '@/refactoring/pages/Cart/ShoppingCart/components/OrderSummary';
import type { CartItem as CartItemType, Coupon } from '@/types';
interface ShoppingCartProps {
@@ -47,8 +46,7 @@ export const ShoppingCart = ({
쿠폰 적용
-
- {selectedCoupon &&
}
+
diff --git a/src/refactoring/pages/Cart/features/ShoppingCart/components/CouponSelect.tsx b/src/refactoring/pages/Cart/features/ShoppingCart/components/CouponSelect.tsx
deleted file mode 100644
index 6196fc92..00000000
--- a/src/refactoring/pages/Cart/features/ShoppingCart/components/CouponSelect.tsx
+++ /dev/null
@@ -1,19 +0,0 @@
-import type { Coupon } from '@/types';
-
-type CouponSelectProps = {
- coupons: Coupon[];
- onCouponApply: (coupon: Coupon) => void;
-};
-
-export const CouponSelect = ({ coupons, onCouponApply }: CouponSelectProps) => {
- return (
-
- );
-};
diff --git a/src/refactoring/pages/Cart/hooks/useCart.ts b/src/refactoring/pages/Cart/hooks/useCart.ts
index 9176d9e4..61ef51d8 100644
--- a/src/refactoring/pages/Cart/hooks/useCart.ts
+++ b/src/refactoring/pages/Cart/hooks/useCart.ts
@@ -8,25 +8,22 @@ export const useCart = () => {
const [cart, setCart] = useState([]);
const [selectedCoupon, setSelectedCoupon] = useState(null);
- // Q. updateCartItemQuantity는 함수로 분리 되어 있는데, addToCart는 분리 안되어 있는 이유는 뭘까..?
const addToCart = (product: Product) => {
- setCart(prevCart => {
- const existingItem = prevCart.find(item => item.product.id === product.id);
- if (existingItem) {
- return prevCart.map(item =>
- item.product.id === product.id ? { ...item, quantity: Math.min(item.quantity + 1, product.stock) } : item
- );
- }
- return [...prevCart, { product, quantity: 1 }];
- });
+ const existingItem = cart.find(item => item.product.id === product.id);
+ if (existingItem) {
+ setCart(updateCartItemQuantity(cart, product.id, existingItem.quantity + 1));
+ return;
+ }
+
+ setCart([...cart, { product, quantity: 1 }]);
};
const removeFromCart = (productId: string) => {
- setCart(prevCart => prevCart.filter(item => item.product.id !== productId));
+ setCart(cart.filter(item => item.product.id !== productId));
};
const updateQuantity = (productId: string, newQuantity: number) => {
- setCart(prevCart => updateCartItemQuantity(prevCart, productId, newQuantity));
+ setCart(updateCartItemQuantity(cart, productId, newQuantity));
};
const applyCoupon = (coupon: Coupon) => {
diff --git a/src/refactoring/pages/Cart/hooks/useCartLocalStorage.ts b/src/refactoring/pages/Cart/hooks/useCartLocalStorage.ts
new file mode 100644
index 00000000..3f39e8e4
--- /dev/null
+++ b/src/refactoring/pages/Cart/hooks/useCartLocalStorage.ts
@@ -0,0 +1,47 @@
+import { useLocalStorage } from '@/refactoring/hooks';
+import { calculateCartTotal, updateCartItemQuantity } from '@/refactoring/pages/Cart/model/cart';
+import type { CartItem, Coupon, Product } from '@/types';
+
+export const useCartLocalStorage = () => {
+ const [cart, setCart] = useLocalStorage({ key: 'cart', initialValue: [] });
+ const [selectedCoupon, setSelectedCoupon] = useLocalStorage({
+ key: 'selectedCoupon',
+ initialValue: null
+ });
+
+ const addToCart = (product: Product) => {
+ const existingItem = cart.find(item => item.product.id === product.id);
+ if (existingItem) {
+ setCart(updateCartItemQuantity(cart, product.id, existingItem.quantity + 1));
+ return;
+ }
+
+ setCart([...cart, { product, quantity: 1 }]);
+ };
+
+ const removeFromCart = (productId: string) => {
+ setCart(cart.filter(item => item.product.id !== productId));
+ };
+
+ const updateQuantity = (productId: string, newQuantity: number) => {
+ setCart(updateCartItemQuantity(cart, productId, newQuantity));
+ };
+
+ const applyCoupon = (coupon: Coupon) => {
+ setSelectedCoupon(coupon);
+ };
+
+ const calculateTotal = () => {
+ return calculateCartTotal(cart, selectedCoupon);
+ };
+
+ return {
+ cart,
+ addToCart,
+ removeFromCart,
+ updateQuantity,
+ applyCoupon,
+ calculateTotal,
+ selectedCoupon
+ };
+};