App.jsx์ ์ฅ๋ฐ๊ตฌ๋ ๊ฐ์ ์ฆ๊ฐ ํ ํจ์๋ฅผ ์ถ๊ฐํ๋ค
// ์ฅ๋ฐ๊ตฌ๋ ์ํ ๊ฐ์ ์ฆ๊ฐ( type : ๊ฐ์,์ฆ๊ฐ )
const updateCart = (type, id) => {
const updatedCart = [...cart];
const productIndex = updatedCart.findIndex(
//findIndex:update ํ ์ ํ์ id์ idnex๋ฅผ ์ฐพ๋๋ค.
(item) => item.product._id === id
);
//๋ง์ฝ ํ์
์ด increase ์ด๋ฉด ๊ธฐ์กด์ ์๋์์ ์ฆ๊ฐํ๋ค.
if (type === "increase") {
updatedCart[productIndex].quantity += 1;
setCart(updatedCart);
}
//๋ง์ฝ ํ์
์ด decrease ์ด๋ฉด ๊ธฐ์กด์ ์๋์์ ๊ฐ์ํ๋ค.
if (type === "decrease") {
updatedCart[productIndex].quantity -= 1;
setCart(updatedCart);
}
};
id๋ฅผ ๊ฐ์ ธ์ค๋ฉด ๊ทธ ๋ฐ์ดํฐ๋ ์๋์ ๊ฐ๋ค.
๋ด๊ฐ ์
๋ฐ์ดํธํ๊ณ ์ ํ๋ ๊ฒ์
products ์ n๋ฒ์งธ ์ธ๋ฑ์ค์ quantity(์๋) ์ด๋ค.
๊ทธ๋์ findIndex๋ก ์ฐพ๊ณ ์ํ๋ ์ ํ์ index๋ฅผ ๋ฆฌํด์์ผ์
productIndex์ ๋ด์์ ์ฌ์ฉํ๋ค
๊ทธ๋ฆฌ๊ณ ํ๋ก๋ฐ์ด๋์ ์ถ๊ฐํ๋ค.
<CartContext.Provider
value={{ cart, addToCart, removeFromCart, updateCart }}
>
CartPage.jsx์ useContext์ ์ถ๊ฐ ํ๋ค.
const { cart, removeFromCart, updateCart } = useContext(CartContext);
QuantityInput์ props๋ก ์ ๋ฌํ๋ค.
<td className="align_center table_quantity_input">
<QuantityInput
quantity={quantity}
stock={product.stock}
setQuantity={updateCart}
cartPage={true}
productId={product._id}
/>
</td>
QuantityInput.jsx์์ ์ ๋ฌ๋ฐ์ props๋ก
import "./QuantityInput.css";
const QuantityInput = ({
quantity,
stock,
setQuantity,
cartPage,
productId,
}) => {
return (
<>
<button
// - ํด๋ฆญํ๋ฉด ๊ธฐ์กด์ ์๋์์ ํ๋ ์ค์ด๋ ๋ค.
onClick={() => {
cartPage //cartํ์ด์ง๋ก ๋์ด์จ props์ด๋ฉด true / ์์ธํ์ด์ง์ QuntityInput์ ๊ฒฝ์ฐ false
? setQuantity("decrease", productId) //type, id
: setQuantity((prev) => prev - 1);
}}
className="quantity_input_button"
disabled={quantity <= 1} // ์๋์ด 1์ด๋๋ฉด ํด๋ฆญํ์ง ๋ชปํ๊ฒํ๋ค.
>
-
</button>
<p className="quantity_input_count">{quantity}</p>
<button
// + ํด๋ฆญํ๋ฉด ๊ธฐ์กด์ ์๋์์ ํ๋ ์ฆ๊ฐํ๋ค.
onClick={() => {
cartPage //cartํ์ด์ง๋ก ๋์ด์จ props์ด๋ฉด true / ์์ธํ์ด์ง์ QuntityInput์ ๊ฒฝ์ฐ false
? setQuantity("increase", productId) //type, id
: setQuantity((prev) => prev + 1);
}}
className="quantity_input_button"
// ์๋๊ณผ ์ฌ๊ณ ์ ๊ฐ์๊ฐ ๊ฐ์์ง๋ฉด ํด๋ฆญํ์ง ๋ชปํ๋ค.
disabled={quantity === stock}
>
+
</button>
</>
);
};
export default QuantityInput;
๋ฒก์๋ API ์ถ๊ฐํ๊ธฐ cartServices
//์ํ ๊ฐ์ ์ฆ๊ฐ
export function increaseProductAPI(id) {
return apiClient.patch(`/cart/increase/${id}`);
}
//์ํ ๊ฐ์ ๊ฐ์
export function decreaseProductAPI(id) {
return apiClient.patch(`/cart/decrease/${id}`);
}
App.jsx์์ api ์ฌ์ฉํด์ ๋ฐฑ์๋์ ์
๋ฐ์ดํธํ๊ณ ์๋ฌ๊ฐ ์๊ธฐ๋ฉด ํ ์คํธ ๋ฉ์ธ์ง๋ก ์ถ๋ ฅํ๋ค.
// ์ฅ๋ฐ๊ตฌ๋ ์ํ ๊ฐ์ ์ฆ๊ฐ( type : ๊ฐ์,์ฆ๊ฐ )
const updateCart = (type, id) => {
const updatedCart = [...cart];
const productIndex = updatedCart.findIndex(
(item) => item.product._id === id
);
//๋ง์ฝ ํ์
์ด increase ์ด๋ฉด ๊ธฐ์กด์ ์๋์์ ์ฆ๊ฐํ๋ค.
if (type === "increase") {
updatedCart[productIndex].quantity += 1;
setCart(updatedCart);
increaseProductAPI(id).catch((err) => {
toast.error("์ํ ์ฆ๊ฐ ์๋ฌ");
});
}
//๋ง์ฝ ํ์
์ด decrease ์ด๋ฉด ๊ธฐ์กด์ ์๋์์ ๊ฐ์ํ๋ค.
if (type === "decrease") {
updatedCart[productIndex].quantity -= 1;
setCart(updatedCart);
decreaseProductAPI(id).catch((err) => {
toast.error("์ํ ๊ฐ์ ์๋ฌ");
});
}
};
์ฅ๋ฐ๊ตฌ๋์์ ๊ตฌ๋งค์๋ ๋ณ๊ฒฝ ํ ๋ฐฑ์๋์ ๋์ผํ์ง ํ์ธํด๋ณธ๋ค.