admin user and ui panel
This commit is contained in:
@@ -54,13 +54,42 @@ export const useCartStore = defineStore('cart', {
|
||||
}
|
||||
},
|
||||
addToCart(data: CartItem) {
|
||||
// Refuse a variant that cannot be ordered — out of stock and not flagged
|
||||
// pre-order. The same guard the admin cart applies (stores/cart.ts), kept
|
||||
// here so it holds even if a caller forgets to disable its button.
|
||||
const target = data.product?.variants?.find((v) => v.id === data.vid)
|
||||
if (!target) return false
|
||||
if (!((target.qty ?? 0) > 0 || target.isPreOrder)) return false
|
||||
|
||||
const index = this.find_index(data)
|
||||
if (index === -1) this.items.push(data)
|
||||
if (index === -1) this.items.push({ ...data, qty: this.capQty(data.qty, target) })
|
||||
else {
|
||||
this.items[index].product = data.product
|
||||
this.items[index].qty = data.qty
|
||||
this.items[index].qty = this.capQty(data.qty, target)
|
||||
}
|
||||
persist(this.items)
|
||||
return true
|
||||
},
|
||||
|
||||
/**
|
||||
* Stock ceiling for a cart line. A pre-order variant is orderable beyond
|
||||
* what is on hand (that is the point of pre-ordering), so it is uncapped;
|
||||
* anything else stops at its on-hand quantity.
|
||||
*/
|
||||
capQty(want: number, variant: { qty?: number; isPreOrder?: boolean }) {
|
||||
const n = Math.max(1, Math.floor(want) || 1)
|
||||
if (variant.isPreOrder) return n
|
||||
const max = variant.qty ?? 0
|
||||
return max > 0 ? Math.min(n, max) : n
|
||||
},
|
||||
|
||||
/** The ceiling for an item already in the cart; null when uncapped. */
|
||||
maxQty(item: Pick<CartItem, 'pid' | 'vid'>) {
|
||||
const i = this.find_index(item)
|
||||
if (i === -1) return null
|
||||
const v = this.variant(this.items[i])
|
||||
if (!v || v.isPreOrder) return null
|
||||
return (v.qty ?? 0) > 0 ? (v.qty as number) : null
|
||||
},
|
||||
remove(item: CartItem) {
|
||||
const index = this.find_index(item)
|
||||
@@ -72,6 +101,13 @@ export const useCartStore = defineStore('cart', {
|
||||
updateFeild(value: unknown, index: number | null, item: Pick<CartItem, 'pid' | 'vid'>, field: keyof CartItem) {
|
||||
if (!this.isInCart(item)) return false
|
||||
const i = index ?? this.find_index(item)
|
||||
// A quantity change is clamped to what is actually in stock (unless the
|
||||
// variant is pre-orderable), so the ceiling holds no matter which screen
|
||||
// drives the stepper.
|
||||
if (field === 'qty') {
|
||||
const v = this.variant(this.items[i])
|
||||
value = v ? this.capQty(Number(value), v) : Math.max(1, Number(value) || 1)
|
||||
}
|
||||
;(this.items[i] as any)[field] = value
|
||||
persist(this.items)
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user