new init
This commit is contained in:
@@ -19,6 +19,18 @@ function applyOrderColorsFromStorage() {
|
||||
document.documentElement.style.setProperty("--order-bg2", c2);
|
||||
}
|
||||
|
||||
// --- NEU: lokale Bestände anhand des Warenkorbs reduzieren
|
||||
function decrementStockLocal(prevProducts, cart) {
|
||||
const delta = {};
|
||||
for (const { product, quantity } of Object.values(cart)) {
|
||||
delta[product.id] = (delta[product.id] || 0) + quantity;
|
||||
}
|
||||
return prevProducts.map((p) =>
|
||||
delta[p.id] ? { ...p, stock: (Number(p.stock) || 0) - delta[p.id] } : p
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
@@ -53,6 +65,11 @@ function chipStyleFor(cat, active) {
|
||||
return active ? strong(h) : soft(h);
|
||||
}
|
||||
|
||||
function ringColorForCategory(cat) {
|
||||
if (!cat) return "#22c55e";
|
||||
// nutzt deine bestehende Farblogik der Chips:
|
||||
return chipStyleFor(cat, true).br; // starke Border-Farbe der Kategorie
|
||||
}
|
||||
// robustes Aktiv-Flag (API kann bool/zahl/string liefern)
|
||||
function isActiveProduct(p) {
|
||||
const v = p?.is_active;
|
||||
@@ -236,11 +253,15 @@ const avatarSrc = useMemo(() => {
|
||||
product.price_cents * quantity
|
||||
);
|
||||
}
|
||||
setProducts((prev) => decrementStockLocal(prev, cart));
|
||||
|
||||
try {
|
||||
const refreshed = await getCurrentUser();
|
||||
if (refreshed) setUser(refreshed);
|
||||
} catch {}
|
||||
|
||||
setCart({});
|
||||
|
||||
try { await logout(); } finally { navigate("/", { replace: true }); }
|
||||
} catch (e) {
|
||||
setError(e?.message || "Bezahlen fehlgeschlagen.");
|
||||
@@ -318,6 +339,7 @@ const avatarSrc = useMemo(() => {
|
||||
isFavorite={favorites.has(p.id)}
|
||||
onToggleFavorite={handleToggleFavorite}
|
||||
count={cart[p.id]?.quantity || 0}
|
||||
ringColor={ringColorForCategory(p.category)}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user