1/** 2 * Cart store â shared state for the cart drawer and the nav badge. 3 * 4 * Holds one quantity per product (sticker, anhaenger), persisted in 5 * localStorage. Storing plain quantities is GDPR-safe: no personal data, 6 * strictly necessary for the shop function, so no consent gate needed. 7 * 8 * Every change dispatches `cart:change` on `document` so independent 9 * surfaces (drawer, nav badge) stay in sync without importing each other. 10 */ 11 12import { MAX_QTY, ANHAENGER_MAX_QTY } from './pricing.js?v=d5530e79454b'; 13 14const STORAGE_KEY = 'kwickey_cart_v1'; 15const PRODUCTS = ['sticker', 'anhaenger']; 16 17const state = { sticker: 0, anhaenger: 0 }; 18 19function clampQty(product, value) { 20 const max = product === 'anhaenger' ? ANHAENGER_MAX_QTY : MAX_QTY; 21 const n = Number(value); 22 if (!Number.isInteger(n) || n < 0) return 0; 23 return Math.min(n, max); 24} 25 26function load() { 27 try { 28 const raw = localStorage.getItem(STORAGE_KEY); 29 if (!raw) return; 30 const parsed = JSON.parse(raw); 31 PRODUCTS.forEach((p) => { state[p] = clampQty(p, parsed?.[p]); }); 32 } catch (_) { /* corrupt or unavailable storage â start empty */ } 33} 34 35function persist() { 36 try { 37 localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); 38 } catch (_) { /* private mode â cart lives for the session only */ } 39} 40 41function emitChange() { 42 document.dispatchEvent(new CustomEvent('cart:change', { 43 detail: { ...state, count: getCartCount() } 44 })); 45} 46 47load(); 48 49/** @param {'sticker'|'anhaenger'} product */ 50export function getQty(product) { 51 return state[product] || 0; 52} 53 54/** @param {'sticker'|'anhaenger'} product @param {number} qty */ 55export function setQty(product, qty) { 56 if (!PRODUCTS.includes(product)) return; 57 const next = clampQty(product, qty); 58 if (next === state[product]) return; 59 state[product] = next; 60 persist(); 61 emitChange(); 62} 63 64/** @param {'sticker'|'anhaenger'} product */ 65export function addOne(product) { 66 setQty(product, getQty(product) + 1); 67} 68 69export function getCartCount() { 70 return PRODUCTS.reduce((sum, p) => sum + state[p], 0); 71} 72 73export function clearCart() { 74 PRODUCTS.forEach((p) => { state[p] = 0; }); 75 persist(); 76 emitChange(); 77}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.