Files
b2b/bo/src/stores/cart.ts
2026-03-25 15:56:52 +01:00

240 lines
5.9 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export interface CartItem {
id: number
productId: number
name: string
image: string
price: number
quantity: number
product_number: string
}
export interface DeliveryMethod {
id: number
name: string
price: number
description: string
}
export interface Cart {
id: string
name: string
items: CartItem[]
}
export const useCartStore = defineStore('cart', () => {
const carts = ref<Cart[]>([])
const activeCartId = ref<string | null>(null)
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 items = computed(() => {
if (!activeCartId.value) return []
const cart = carts.value.find(c => c.id === activeCartId.value)
return cart ? cart.items : []
})
const activeCart = computed(() => {
return carts.value.find(c => c.id === activeCartId.value) || null
})
const productsTotal = computed(() => {
return items.value.reduce((sum, item) => sum + (item.price * item.quantity), 0)
})
const vatAmount = computed(() => {
return productsTotal.value * vatRate.value
})
const orderTotal = computed(() => {
return productsTotal.value + shippingCost.value + vatAmount.value
})
const itemCount = computed(() => {
return items.value.reduce((sum, item) => sum + item.quantity, 0)
})
function createCart(name: string): Cart {
const newCart: Cart = {
id: `cart-${Date.now()}`,
name,
items: []
}
carts.value.push(newCart)
activeCartId.value = newCart.id
return newCart
}
function deleteCart(cartId: string) {
const index = carts.value.findIndex(c => c.id === cartId)
if (index !== -1) {
carts.value.splice(index, 1)
if (activeCartId.value === cartId) {
const firstCart = carts.value[0]
activeCartId.value = firstCart ? firstCart.id : null
}
}
}
function renameCart(cartId: string, newName: string) {
const cart = carts.value.find(c => c.id === cartId)
if (cart) {
cart.name = newName
}
}
function setActiveCart(cartId: string) {
const cart = carts.value.find(c => c.id === cartId)
if (cart) {
activeCartId.value = cartId
}
}
function addItemToActiveCart(item: CartItem) {
if (!activeCartId.value) {
createCart('Cart 1')
}
const cart = carts.value.find(c => c.id === activeCartId.value)
if (cart) {
const existingItem = cart.items.find(i => i.productId === item.productId)
if (existingItem) {
existingItem.quantity += item.quantity
} else {
cart.items.push(item)
}
}
}
function updateQuantity(itemId: number, quantity: number) {
const cart = carts.value.find(c => c.id === activeCartId.value)
if (!cart) return
const item = cart.items.find(i => i.id === itemId)
if (item) {
if (quantity <= 0) {
removeItem(itemId)
} else {
item.quantity = quantity
}
}
}
function deleteProduct(id: number): boolean {
const cart = carts.value.find(c => c.id === activeCartId.value)
if (!cart) return false
const index = cart.items.findIndex(a => a.id === id)
if (index === -1) return false
cart.items.splice(index, 1)
resetProductPagination()
return true
}
function resetProductPagination() {
currentPage.value = 1
}
function removeItem(itemId: number) {
const cart = carts.value.find(c => c.id === activeCartId.value)
if (!cart) return
const index = cart.items.findIndex(i => i.id === itemId)
if (index !== -1) {
cart.items.splice(index, 1)
}
}
function clearCart() {
const cart = carts.value.find(c => c.id === activeCartId.value)
if (cart) {
cart.items = []
}
selectedAddressId.value = null
selectedDeliveryMethodId.value = null
shippingCost.value = 0
}
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
}
}
function initMockData() {
const cart1: Cart = {
id: 'cart-1',
name: 'Cart 1',
items: [
{ id: 1, productId: 101, name: 'Premium Widget Pro', product_number: 'NC209/7000', image: '/img/product-1.jpg', price: 129.99, quantity: 2 },
{ id: 2, productId: 102, name: 'Ultra Gadget X', product_number: 'NC234/6453', image: '/img/product-2.jpg', price: 89.50, quantity: 1 }
]
}
const cart2: Cart = {
id: 'cart-2',
name: 'Cart 2',
items: [
{ id: 3, productId: 103, name: 'Mega Tool Set', product_number: 'NC324/9030', image: '/img/product-3.jpg', price: 249.00, quantity: 3 }
]
}
const cart3: Cart = {
id: 'cart-3',
name: 'Cart 3',
items: []
}
carts.value = [cart1, cart2, cart3]
activeCartId.value = 'cart-1'
}
initMockData()
return {
carts,
activeCartId,
activeCart,
items,
selectedAddressId,
selectedDeliveryMethodId,
shippingCost,
vatRate,
deliveryMethods,
productsTotal,
vatAmount,
orderTotal,
itemCount,
createCart,
deleteCart,
renameCart,
setActiveCart,
addItemToActiveCart,
updateQuantity,
deleteProduct,
removeItem,
clearCart,
setSelectedAddress,
setDeliveryMethod
}
})