fix: cart

This commit is contained in:
2026-04-14 15:56:48 +02:00
parent c59428adaa
commit fa85c34794
12 changed files with 242 additions and 317 deletions

View File

@@ -11,6 +11,7 @@ export interface Product {
productDetails?: string
product_id: number
is_favorite?: boolean
quantity: number
}
export interface ProductResponse {
@@ -56,6 +57,11 @@ export const useCustomerProductStore = defineStore('customer-product', () => {
}
}
function updateFavoriteState(product_id: number, value: boolean) {
const p = productsList.value.find(p => p.product_id === product_id)
if (p) p.is_favorite = value
}
async function toggleFavorite(product: Product) {
const productId = product.product_id
const isFavorite = product.is_favorite
@@ -64,11 +70,19 @@ export const useCustomerProductStore = defineStore('customer-product', () => {
try {
if (!isFavorite) {
await useFetchJson(url, { method: 'POST' })
await useFetchJson(url, {
method: 'POST',
body: JSON.stringify({
id: productId
}),
})
} else {
await useFetchJson(url, { method: 'DELETE' })
await useFetchJson(url, {
method: 'DELETE',
})
}
product.is_favorite = !isFavorite
product.is_favorite = !product.is_favorite
} catch (e: unknown) {
error.value = e instanceof Error ? e.message : 'Failed to update favorite'
}
@@ -77,6 +91,7 @@ export const useCustomerProductStore = defineStore('customer-product', () => {
return {
fetchProductList,
toggleFavorite,
updateFavoriteState,
productsList,
total,
loading,