λν
μΌ νμ΄μ§μμ μ₯λ°κ΅¬λ λ΄κΈ° λ²νΌμ λλ₯΄λ©΄ λ°±μλμ λ°μ΄ν°λ₯Ό 보λ΄μ£Όλ ν¨μλ₯Ό μμ±ν΄λ³΄λ € νλ€. λ°±μλμ 보λ΄μΌ ν λ°μ΄ν°λ μν id
μ μλμ΄λ€. κ°κ²©μ΄λ μν κ΄λ ¨ μ 보λ product_id
λ§ λ³΄λ΄μ€λ λ€ μ²λ¦¬κ° κ°λ₯νκ²λ νλ€.
const handleAddItemToCart = () => {
fetch(`${API.USER}/cart`, {
method: 'POST',
body: JSON.stringify({
product_id: detailContents.id,
quantity: productAmount,
}),
})
.then(response => response.json())
.then(result => {
if (result.MESSAGE === 'SUCCESS') {
alert('μ₯λ°κ΅¬λμ λ΄κ²Όμ΅λλ€.');
window.location.reload();
});
};
const handleAddItemToCart = () => {
fetch(`${API.USER}/cart`, {
method: 'POST',
headers: {
Authorization: localStorage.getItem('token'),
},
body: JSON.stringify({
product_id: detailContents.id,
quantity: productAmount,
}),
})
.then(response => response.json())
.then(result => {
if (result.MESSAGE === 'SUCCESS') {
alert('μ₯λ°κ΅¬λμ λ΄κ²Όμ΅λλ€.');
window.location.reload();
} else if (result.MESSAGE === 'ITEM_ALREADY_EXIST')
alert('μ΄λ―Έ μ₯λ°κ΅¬λμ μλ μνμ
λλ€.');
else if (result.ERROR === 'INVALID_TOKEN')
alert('λ‘κ·ΈμΈ ν μ₯λ°κ΅¬λμ λ΄μμ£ΌμΈμ.');
});
};
headers
μ ν ν°μ μ€μ΄μ£Όκ³ , body
μ μ°λ¦¬κ° λ°μ΄ν°λ₯Ό 보λ΄μ€ product_id
μ quantity
λ₯Ό λ΄μμ€λ€. μ΄νμ, μλ΅μ λ°κ³ κ·Έμ λν κ°κ°μ λ©μμ§λ₯Ό λ£μ΄μ€λ€. μμ ν μ½λμμ λμ¬κ²¨ λ³Ό λΆλΆμ headers
μ΄λ€. λ‘κ·ΈμΈμ ν μ μ λ€λ§ μ₯λ°κ΅¬λμ μνμ λ΄μ μ μλλ‘ νλλ°, κ·Έλ¬λ €λ©΄ headers
μ ν ν°μ΄ μμ΄μΌ κ°λ₯νλ€. μμ μ μλ μ΄ λΆλΆμ κ°κ³Όνκ³ λμ΄κ°μλ€.
<Button
handleClick={handleAddItemToCart}
format="bigger"
type="button"
title={`μ₯λ°κ΅¬λμ λ΄κΈ° (${Number(
productAmount * price?.split('.')[0]
).toLocaleString()} μ)`}
/>
μμ μμ±νλhandleAddItemToCart
ν¨μλ₯Ό μ₯λ°κ΅¬λ λ΄κΈ° λ²νΌμ μΆκ°ν΄μ£Όλ©΄ λλ€.