๊ณ ๋ฏผํด๋ดค๋๋ฐ... ์ฅ๋ฐ๊ตฌ๋ ํ์ด์ง๋ฅผ ๋ฐ๋ก ๋ง๋๋๊ฒ ๋์๊ฑฐ ๊ฐ์ ใ
ํ์ ๋๊ฐ ๋ง๋ค์๊ณ
ํ์ด์ด ๋ฒ ์ด์ค์์ ์ฅ๋ฐ๊ตฌ๋ db๋ถ๋ฌ์ ์์ ๋ฃ์ด์ฃผ๋ฉด ๋ ๋ฏ
1. ์ฅ๋ฐ๊ตฌ๋์ ์ค์๊ฐ์ผ๋ก ์ ์ ๋ณ ์ฅ๋ฐ๊ตฌ๋ ํญ๋ชฉ ๊ฐ์ ธ์ค๊ธฐ
useEffect(() => {
//์ฅ๋ฐ๊ตฌ๋ ๊ฐ์ ธ์ค๋ ํจ์
const fetchUserCart = async () => {
if (user) {
//์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ
const q = query(
collection(db, "cart"),
where("userId", "==", user.uid) //๋ก๊ทธ์ธ ํ ์ ์ ์ ๋์ผํ ๋ฐ์ดํฐ๋ง
);
//์ค์๊ฐ ๊ฐ์ ธ์ค๊ธฐ
onSnapshot(q, (snapshot) => {
const userCart = snapshot.docs.map((doc) => doc.data());
setUserCart(userCart);
});
}
};
fetchUserCart();
}, [user]); // ๋ก๊ทธ์ธ ์ ์ ๊ฐ ๋ณ๊ฒฝ ๋ ๋๋ง๋ค ์คํ
2. ์ฅ๋ฐ๊ตฌ๋ ํญ๋ชฉ ์ญ์
//์ฅ๋ฐ๊ตฌ๋ ์ญ์
const handleRemoveCart = async (value) => {
if (confirm("์ฅ๋ฐ๊ตฌ๋์์ ์ญ์ ํ๊ฒ ์ต๋๊น?")) {
//2-1)์ ์ ๊ฐ ์์ผ๋ฉด(๋ก๊ทธ์ธ)
if (user) {
try {
//์ฟผ๋ฆฌ๋ฌธ ์์ฑ
const q = query(
collection(db, "cart"), // ์ญ์ ํ ์ปฌ๋ ์
์ง์
where("userId", "==", user.uid), // ํ์ฌ ๋ก๊ทธ์ธ๋์ด์๋ ์ ์ ์ ๊ฐ์ ๊ฒ
where("interestBook", "==", value) //book.isbn ์ผ๋ก ์ฐพ์
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach(async (doc) => {
try {
await deleteDoc(doc.ref); //์ญ์ ํ๋ค.
console.log("์ฅ๋ฐ๊ตฌ๋ ์ญ์ ์ฑ๊ณต!");
} catch (error) {
console.error("Error deleting document:", error);
}
});
} catch (error) {
console.error("Error querying document:", error);
}
}
} else {
return;
}
};
3. ์๋ ์ ํ ๋ฐ ์ค์๊ฐ ๋ฐ์ํ์ฌ ํ์ธ (์์ ์ฅ๋ฐ๊ตฌ๋ ๊ฐ์ ธ์ค๋ ํจ์์ onSnapshot์ ์ฌ์ฉํ์ฌ
์ค์๊ฐ์ผ๋ก ๊ฐ์ ธ์ค๊ฒ ๋ง๋ค์์)
<div className="amountSection">
<div
onClick={() =>
handleMinus(cart.interestBook, cart.amount)
}
className="minus"
>
<FaMinus />
</div>
<div className="amount">{cart.amount}</div>
<div
onClick={() =>
handlePlus(cart.interestBook, cart.amount)
}
className="plus"
>
<FaPlus />
</div>
์ด๊ฒ ์์ ๋ถ๋ถ์ธ๋ฐ
- ๋ฅผ ๋๋ฅด๋ฉด ๋ง์ด๋์ค๋ก ๊ณ์ฐํ์ฌ ์
๋ฐ์ดํธ , +๋ฅผ ๋๋ฅด๋ฉด ํ๋ฌ์ค๋ก ๊ณ์ฐํ์ฌ ์
๋ฐ์ดํธ ํ๊ฒ๋ ๊ตฌ์ํจ
//์๋ ๋ณ๊ฒฝ - ๋ง์ด๋์ค
const handleMinus = (bookId, amount) => {
if (amount >= 2) {
const minusAmount = amount - 1;
//console.log(minusAmount);
//DB์์ ๋ง์ด๋์ค ์ฒ๋ฆฌํ๊ธฐ
updateCart(bookId, minusAmount);
} else {
alert("์ต์ 1๊ฐ ์ด์ ๊ตฌ๋งค๊ฐ๋ฅํฉ๋๋ค.");
}
};
//์๋ ๋ณ๊ฒฝ - ํ๋ฌ์ค
const handlePlus = (bookId, amount) => {
const plusAmount = amount + 1;
//const plusAmount = num.toString();
//DB์์ ํ๋ฌ์ค ์ฒ๋ฆฌํ๊ธฐ
updateCart(bookId, plusAmount);
};
๊ทธ๋ฆฌ๊ณ ๋ง์ด๋์ค์ ๊ฒฝ์ฐ 1๊ฐ์ ๊ฒฝ์ฐ ๋ง์ด๋์ค๋ฅผ ํ ์ ์์ผ๋ ํ ๊ฐ ์ด์ ๊ตฌ๋งค๊ฐ๋ฅํ๋ค๊ณ ๊ฒฝ๊ณ ์ฐฝ ๋์ฐ๊ณ ๋ฆฌํดํจ
์๋ ์
๋ฐ์ดํธํ๋ ํจ์
// ์๋ ์
๋ฐ์ดํธ
const updateCart = async (bookId, amount) => {
//์นดํธ ์ปฌ๋ ์
์์ ์ธ์ฆ ๋ ์ ์ , ๊ทธ๋ฆฌ๊ณ ์๋ ์ ํํ๋ book์ id๋ค์ด๊ฐ
const q = query(
collection(db, "cart"),
where("userId", "==", user.uid),
where("interestBook", "==", bookId)
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach(async (doc) => {
try {
const docRef = doc.ref;
await updateDoc(docRef, { amount: amount });
} catch (error) {
console.error(error);
}
});
};
4. ์ํ ๋ณ ์ด ๊ธ์ก ๊ณ์ฐ
<p style={{ fontWeight: "bold" }}>
์ด {cart.salesPrice * cart.amount}์
</p>
5. ์ฃผ๋ฌธ ์ ๋ณด์ ์ด ์๋๊ณผ ์ด ์ํ๊ธ์ก ๊ณ์ฐํ์ฌ ์ฃผ๋ฌธ ๊ธ์ก ์ฐ์ ํ๊ธฐ
const [amount, setAmount] = useState([]); // ํ์ฌ ์ฅ๋ฐ๊ตฌ๋์ ์ํ ์๋
const [totalAmount, setTotalAmount] = useState(null); //์๋ ํฉ๊ณ
const [totalPrice, setTotalPrice] = useState(null); //๊ธ์ก ํฉ๊ณ
useEffect(() => {
//์ฅ๋ฐ๊ตฌ๋ ๊ฐ์ ธ์ค๋ ํจ์
const fetchUserCart = async () => {
if (user) {
//์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ
const q = query(
collection(db, "cart"),
where("userId", "==", user.uid) //๋ก๊ทธ์ธ ํ ์ ์ ์ ๋์ผํ ๋ฐ์ดํฐ๋ง
);
//์ค์๊ฐ ๊ฐ์ ธ์ค๊ธฐ
onSnapshot(q, (snapshot) => {
const userCart = snapshot.docs.map((doc) => doc.data());
const amountResult = snapshot.docs.map((doc) => doc.data().amount);
setUserCart(userCart);
setAmount(amountResult);
//console.log(totalPrice);
});
}
};
//์ด ์ํ ์๋ ํฉ๊ณ ๊ตฌํ๊ธฐ
const sumAmount = () => {
const result = userCart.reduce((prev, current) => {
return prev + current.amount;
}, 0);
setTotalAmount(result);
};
//์ด ์ํ ๊ธ์ก ํฉ๊ณ ๊ตฌํ๊ธฐ
const sumPrice = () => {
const result = userCart.reduce((prev, current, index) => {
//๊ฐ ์ํ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ํฉ์ฐํ๋ค.
const itemTotalPrice = current.salesPrice * amount[index];
return prev + itemTotalPrice;
}, 0);
setTotalPrice(result);
};
fetchUserCart();
sumAmount();
sumPrice();
}, [user, userCart]); // ๋ก๊ทธ์ธ ์ ์ ๊ฐ ๋ณ๊ฒฝ ๋ ๋๋ง๋ค ์คํ
DB๋ฅผ ๊ฐ์ ธ์ฌ ๋ ์๋๋ง ๋ฐ๋ก ๊ฐ์ ธ์ค๊ฒ ํ๊ณ state์ ์ ์ฅํจ.
reduce() ๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ํฉ๊ณ๋ฅผ ๊ตฌํด์ค
์ด ์ํ ๊ธ์ก ํฉ๊ณ๋ฅผ ๊ตฌํ ๋๋ 1๊ฐ์ ๊ธ์ก x ์๋์ ๊ธ์ก์ด ๋์์ผ ํ๋๊น
userCart์์ 1๊ฐ์ ๊ธ์ก์ ๊ฐ์ ธ์ค๊ณ i๋ฒ ์ธ๋ฑ์ค์ amount๋ฅผ ๊ณฑํด์ค ํ ํฉํ๋ค.
์ฐธ๊ณ
https://miiingo.tistory.com/365
[Node.js] javascript: Array.reduce() ์ฌ์ฉ ๋ฐฉ๋ฒ ์ ๋ฆฌ
Array.reduce() โ ๊ธฐ๋ณธ ์ ๋ฆฌ ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ๋ฆฌ๋์(reducer) ํจ์๋ฅผ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํฉ๋๋ค. arr.reduce(callback[, initialValue]) callback : ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์คํํ ํจ์.
miiingo.tistory.com
์ฅ๋ฐ๊ตฌ๋์ ๋์๊ฐ ์์ ๊ฒฝ์ฐ
{userCart.length === 0 ? (
<div
className="cart"
style={{
height: "500px",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<p>์ฅ๋ฐ๊ตฌ๋์ ์ํ์ด ์์ต๋๋ค.</p>
</div>
) :