'use client'; import { useCallback, useEffect, useState } from 'react'; import type { Checkout, PickupPoint } from 'brainerce'; import { getClient } from '@/core/lib/brainerce'; import { useTranslations } from '@/core/lib/translations'; import { cn } from '@/core/lib/utils'; interface PickupPointStepProps { checkoutId: string; /** The point already chosen on this checkout, if any (a reload keeps it). */ chosenId?: string | null; /** Called with the checkout the server returns once a point is stored. */ onChosen: (checkout: Checkout) => void; className?: string; } type Status = 'loading' | 'ready' | 'error'; /** * Where the parcel goes, for a shipping rate that delivers to a carrier pickup point (a partner shop * or a locker): the points nearest the shipping address, one to choose. * * Three states the shopper must be able to tell apart, because they mean different things: * - an empty list: no point is near. Offer a search elsewhere. * - an error: the carrier could not be reached. Offer a retry. NEVER say "none near you": it would * quietly take the option away from a shopper who has one. * - a rejected choice (the point stopped being listed): refresh the list. * * `hours` and `note` are the carrier's own words and are often absent: shown as written, never parsed. */ export function PickupPointStep({ checkoutId, chosenId = null, onChosen, className, }: PickupPointStepProps) { const t = useTranslations('checkout'); const [points, setPoints] = useState([]); const [status, setStatus] = useState('loading'); const [saving, setSaving] = useState(null); const [saveError, setSaveError] = useState(false); const [city, setCity] = useState(''); const [searchedCity, setSearchedCity] = useState(null); const load = useCallback( async (searchCity: string | null) => { setStatus('loading'); setSaveError(false); try { const found = await getClient().getPickupPoints( checkoutId, searchCity ? { city: searchCity } : {} ); setPoints(found); setStatus('ready'); } catch { setStatus('error'); } }, [checkoutId] ); useEffect(() => { void load(null); }, [load]); async function choose(point: PickupPoint) { setSaving(point.id); setSaveError(false); try { onChosen(await getClient().selectPickupPoint(checkoutId, point)); } catch { // The point may have closed since the list was read: show the current list again. setSaveError(true); await load(searchedCity); } finally { setSaving(null); } } function search(event: React.FormEvent) { event.preventDefault(); const value = city.trim(); setSearchedCity(value || null); void load(value || null); } return (

{t('pickupPointIntro')}

setCity(e.target.value)} placeholder={t('pickupPointSearchPlaceholder')} aria-label={t('pickupPointSearchPlaceholder')} autoComplete="address-level2" className="border-border bg-background text-foreground min-w-0 flex-1 rounded border px-3 py-2 text-sm" />
{saveError && (

{t('pickupPointNoLongerAvailable')}

)} {status === 'loading' && (

{t('pickupPointLoading')}

)} {status === 'error' && (

{t('pickupPointLoadFailed')}

)} {status === 'ready' && points.length === 0 && (

{t('pickupPointNone')}

{t('pickupPointNoneHint')}

)} {status === 'ready' && points.length > 0 && (
    {points.map((point) => { const isChosen = chosenId === point.id; return (
  • ); })}
)}
); }