function CartPage(props) {
const dispatch = useDispatch();
const [Total, setTotal] = useState(0);
const [ShowTotal, setShowTotal] = useState(false);
const [ShowSuccess, setShowSuccess] = useState(false);
useEffect(() => {
let cartItems = [];
//리덕스 User state안에 cart 안에 상품이 들어있는지 확인
if (props.user.userData && props.user.userData.cart) {
if (props.user.userData.cart.length > 0) {
props.user.userData.cart.forEach((item) => {
cartItems.push(item.id);
});
dispatch(getCartItems(cartItems, props.user.userData.cart)).then(
(response) => {
calculateTotal(response.payload);
}
);
}
}
}
//user_actions.js
export function getCartItems(cartItems, userCart) {
const request = axios
.get(`/api/product/products_by_id?id=${cartItems}&type=array`)
.then((response) => {
// CartItem들에 해당하는 정보들을
// Product Collection에서 가져온후에
// Quantity 정보를 넣어 준다.
userCart.forEach((cartItem) => {
response.data.forEach((productDetail, index) => {
if (cartItem.id === productDetail._id) {
response.data[index].quantity = cartItem.quantity;
}
});
});
return response.data;
});
return {
type: GET_CART_ITEMS,
payload: request,
};
}
//userCardBlock.js
import React from "react";
import "./UserCardBlock.css";
function UserCardBlock(props) {
const renderCartImage = (images) => {
if (images.length > 0) {
let image = images[0];
return `http://localhost:5000/${image}`;
}
};
const renderItems = () =>
props.products &&
props.products.map((product, index) => (
<tr key={index}>
<td>
<img
style={{ width: "70px" }}
alt="product"
src={renderCartImage(product.images)}
/>
</td>
<td>{product.quantity} EA</td>
<td>$ {product.price}</td>
<td>
Remove</button>
</td>
</tr>
));
return (
<div>
<table>
<thead>
<tr>
<th>Product Image</th>
<th>Product Quantity</th>
<th>Product Price</th>
<th>Remove from Cart</th>
</tr>
</thead>
<tbody>{renderItems()}</tbody>
</table>
</div>
);
}
export default UserCardBlock;
//userCardBlock.css
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
//<cartPage.js.
<UserCardBlock
products={props.user.cartDetail}
/>
추가
json 형식으로 success: true와 product 정보를 같이 주는 걸 장바구니 정보만 받기 위해 수정
Product.find({ _id: { $in: productIds } })
.populate('writer')
.exec((err, product) => {
if (err) return res.status(400).send(err)
return res.status(200).json({success: true, product})
})
})
Product.find({ _id: { $in: productIds } })
.populate('writer')
.exec((err, product) => {
if (err) return res.status(400).send(err)
return res.status(200).send(product)
})
})
//json형식을 send로 수정
//DetailProductPage.js 도 수정
axios
.get(`/api/product/products_by_id?id=${productId}&type=single`)
.then((response) => {
setProduct(response.data[0]);
})
.catch((err) => alert(err));
price * quantity
let calculateTotal = (cartDetail) => {
let total = 0;
cartDetail.map((item) => {
total += parseInt(item.price, 10) * item.quantity;
});
setTotal(total);
setShowTotal(true);
};
//cartPage.js
let removeFromCart = (productId) => {
dispatch(removeCartItem(productId)).then((response) => {
if (response.payload.productInfo.length <= 0) {
setShowTotal(false);
}
});
};
//UserCartBlock.js
<button onClick={() => props.removeItem(product._id)}>Remove</button>
// user_reducer.js에 추가
case REMOVE_CART_ITEM:
return {
...state,
cartDetail: action.payload.productInfo,
userData: {
...state.userData,
cart: action.payload.cart,
},
};
//types.js 에 추가
export const REMOVE_CART_ITEM = 'remove_cart_item';
//user.js
router.get("/removeFromCart", auth, (req, res) => {
//먼저 cart안에 내가 지우려고 한 상품을 지워주기
User.findOneAndUpdate(
{ _id: req.user._id },
{
$pull: { cart: { id: req.query.id } },
},
{ new: true },
(err, userInfo) => {
let cart = userInfo.cart;
let array = cart.map((item) => {
return item.id;
});