Merge remote-tracking branch 'origin/translate' into front-styles
This commit is contained in:
@@ -2,21 +2,10 @@ import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { useFetchJson } from '@/composable/useFetchJson'
|
||||
|
||||
export interface CartItem {
|
||||
id: number
|
||||
productId: number
|
||||
name: string
|
||||
image: string
|
||||
price: number
|
||||
quantity: number
|
||||
product_number: string
|
||||
}
|
||||
|
||||
export interface DeliveryMethod {
|
||||
export interface Cart {
|
||||
id: number
|
||||
name: string
|
||||
price: number
|
||||
description: string
|
||||
items: any[]
|
||||
}
|
||||
|
||||
export interface Address {
|
||||
@@ -29,17 +18,9 @@ export interface Address {
|
||||
export type AddressTemplate = Record<string, string>
|
||||
|
||||
export const useCartStore = defineStore('cart', () => {
|
||||
const items = ref<CartItem[]>([])
|
||||
const selectedAddressId = ref<number | null>(null)
|
||||
const selectedDeliveryMethodId = ref<number | null>(null)
|
||||
const shippingCost = ref(0)
|
||||
const vatRate = ref(0.23) // 23% VAT
|
||||
const currentPage = ref(1)
|
||||
const deliveryMethods = ref<DeliveryMethod[]>([
|
||||
{ id: 1, name: 'Standard Delivery', price: 0, description: '5-7 business days' },
|
||||
{ id: 2, name: 'Express Delivery', price: 15, description: '2-3 business days' },
|
||||
{ id: 3, name: 'Priority Delivery', price: 30, description: 'Next business day' }
|
||||
])
|
||||
const carts = ref<Cart[]>([])
|
||||
const activeCartId = ref<number | null>(null)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const addresses = ref<Address[]>([])
|
||||
const addressLoading = ref(false)
|
||||
@@ -165,74 +146,70 @@ export const useCartStore = defineStore('cart', () => {
|
||||
]
|
||||
}
|
||||
|
||||
const productsTotal = computed(() => {
|
||||
return items.value.reduce((sum, item) => sum + (item.price * item.quantity), 0)
|
||||
})
|
||||
async function addNewCart(name: string) {
|
||||
try {
|
||||
error.value = null
|
||||
|
||||
const vatAmount = computed(() => {
|
||||
return productsTotal.value * vatRate.value
|
||||
})
|
||||
const url = `/api/v1/restricted/carts/add-new-cart`
|
||||
const response = await useFetchJson<ApiResponse>(url)
|
||||
|
||||
const orderTotal = computed(() => {
|
||||
return productsTotal.value + shippingCost.value + vatAmount.value
|
||||
})
|
||||
|
||||
const itemCount = computed(() => {
|
||||
return items.value.reduce((sum, item) => sum + item.quantity, 0)
|
||||
})
|
||||
|
||||
function updateQuantity(itemId: number, quantity: number) {
|
||||
const item = items.value.find(i => i.id === itemId)
|
||||
if (item) {
|
||||
if (quantity <= 0) {
|
||||
removeItem(itemId)
|
||||
} else {
|
||||
item.quantity = quantity
|
||||
const newCart: Cart = {
|
||||
id: response.items.cart_id,
|
||||
name: response.items.name,
|
||||
items: []
|
||||
}
|
||||
|
||||
carts.value.push(newCart)
|
||||
activeCartId.value = newCart.id
|
||||
|
||||
return newCart
|
||||
} catch (e: any) {
|
||||
error.value = e?.message ?? 'Error creating cart'
|
||||
}
|
||||
}
|
||||
|
||||
function deleteProduct(id: number): boolean {
|
||||
const index = items.value.findIndex(a => a.id === id)
|
||||
if (index === -1) return false
|
||||
const route = useRoute()
|
||||
const amount = ref<number>(1);
|
||||
const errorMessage = ref('');
|
||||
|
||||
items.value.splice(index, 1)
|
||||
resetProductPagination()
|
||||
async function addProduct(product_id: number, count: number) {
|
||||
if (!activeCartId.value) {
|
||||
errorMessage.value = 'No active cart selected'
|
||||
return
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
try {
|
||||
const res = await useFetchJson<ApiResponse>(
|
||||
`/api/v1/restricted/carts/add-product-to-cart?cart_id=${activeCartId.value}&product_id=${product_id}&amount=${count}`
|
||||
)
|
||||
|
||||
function resetProductPagination() {
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
function removeItem(itemId: number) {
|
||||
const index = items.value.findIndex(i => i.id === itemId)
|
||||
if (index !== -1) {
|
||||
items.value.splice(index, 1)
|
||||
console.log('fsdfsdfdsfdsfs', res)
|
||||
} catch (e: any) {
|
||||
errorMessage.value = e?.message ?? 'Error adding product'
|
||||
}
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
items.value = []
|
||||
selectedAddressId.value = null
|
||||
selectedDeliveryMethodId.value = null
|
||||
shippingCost.value = 0
|
||||
}
|
||||
function setActiveCart(id: number | null) {
|
||||
activeCartId.value = id
|
||||
|
||||
function setSelectedAddress(addressId: number | null) {
|
||||
selectedAddressId.value = addressId
|
||||
}
|
||||
|
||||
function setDeliveryMethod(methodId: number) {
|
||||
selectedDeliveryMethodId.value = methodId
|
||||
const method = deliveryMethods.value.find(m => m.id === methodId)
|
||||
if (method) {
|
||||
shippingCost.value = method.price
|
||||
if (id) {
|
||||
localStorage.setItem('activeCartId', String(id))
|
||||
} else {
|
||||
localStorage.removeItem('activeCartId')
|
||||
}
|
||||
}
|
||||
|
||||
initMockData()
|
||||
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 {
|
||||
items,
|
||||
@@ -268,4 +245,4 @@ export const useCartStore = defineStore('cart', () => {
|
||||
setAddressSearchQuery,
|
||||
getAddressTemplate
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user