a50524832e
- Removed the inline "Nur noch wenige verfügbar" text hint from AddToCartButton/AddToCartInlineButton (was making card heights vary in every grid that renders them — RelatedProducts, ProductSpotlight's CTA row) — now only shown via the same image-overlaid pill badge Ausverkauft/discount already use (position: absolute, doesn't affect layout). Added that badge to RelatedProducts.tsx and todo-cards' Pricing.tsx, which didn't have it before. - RelatedProducts cards now also show "inkl. X% MwSt." (was missing entirely) - VatBreakdown rows are now flex rows with a spacer instead of plain text, so every € amount right-aligns to the same edge regardless of how many digits the rate itself has (was visibly staggered with mixed 7%/19% rates) - Cart's manual discount-code field only renders when Payload actually has at least one active code right now (lib/discountServer.ts's new hasActiveDiscountCode()) — no point showing an open field that could never validate. An already-applied code (e.g. from an older session) still always shows its own result row regardless. - Checkout's "1. Rechnungsadresse" no longer offers a Packstation option — a Packstation isn't a valid billing address for an invoice. Only a plain street address now; Packstation is only offered on the separate, optional "Abweichende Lieferadresse" section, which already had its own address/Packstation toggle. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
116 lines
4.9 KiB
TypeScript
116 lines
4.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import Image from "next/image";
|
|
import { addToCart } from "../lib/cart";
|
|
import { useCartFly } from "./CartFly";
|
|
|
|
// Exported so consumers like RelatedProducts.tsx can delay their own
|
|
// follow-up UI changes (e.g. swapping out this exact card) until after
|
|
// the success state has actually had time to be seen.
|
|
export const FEEDBACK_MS = 2000;
|
|
|
|
/**
|
|
* Add-to-cart button that stays on the page (unlike AddToCartButton, which
|
|
* navigates to /cart) — used wherever a product grid lets you keep browsing,
|
|
* so a click needs its own success confirmation instead of relying on the
|
|
* navigation itself as feedback.
|
|
*/
|
|
export function AddToCartInlineButton({
|
|
id,
|
|
label = "In den Warenkorb",
|
|
className,
|
|
outOfStock = false,
|
|
variants = [],
|
|
}: {
|
|
id: string;
|
|
label?: string;
|
|
className?: string;
|
|
/** Product-level — only meaningful when `variants` is empty. A varianted
|
|
* product's buyability is entirely per-variant instead (see below). */
|
|
outOfStock?: boolean;
|
|
/** Optional — products.variants (name + optional priceOverride + its own
|
|
* outOfStock). When non-empty, a variant must be picked (defaults to the
|
|
* first *in-stock* one, or just the first if all are out) before "add to
|
|
* cart" is enabled — the selected variant's name is snapshotted onto the
|
|
* cart line and, later, the order itself. */
|
|
variants?: { name: string; priceOverride: number | null; outOfStock: boolean; lowStock: boolean }[];
|
|
}) {
|
|
const [added, setAdded] = useState(false);
|
|
const [selectedVariant, setSelectedVariant] = useState(variants.find((v) => !v.outOfStock)?.name ?? variants[0]?.name);
|
|
const timeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
const { fly } = useCartFly();
|
|
|
|
useEffect(() => () => clearTimeout(timeoutRef.current), []);
|
|
|
|
// Whichever is actually being offered right now — the selected variant's
|
|
// own flag if there are variants, otherwise the plain product-level one.
|
|
const currentlyOutOfStock = variants.length > 0 ? (variants.find((v) => v.name === selectedVariant)?.outOfStock ?? false) : outOfStock;
|
|
|
|
function handleClick() {
|
|
if (currentlyOutOfStock) return;
|
|
addToCart(id, 1, selectedVariant);
|
|
if (buttonRef.current) fly(buttonRef.current);
|
|
setAdded(true);
|
|
clearTimeout(timeoutRef.current);
|
|
timeoutRef.current = setTimeout(() => setAdded(false), FEEDBACK_MS);
|
|
}
|
|
|
|
const base =
|
|
className ??
|
|
"flex items-center justify-between px-5 py-3 rounded-sm border w-full transition-all duration-200 active:scale-[0.97] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand focus-visible:ring-offset-2 focus-visible:ring-offset-bg-base";
|
|
// Branch the border/bg classes instead of appending an "override" on top
|
|
// of the default ones — Tailwind v4 has no leading-`!` important prefix
|
|
// anymore (it's a trailing `!` now), so two conflicting utilities like
|
|
// border-border/border-success both being present would silently race on
|
|
// CSS source order instead of one cleanly winning.
|
|
const stateClasses = currentlyOutOfStock
|
|
? "border-border opacity-60 cursor-not-allowed"
|
|
: added
|
|
? "border-success bg-success-subtle"
|
|
: "border-border hover:border-brand";
|
|
|
|
return (
|
|
// Low stock isn't shown as its own text line here (see
|
|
// AddToCartButton.tsx's identical comment on why) — the image-overlaid
|
|
// pill badge (ProductGrid.tsx/RelatedProducts.tsx, position: absolute,
|
|
// outside layout flow) is where this shows now, same as
|
|
// Ausverkauft/discount already do.
|
|
<div className="flex flex-col gap-2 w-full">
|
|
{variants.length > 0 && (
|
|
<select
|
|
value={selectedVariant}
|
|
onChange={(e) => setSelectedVariant(e.target.value)}
|
|
className="w-full rounded-sm border border-border px-3 py-2 text-body-sm text-text-primary bg-bg-base focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand"
|
|
aria-label="Variante auswählen"
|
|
>
|
|
{variants.map((v) => (
|
|
<option key={v.name} value={v.name}>
|
|
{v.name}
|
|
{v.outOfStock ? " (ausverkauft)" : v.lowStock ? " (nur noch wenige)" : ""}
|
|
</option>
|
|
))}
|
|
</select>
|
|
)}
|
|
<button
|
|
ref={buttonRef}
|
|
type="button"
|
|
onClick={handleClick}
|
|
disabled={currentlyOutOfStock}
|
|
className={`${base} ${stateClasses}`}
|
|
>
|
|
<span
|
|
className={
|
|
"text-body-sm transition-colors " +
|
|
(currentlyOutOfStock ? "text-text-muted" : added ? "font-semibold text-success" : "text-text-primary")
|
|
}
|
|
>
|
|
{currentlyOutOfStock ? "Ausverkauft" : added ? "Hinzugefügt ✓" : label}
|
|
</span>
|
|
<Image alt="" src="/icon-cart-outline.png" width={32} height={30} className="h-[1.875rem] w-8 object-contain" />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|