From e3c571ee4fb71f1428e72b10d335ae307bb3528a Mon Sep 17 00:00:00 2001 From: seaznCode Date: Tue, 5 May 2026 22:07:56 +0200 Subject: [PATCH] Refactor CoffeeSelectionGrid to improve quantity handling and enhance user interface with detailed pack information Co-authored-by: Copilot --- .../components/CoffeeSelectionGrid.tsx | 154 ++++++++---------- 1 file changed, 71 insertions(+), 83 deletions(-) diff --git a/src/app/coffee-abonnements/components/CoffeeSelectionGrid.tsx b/src/app/coffee-abonnements/components/CoffeeSelectionGrid.tsx index 91c1310..5b3a752 100644 --- a/src/app/coffee-abonnements/components/CoffeeSelectionGrid.tsx +++ b/src/app/coffee-abonnements/components/CoffeeSelectionGrid.tsx @@ -1,16 +1,15 @@ import Link from 'next/link'; import type { CoffeeItem } from '../hooks/getActiveCoffees'; +import { CAPSULES_PER_PACK, MAX_ABO_PACKS, MIN_ABO_PACKS, packsToCapsules } from '../lib/orderRules'; type Props = { coffees: CoffeeItem[]; loading: boolean; error: string | null; selections: Record; - bump: Record; - selectedPlanCapsules: number; - totalCapsules: number; - onToggleCoffee: (id: string) => void; - onChangeQuantity: (id: string, delta: number) => void; + totalPacks: number; + onAdjustQuantity: (id: string, delta: number) => void; + onSetQuantity: (id: string, nextQuantity: number) => void; title: string; }; @@ -19,16 +18,29 @@ export default function CoffeeSelectionGrid({ loading, error, selections, - bump, - selectedPlanCapsules, - totalCapsules, - onToggleCoffee, - onChangeQuantity, + totalPacks, + onAdjustQuantity, + onSetQuantity, title, }: Props) { + const remainingCapacity = Math.max(0, MAX_ABO_PACKS - totalPacks); + return (
-

{title}

+
+
+

{title}

+

Choose your coffees in packs. One pack contains {CAPSULES_PER_PACK} capsules.

+
+ Minimum order: {MIN_ABO_PACKS} packs ({packsToCapsules(MIN_ABO_PACKS)} capsules). +
+
+
+
Capacity
+
{remainingCapacity.toLocaleString('en-US')} packs left
+
up to {MAX_ABO_PACKS.toLocaleString('en-US')} packs per subscription
+
+
{error && (
{error}
@@ -46,13 +58,9 @@ export default function CoffeeSelectionGrid({ {coffees.map((coffee) => { const active = coffee.id in selections; const qty = selections[coffee.id] || 0; - const remainingCapsules = selectedPlanCapsules - totalCapsules; - const maxForCoffee = active ? Math.min(120, qty + remainingCapsules) : 0; - const sliderMax = Math.max(10, maxForCoffee); - const sliderProgress = sliderMax <= 10 - ? 100 - : Math.min(100, Math.max(0, ((qty - 10) / (sliderMax - 10)) * 100)); - const canAddCoffee = active || remainingCapsules >= 10; + const maxForCoffee = qty + remainingCapacity; + const addableForCoffee = remainingCapacity; + const subtotal = qty * coffee.pricePer10; return (
- EUR {coffee.pricePer10} + EUR {coffee.pricePer10.toFixed(2)} - per 10 + per pack
@@ -89,79 +97,59 @@ export default function CoffeeSelectionGrid({

{coffee.description}

- View
- {active && ( -
-
- Quantity (10-{maxForCoffee} pcs) - - {qty} pcs - +
+
+
+
Pack selection
+
{packsToCapsules(qty).toLocaleString('en-US')} capsules
-
- -
- onChangeQuantity(coffee.id, parseInt(e.target.value, 10) - qty)} - className="w-full appearance-none cursor-pointer bg-transparent" - style={{ - background: - 'linear-gradient(to right,#0f172a 0%,#0f172a ' + - sliderProgress + - '%,#e2e8f0 ' + - sliderProgress + - '%,#e2e8f0 100%)', - height: '6px', - borderRadius: '999px', - }} - /> -
- -
-
- Subtotal - EUR {((qty / 10) * coffee.pricePer10).toFixed(2)} +
+ {qty.toLocaleString('en-US')} packs
- )} + +
+ + onSetQuantity(coffee.id, Number(e.target.value))} + className="h-10 w-full rounded-xl border border-slate-200 bg-white px-3 text-center text-sm font-semibold text-slate-900 shadow-sm focus:border-slate-900 focus:outline-none focus:ring-2 focus:ring-slate-900/10" + /> + +
+ +
+ You can add {addableForCoffee.toLocaleString('en-US')} more packs here. + EUR {subtotal.toFixed(2)} +
+
); })}