This commit is contained in:
2025-10-09 09:45:37 +07:00
parent 55e49096c5
commit b237658bb9
4 changed files with 32 additions and 12 deletions

View File

@@ -79,7 +79,7 @@
"id": "c06c",
"productId": "d317",
"userId": "1",
"quantity": 1
"quantity": 2
}
]
}

View File

@@ -12,7 +12,6 @@ export const CartApi = {
},
addToCart: async (data: AddToCartDTO) => {
let resultExisted = await axios.get(`${import.meta.env.VITE_SV_HOST}/carts?productId=${data.productId}`)
console.log("resultExisted", resultExisted.data)
if (!resultExisted.data[0]) {
/* thêm mới */
await axios.post(`${import.meta.env.VITE_SV_HOST}/carts`, {
@@ -28,6 +27,6 @@ export const CartApi = {
quantity: resultExisted.data[0].quantity + 1
})
console.log("resultUpdate", resultUpdate.data)
return true
}
}

View File

@@ -2,11 +2,28 @@ import React from 'react'
import { useSelector } from 'react-redux'
import type { StoreType } from '../../../stores'
import type { CartItem } from '../../../types/cart.type'
import { Apis } from '../../../apis'
import type { Product } from '../../../types/product.type'
export default function Cart() {
const userStore = useSelector((store: StoreType) => store.user)
//
//đây là api lấy product id let data = Apis.product.findProductById(productId)
// data có interface là Product
// cho bạn interface product luôn
/*
export interface Product {
id: string;
categoryId: string;
price: number;
iconUrl: string;
isActive: boolean;
name: string;
des: string;
}
*/
return (
<div>
Cart Page

View File

@@ -29,11 +29,16 @@ export default function ProductDetail() {
return (
<div className="max-w-4xl mx-auto p-6 mt-10 bg-white shadow-lg rounded-2xl">
<Button onClick={() => {
Apis.cart.addToCart({
productId: product.id,
userId: userStore.data?.id
})
<Button onClick={async () => {
try {
let result = await Apis.cart.addToCart({
productId: product.id,
userId: userStore.data?.id
})
alert("thành công")
} catch (err) {
}
}}>Add To Cart</Button>
<div className="flex flex-col md:flex-row gap-6">
{/* Hình ảnh sản phẩm */}
@@ -52,11 +57,10 @@ export default function ProductDetail() {
{product.price.toLocaleString()}
</p>
<span
className={`inline-block px-3 py-1 text-sm font-medium rounded-full ${
product.isActive
className={`inline-block px-3 py-1 text-sm font-medium rounded-full ${product.isActive
? 'bg-green-100 text-green-700'
: 'bg-red-100 text-red-700'
}`}
}`}
>
{product.isActive ? 'Đang bán' : 'Ngừng kinh doanh'}
</span>