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", "id": "c06c",
"productId": "d317", "productId": "d317",
"userId": "1", "userId": "1",
"quantity": 1 "quantity": 2
} }
] ]
} }

View File

@@ -12,7 +12,6 @@ export const CartApi = {
}, },
addToCart: async (data: AddToCartDTO) => { addToCart: async (data: AddToCartDTO) => {
let resultExisted = await axios.get(`${import.meta.env.VITE_SV_HOST}/carts?productId=${data.productId}`) let resultExisted = await axios.get(`${import.meta.env.VITE_SV_HOST}/carts?productId=${data.productId}`)
console.log("resultExisted", resultExisted.data)
if (!resultExisted.data[0]) { if (!resultExisted.data[0]) {
/* thêm mới */ /* thêm mới */
await axios.post(`${import.meta.env.VITE_SV_HOST}/carts`, { await axios.post(`${import.meta.env.VITE_SV_HOST}/carts`, {
@@ -28,6 +27,6 @@ export const CartApi = {
quantity: resultExisted.data[0].quantity + 1 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 { useSelector } from 'react-redux'
import type { StoreType } from '../../../stores' import type { StoreType } from '../../../stores'
import type { CartItem } from '../../../types/cart.type' import type { CartItem } from '../../../types/cart.type'
import { Apis } from '../../../apis'
import type { Product } from '../../../types/product.type'
export default function Cart() { export default function Cart() {
const userStore = useSelector((store: StoreType) => store.user) 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 ( return (
<div> <div>
Cart Page Cart Page

View File

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