fix: cart
This commit is contained in:
@@ -15,7 +15,6 @@ export const useCartStore = defineStore('cart', () => {
|
||||
const activeCartId = ref<number | null>(null)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
|
||||
async function fetchCarts() {
|
||||
try {
|
||||
const res = await useFetchJson<ApiResponse>(
|
||||
@@ -28,7 +27,6 @@ export const useCartStore = defineStore('cart', () => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function addNewCart(name: string) {
|
||||
try {
|
||||
error.value = null
|
||||
@@ -51,38 +49,59 @@ export const useCartStore = defineStore('cart', () => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const route = useRoute()
|
||||
const amount = ref<number>(1);
|
||||
const errorMassege = ref('');
|
||||
const cartId = computed(() => Number(route.params.id));
|
||||
const errorMessage = ref('');
|
||||
|
||||
async function addProduct(product_id: number, count: number) {
|
||||
if (!activeCartId.value) {
|
||||
errorMessage.value = 'No active cart selected'
|
||||
return
|
||||
}
|
||||
|
||||
async function addProduct(product_id: number) {
|
||||
try {
|
||||
const res = await useFetchJson<ApiResponse>(
|
||||
`/api/v1/restricted/carts/add-product-to-cart?cart_id=${cartId.value}&product_id=${product_id}&amount=${amount.value}`
|
||||
`/api/v1/restricted/carts/add-product-to-cart?cart_id=${activeCartId.value}&product_id=${product_id}&amount=${count}`
|
||||
)
|
||||
console.log('sfdsfdfd', res);
|
||||
|
||||
console.log('fsdfsdfdsfdsfs', res)
|
||||
} catch (e: any) {
|
||||
errorMassege.value = e?.message ?? 'Error loading carts'
|
||||
errorMessage.value = e?.message ?? 'Error adding product'
|
||||
}
|
||||
}
|
||||
|
||||
function setActiveCart(id: number | null) {
|
||||
activeCartId.value = id
|
||||
|
||||
// function setActiveCart(id: number) {
|
||||
// activeCartId.value = id
|
||||
// }
|
||||
if (id) {
|
||||
localStorage.setItem('activeCartId', String(id))
|
||||
} else {
|
||||
localStorage.removeItem('activeCartId')
|
||||
}
|
||||
}
|
||||
|
||||
function initCart() {
|
||||
const saved = localStorage.getItem('activeCartId')
|
||||
|
||||
if (saved) {
|
||||
activeCartId.value = Number(saved)
|
||||
}
|
||||
}
|
||||
|
||||
const activeCart = computed(() => {
|
||||
return carts.value.find(c => c.cart_id === activeCartId.value)
|
||||
})
|
||||
|
||||
return {
|
||||
carts,
|
||||
activeCartId,
|
||||
error,
|
||||
errorMassege,
|
||||
// setActiveCart,
|
||||
errorMessage,
|
||||
activeCart,
|
||||
setActiveCart,
|
||||
addProduct,
|
||||
fetchCarts,
|
||||
addNewCart,
|
||||
|
||||
initCart,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user