49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import type { Dish, Size } from '~/server/utils/store'
|
|
|
|
export interface CartLine {
|
|
key: string
|
|
dishId: string
|
|
name: string
|
|
img?: string
|
|
size: Size
|
|
meat: string
|
|
sauce?: string
|
|
extras: string[]
|
|
extrasLabel: string[]
|
|
note?: string
|
|
qty: number
|
|
unit: number
|
|
}
|
|
|
|
export function useCart() {
|
|
const lines = useState<CartLine[]>('cart', () => [])
|
|
const mode = useState<'livraison' | 'retrait'>('mode', () => 'livraison')
|
|
const bump = useState('cartBump', () => 0)
|
|
|
|
const count = computed(() => lines.value.reduce((a, l) => a + l.qty, 0))
|
|
const subtotal = computed(() => lines.value.reduce((a, l) => a + l.unit * l.qty, 0))
|
|
|
|
function add(line: Omit<CartLine, 'key'>) {
|
|
const same = lines.value.find(l => l.dishId === line.dishId && l.size === line.size && l.meat === line.meat
|
|
&& l.sauce === line.sauce && l.note === line.note && l.extras.join() === line.extras.join())
|
|
if (same) same.qty += line.qty
|
|
else lines.value.push({ ...line, key: Math.random().toString(36).slice(2) })
|
|
bump.value++
|
|
}
|
|
function setQty(key: string, qty: number) {
|
|
const l = lines.value.find(x => x.key === key)
|
|
if (!l) return
|
|
if (qty <= 0) lines.value = lines.value.filter(x => x.key !== key)
|
|
else l.qty = Math.min(20, qty)
|
|
}
|
|
function remove(key: string) { lines.value = lines.value.filter(x => x.key !== key) }
|
|
function clear() { lines.value = [] }
|
|
|
|
return { lines, mode, bump, count, subtotal, add, setQty, remove, clear }
|
|
}
|
|
|
|
// Fenêtre de personnalisation d'un plat (partagée par toutes les pages)
|
|
export function useCustomize() {
|
|
const dish = useState<Dish | null>('customizeDish', () => null)
|
|
return { dish, open: (d: Dish) => { dish.value = d }, close: () => { dish.value = null } }
|
|
}
|