export function ProductPurchase({ id }) {
const { data: product } = useSuspenseQuery(productQueryOption({ id }));
return (
<div>
<h1>{product.title}</h1>
<p>{calculatePrice(product)}원</p>
<button
onClick={() => {
if (!validateStock(product.stock)) {
return;
}
submitOrder({
productId: id,
price: getFinalPrice(product),
timestamp: Date.now(),
}).then(() => {
navigate('/success');
});
}}
>
구매
</button>
</div>
);
}
const getFinalPrice = product => {
const { price } = calculatePrice(product);
const discount = getDiscount(price);
return price - discount;
};
TypeScript
복사
