mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 19:57:56 +01:00
feat(marketplace): slide-in cart drawer + quick-add on RevampIT browse cards (P3)
- CartProvider gains shared drawer state; CartDrawer renders globally. - CartIcon now opens the drawer (Button primitive) instead of navigating. - CartDrawer: review/remove items, total, pickup note, Zur Kasse + continue; Esc-to-close, scrim, body-scroll lock. - ListingCard refactored to the stretched-link pattern so a real add-to-cart Button can coexist with the card link (valid HTML). Quick-add shows only on RevampIT, non-compact, non-gratis cards — adding opens the drawer for feedback. Respects the card's no-floating-badge discipline (CTA sits in the body). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b479cd614b
commit
c9507f35ed
5 files changed
+217
-37
No files matched your search
+2
-1
@@ -1072,7 +1072,8 @@
|
||||
"continueShopping": "Weiter einkaufen",
|
||||
"pickupNote": "Abholung in Zürich — kostenlos",
|
||||
"revampitOnly": "Nur Revamp-IT-Geräte können in den Warenkorb. Community-Inserate werden direkt gekauft.",
|
||||
"cartAriaLabel": "Warenkorb, {count} Artikel"
|
||||
"cartAriaLabel": "Warenkorb, {count} Artikel",
|
||||
"closeAriaLabel": "Warenkorb schliessen"
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
|
||||
@@ -11,11 +11,15 @@
|
||||
* meta eyebrow, never as a colored pill floating on the image.
|
||||
*/
|
||||
|
||||
import { type MouseEvent } from 'react'
|
||||
import { ShoppingCart, Check } from 'lucide-react'
|
||||
import { Link } from '@/i18n/navigation'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { normalizeConditionValue, ZUSTAND_OPTIONS } from '@/config/erfassung/conditions'
|
||||
import { formatCHF } from '@/config/marketplace'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ListingImage } from './ListingImage'
|
||||
import { useCart } from './cart/CartProvider'
|
||||
import { ORG } from '@/config/org'
|
||||
|
||||
const CANONICAL_CONDITION_VALUES = new Set(ZUSTAND_OPTIONS.map(o => o.value))
|
||||
@@ -65,41 +69,83 @@ export function ListingCard({ listing, variant = 'default', className = '' }: Li
|
||||
].filter(Boolean) as string[]
|
||||
|
||||
const location = listing.pickup_location || listing.seller_city
|
||||
// RevampIT stock is cart-eligible; community P2P listings stay direct-buy.
|
||||
const showQuickAdd = listing.is_revampit && !isCompact && !isGratis
|
||||
|
||||
return (
|
||||
<Link
|
||||
href={`/marketplace/${listing.id}`}
|
||||
className={`group card-shell overflow-hidden hover:border-strong transition-colors block ${className}`}
|
||||
>
|
||||
<div className={`relative ${isCompact ? 'aspect-square' : 'aspect-4/3'}`}>
|
||||
<ListingImage
|
||||
src={listing.thumbnail}
|
||||
alt={listing.title}
|
||||
fallbackIconSize={isCompact ? 'w-8 h-8' : 'w-12 h-12'}
|
||||
/>
|
||||
</div>
|
||||
// Container is the positioning context for the stretched link, so a real
|
||||
// add-to-cart button can sit above it (valid HTML — no <button> in <a>).
|
||||
<div className={`group card-shell relative overflow-hidden hover:border-strong transition-colors ${className}`}>
|
||||
<Link
|
||||
href={`/marketplace/${listing.id}`}
|
||||
className="block after:absolute after:inset-0 after:content-['']"
|
||||
>
|
||||
<div className={`relative ${isCompact ? 'aspect-square' : 'aspect-4/3'}`}>
|
||||
<ListingImage
|
||||
src={listing.thumbnail}
|
||||
alt={listing.title}
|
||||
fallbackIconSize={isCompact ? 'w-8 h-8' : 'w-12 h-12'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className={isCompact ? 'p-3' : 'p-4'}>
|
||||
{eyebrowParts.length > 0 && (
|
||||
<div className="font-mono text-[10px] uppercase tracking-[0.18em] text-text-tertiary mb-2 truncate">
|
||||
{eyebrowParts.join(' · ')}
|
||||
<div className={isCompact ? 'p-3' : 'p-4'}>
|
||||
{eyebrowParts.length > 0 && (
|
||||
<div className="font-mono text-[10px] uppercase tracking-[0.18em] text-text-tertiary mb-2 truncate">
|
||||
{eyebrowParts.join(' · ')}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<h3 className={`font-semibold text-text-primary line-clamp-2 ${isCompact ? 'text-sm' : 'text-base'}`}>
|
||||
{listing.title}
|
||||
</h3>
|
||||
|
||||
<div className={`mt-3 font-mono tabular-nums ${isCompact ? 'text-base' : 'text-lg'} font-semibold text-text-primary`}>
|
||||
{isGratis ? t('listing.free').toUpperCase() : formatCHF(Number(listing.price_chf))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<h3 className={`font-semibold text-text-primary line-clamp-2 ${isCompact ? 'text-sm' : 'text-base'}`}>
|
||||
{listing.title}
|
||||
</h3>
|
||||
|
||||
<div className={`mt-3 font-mono tabular-nums ${isCompact ? 'text-base' : 'text-lg'} font-semibold text-text-primary`}>
|
||||
{isGratis ? t('listing.free').toUpperCase() : formatCHF(Number(listing.price_chf))}
|
||||
<div className="mt-3 pt-3 border-t border-subtle font-mono text-[11px] uppercase tracking-[0.14em] text-text-tertiary truncate">
|
||||
{sellerName}
|
||||
{location && <> · {location}</>}
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
|
||||
<div className="mt-3 pt-3 border-t border-subtle font-mono text-[11px] uppercase tracking-[0.14em] text-text-tertiary truncate">
|
||||
{sellerName}
|
||||
{location && <> · {location}</>}
|
||||
{showQuickAdd && (
|
||||
<div className="relative z-[1] px-4 pb-4">
|
||||
<CardQuickAdd listing={listing} />
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** Compact add-to-cart for RevampIT browse cards. Stops link navigation. */
|
||||
function CardQuickAdd({ listing }: { listing: ListingCardData }) {
|
||||
const t = useTranslations('marketplace.cart')
|
||||
const { has, add, openDrawer } = useCart()
|
||||
const inCart = has(listing.id)
|
||||
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
if (!inCart) {
|
||||
add({
|
||||
id: listing.id,
|
||||
title: listing.title,
|
||||
priceChf: Number(listing.price_chf),
|
||||
thumbnail: listing.thumbnail,
|
||||
category: listing.category,
|
||||
condition: listing.condition,
|
||||
})
|
||||
}
|
||||
openDrawer()
|
||||
}
|
||||
|
||||
return (
|
||||
<Button onClick={handleClick} variant={inCart ? 'outline' : 'primary'} size="sm" className="w-full gap-2">
|
||||
{inCart ? <Check className="h-4 w-4" aria-hidden="true" /> : <ShoppingCart className="h-4 w-4" aria-hidden="true" />}
|
||||
{inCart ? t('inCart') : t('addToCart')}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect } from 'react'
|
||||
import Image from 'next/image'
|
||||
import { X, ShoppingCart, Package, Trash2 } from 'lucide-react'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Link } from '@/i18n/navigation'
|
||||
import { formatCHF } from '@/config/marketplace'
|
||||
import { useCart } from './CartProvider'
|
||||
|
||||
/**
|
||||
* Slide-in cart drawer from the right. Opened by CartIcon, lets the buyer
|
||||
* review/remove items and proceed to the cart page without leaving the
|
||||
* marketplace. RevampIT-only stock (see CartProvider).
|
||||
*/
|
||||
export function CartDrawer() {
|
||||
const t = useTranslations('marketplace.cart')
|
||||
const { items, total, count, drawerOpen, closeDrawer, remove } = useCart()
|
||||
|
||||
// Esc closes; lock body scroll while open.
|
||||
useEffect(() => {
|
||||
if (!drawerOpen) return
|
||||
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') closeDrawer() }
|
||||
window.addEventListener('keydown', onKey)
|
||||
const prevOverflow = document.body.style.overflow
|
||||
document.body.style.overflow = 'hidden'
|
||||
return () => {
|
||||
window.removeEventListener('keydown', onKey)
|
||||
document.body.style.overflow = prevOverflow
|
||||
}
|
||||
}, [drawerOpen, closeDrawer])
|
||||
|
||||
if (!drawerOpen) return null
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[100] flex justify-end" role="dialog" aria-modal="true" aria-label={t('title')}>
|
||||
{/* Scrim */}
|
||||
<div className="absolute inset-0 bg-black/50" onClick={closeDrawer} aria-hidden="true" />
|
||||
|
||||
{/* Panel */}
|
||||
<div className="relative flex h-full w-full max-w-sm flex-col border-l border-strong bg-surface-base">
|
||||
<header className="flex items-center justify-between border-b border-subtle px-5 py-4">
|
||||
<h2 className="flex items-center gap-2 text-base font-semibold text-text-primary">
|
||||
<ShoppingCart className="h-5 w-5" aria-hidden="true" />
|
||||
{t('title')}
|
||||
{count > 0 && <span className="text-text-tertiary">({count})</span>}
|
||||
</h2>
|
||||
<Button variant="ghost" size="icon" onClick={closeDrawer} aria-label={t('closeAriaLabel')}>
|
||||
<X className="h-5 w-5" aria-hidden="true" />
|
||||
</Button>
|
||||
</header>
|
||||
|
||||
{items.length === 0 ? (
|
||||
<div className="flex flex-1 flex-col items-center justify-center gap-4 px-6 text-center">
|
||||
<ShoppingCart className="h-12 w-12 text-text-muted" aria-hidden="true" />
|
||||
<p className="text-text-secondary">{t('empty')}</p>
|
||||
<Button as={Link} href="/marketplace" variant="outline" onClick={closeDrawer}>
|
||||
{t('emptyCta')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<ul className="flex-1 divide-y divide-subtle overflow-y-auto px-5">
|
||||
{items.map(item => (
|
||||
<li key={item.id} className="flex gap-3 py-4">
|
||||
<div className="h-14 w-14 shrink-0 overflow-hidden rounded-lg bg-surface-raised">
|
||||
{item.thumbnail ? (
|
||||
<Image src={item.thumbnail} alt={item.title} width={56} height={56} className="h-full w-full object-cover" />
|
||||
) : (
|
||||
<div className="flex h-full w-full items-center justify-center">
|
||||
<Package className="h-6 w-6 text-text-muted" aria-hidden="true" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<Link href={`/marketplace/${item.id}`} onClick={closeDrawer} className="line-clamp-2 text-sm font-medium text-text-primary hover:text-action">
|
||||
{item.title}
|
||||
</Link>
|
||||
<p className="mt-1 font-mono text-sm tabular-nums text-text-secondary">{formatCHF(Number(item.priceChf))}</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => remove(item.id)}
|
||||
aria-label={t('remove')}
|
||||
className="shrink-0 text-text-tertiary hover:text-error-600"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" aria-hidden="true" />
|
||||
</Button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
<footer className="border-t border-subtle px-5 py-4">
|
||||
<div className="mb-1 flex items-center justify-between">
|
||||
<span className="text-sm text-text-secondary">{t('total')}</span>
|
||||
<span className="font-mono text-lg font-bold tabular-nums text-text-primary">{formatCHF(total)}</span>
|
||||
</div>
|
||||
<p className="mb-3 text-xs text-text-tertiary">{t('pickupNote')}</p>
|
||||
<div className="flex flex-col gap-2">
|
||||
<Button as={Link} href="/marketplace/cart" variant="primary" className="w-full" onClick={closeDrawer}>
|
||||
{t('checkout')}
|
||||
</Button>
|
||||
<Button variant="ghost" className="w-full" onClick={closeDrawer}>
|
||||
{t('continueShopping')}
|
||||
</Button>
|
||||
</div>
|
||||
</footer>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -2,20 +2,22 @@
|
||||
|
||||
import { ShoppingCart } from 'lucide-react'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { Link } from '@/i18n/navigation'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { useCart } from './CartProvider'
|
||||
|
||||
/** Cart link with item-count badge. Shows nothing-special when empty. */
|
||||
/** Cart trigger with item-count badge — opens the slide-in drawer. */
|
||||
export function CartIcon({ className }: { className?: string }) {
|
||||
const t = useTranslations('marketplace.cart')
|
||||
const { count, hydrated } = useCart()
|
||||
const { count, hydrated, openDrawer } = useCart()
|
||||
const shown = hydrated ? count : 0
|
||||
|
||||
return (
|
||||
<Link
|
||||
href="/marketplace/cart"
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={openDrawer}
|
||||
aria-label={t('cartAriaLabel', { count: shown })}
|
||||
className={`relative inline-flex h-10 w-10 items-center justify-center rounded-md text-text-secondary transition-colors hover:bg-surface-raised hover:text-text-primary focus:outline-hidden focus:ring-2 focus:ring-action focus:ring-offset-2 ${className ?? ''}`}
|
||||
className={`relative text-text-secondary hover:text-text-primary ${className ?? ''}`}
|
||||
>
|
||||
<ShoppingCart className="h-5 w-5" aria-hidden="true" />
|
||||
{shown > 0 && (
|
||||
@@ -23,6 +25,6 @@ export function CartIcon({ className }: { className?: string }) {
|
||||
{shown}
|
||||
</span>
|
||||
)}
|
||||
</Link>
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
* stay direct-buy / contact-seller), matching the Galaxus-vs-Ricardo split.
|
||||
*/
|
||||
import { createContext, useContext, useEffect, useState, useCallback, useMemo, type ReactNode } from 'react'
|
||||
import { CartDrawer } from './CartDrawer'
|
||||
|
||||
export interface CartItem {
|
||||
/** listing id */
|
||||
@@ -29,6 +30,10 @@ interface CartState {
|
||||
add: (item: CartItem) => void
|
||||
remove: (id: string) => void
|
||||
clear: () => void
|
||||
/** Slide-in drawer visibility (shared by CartIcon trigger + CartDrawer). */
|
||||
drawerOpen: boolean
|
||||
openDrawer: () => void
|
||||
closeDrawer: () => void
|
||||
}
|
||||
|
||||
const CartContext = createContext<CartState | null>(null)
|
||||
@@ -37,6 +42,7 @@ const STORAGE_KEY = 'revampit-cart-v1'
|
||||
export function CartProvider({ children }: { children: ReactNode }) {
|
||||
const [items, setItems] = useState<CartItem[]>([])
|
||||
const [hydrated, setHydrated] = useState(false)
|
||||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||||
|
||||
// Load once on mount (client only).
|
||||
useEffect(() => {
|
||||
@@ -69,6 +75,8 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
||||
setItems((prev) => prev.filter((i) => i.id !== id))
|
||||
}, [])
|
||||
const clear = useCallback(() => setItems([]), [])
|
||||
const openDrawer = useCallback(() => setDrawerOpen(true), [])
|
||||
const closeDrawer = useCallback(() => setDrawerOpen(false), [])
|
||||
|
||||
const value = useMemo<CartState>(() => {
|
||||
const total = items.reduce((sum, i) => sum + (Number(i.priceChf) || 0), 0)
|
||||
@@ -81,10 +89,18 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
||||
add,
|
||||
remove,
|
||||
clear,
|
||||
drawerOpen,
|
||||
openDrawer,
|
||||
closeDrawer,
|
||||
}
|
||||
}, [items, hydrated, add, remove, clear])
|
||||
}, [items, hydrated, add, remove, clear, drawerOpen, openDrawer, closeDrawer])
|
||||
|
||||
return <CartContext.Provider value={value}>{children}</CartContext.Provider>
|
||||
return (
|
||||
<CartContext.Provider value={value}>
|
||||
{children}
|
||||
<CartDrawer />
|
||||
</CartContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useCart(): CartState {
|
||||
|
||||
Reference in new issue
Block a user