/** * Booking: where a pickup point service delivers (PMSG-347). * * - The customer's locker or shop is checked against the provider's places near their postcode, * as lockers and shops do close; if it has gone, the merchant picks another. * - The merchant can change a place that is still offered (e.g. the customer asked for another); * a note then reminds them to tell the customer. * - A pickup service with no place of its provider on the order (e.g. a Flat rate order, or a * service of another carrier): the merchant picks one near the customer. * - A home delivery service for a customer who chose a pickup point: a warning. * - A pickup service with no provider can't be booked here: choose another service. */ import { useEffect, useMemo, useState } from 'react'; import { __, sprintf } from '@wordpress/i18n'; import { createInterpolateElement } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; import { Button, Card, CardBody, CardHeader, Flex, Notice, SelectControl, Spinner } from '@wordpress/components'; import type { Order, OrderPickupPoint, Quote } from '../../../types'; import { bookingPickupPoint, hasPickupLookup, isPickupQuote, pickupKind, pickupPointText, pickupProvider, } from '../utils/pickupPoint'; /** A place from GET /pickup-points/search. */ interface PickupPlace { id: number; name: string; address1: string; address2: string; postcode: string; distanceMiles: number | null; isLocker: boolean; } interface SearchResponse { pickupPoints: PickupPlace[]; } export type PickupPointStatus = /** Not a pickup service, or the place is confirmed: booking can go ahead. */ | 'ok' /** Still checking the place. */ | 'checking' /** The merchant must pick a place first. */ | 'needs-pick' /** A pickup service with no provider: choose another service. */ | 'unsupported'; /** * What the section should say for a booking: whether to warn, check or ask for a place. * * @param order The order as this booking sends it. * @param quote The service being booked. * @param places The provider's places near the customer (null while loading or on error). */ export function pickupPointStatus(order: Order, quote: Quote | null, places: PickupPlace[] | null): PickupPointStatus { if (!isPickupQuote(quote)) return 'ok'; if (!hasPickupLookup(quote)) return 'unsupported'; const point = bookingPickupPoint(order, quote); if (!places) return point ? 'checking' : 'needs-pick'; return point && places.some((place) => place.id === point.id) ? 'ok' : 'needs-pick'; } /** * The customer's checkout choice when this booking goes to another place of the same carrier (the * merchant changed it), so the merchant is reminded to tell them. null otherwise. */ export function replacedChoice( customerPoint: OrderPickupPoint | null, bookingPoint: OrderPickupPoint | null ): OrderPickupPoint | null { if (!customerPoint || !bookingPoint) return null; const sameCarrier = customerPoint.provider.toUpperCase() === bookingPoint.provider.toUpperCase(); return sameCarrier && customerPoint.id !== bookingPoint.id ? customerPoint : null; } /** * The customer's checkout choice when this booking is another carrier's pickup service (e.g. they * chose an InPost locker and the merchant books Evri ParcelShop to ParcelShop): they will collect * somewhere else, so the merchant is reminded. null otherwise. */ export function otherCarrierChoice(customerPoint: OrderPickupPoint | null, provider: string): OrderPickupPoint | null { if (!customerPoint || !provider) return null; return customerPoint.provider.toUpperCase() !== provider.toUpperCase() ? customerPoint : null; } /** "Name (postcode)": enough for the merchant to recognise a place in a reminder. */ export function shortPlace(point: Pick): string { const name = point.name.trim(); const postcode = point.postcode.trim(); return postcode ? `${name} (${postcode})` : name; } function toOrderPoint(place: PickupPlace, provider: string): OrderPickupPoint { return { id: place.id, provider, name: place.name, address1: place.address1, address2: place.address2, postcode: place.postcode, isLocker: place.isLocker, }; } interface PickupPointSectionProps { /** The order as this booking sends it (with the merchant's pick, if any). */ order: Order; /** The place the customer chose at checkout, before any pick of the merchant's. */ customerChoice: OrderPickupPoint | null; quote: Quote | null; /** The merchant picked a place for this booking. */ onPick: (point: OrderPickupPoint) => void; /** Told whether booking must wait for a place. */ onStatusChange: (status: PickupPointStatus) => void; } export default function PickupPointSection({ order, customerChoice, quote, onPick, onStatusChange }: PickupPointSectionProps) { const isPickup = isPickupQuote(quote); const provider = pickupProvider(quote); const canLookUp = hasPickupLookup(quote); const postcode = order?.shipping?.postcode ?? ''; const [places, setPlaces] = useState(null); // Locker or ParcelShop, from the quote's tags. const kind = pickupKind(quote); const [error, setError] = useState(null); // The merchant opened the list to change a confirmed place. const [changing, setChanging] = useState(false); // The merchant closed the other-carrier reminder; it shows again for another service. const [otherCarrierClosed, setOtherCarrierClosed] = useState(false); useEffect(() => { setPlaces(null); setError(null); setChanging(false); setOtherCarrierClosed(false); if (!canLookUp) return; let cancelled = false; ( apiFetch({ path: `/parcel2go-shipping/v1/pickup-points/search?provider=${encodeURIComponent(provider)}&postcode=${encodeURIComponent(postcode)}`, }) as Promise ) .then((response) => { if (cancelled) return; setPlaces(response.pickupPoints ?? []); }) .catch((e: { message?: string }) => { if (!cancelled) setError(e?.message || __('Pickup points could not be loaded.', 'parcel2go-shipping')); }); return () => { cancelled = true; }; }, [canLookUp, provider, postcode, kind]); const status = pickupPointStatus(order, quote, places); useEffect(() => onStatusChange(status), [status, onStatusChange]); const current = bookingPickupPoint(order, quote); const customerPoint = customerChoice; const place = kind === 'locker' ? __('locker', 'parcel2go-shipping') : __('ParcelShop', 'parcel2go-shipping'); const options = useMemo( () => [ { label: sprintf(/* translators: %s: locker or ParcelShop */ __('Choose a %s', 'parcel2go-shipping'), place), value: '' }, ...(places ?? []).map((p) => ({ label: `${pickupPointText(p)}${p.distanceMiles !== null ? ` (${p.distanceMiles} mi)` : ''}`, value: String(p.id), })), ], [places, place] ); // Home delivery for a customer who chose to collect. if (!isPickup) { return customerPoint ? ( {sprintf( /* translators: %s: the customer's pickup point */ __('The customer chose to collect from %s. This service delivers to their address.', 'parcel2go-shipping'), pickupPointText(customerPoint) )} ) : null; } if (!canLookUp) { return ( {__('Customer collects from', 'parcel2go-shipping')} {__("This service delivers to a pickup point that can't be chosen here, so it can't be booked. Choose another service.", 'parcel2go-shipping')} ); } const confirmed = status === 'ok' && current; const lost = !!current && places !== null && status === 'needs-pick'; // The merchant's pick replaced the place the customer chose at checkout: they should be told. const replacedCustomerChoice = confirmed ? replacedChoice(customerPoint, confirmed) : null; // The customer chose the other carrier's locker or shop at checkout. const otherCarrier = otherCarrierChoice(customerPoint, provider); const showPicker = places !== null && places.length > 0 && (!confirmed || changing); const loading = places === null && !error; return ( {__('Customer collects from', 'parcel2go-shipping')} {loading ? ( // Nothing until the places are in. ) : ( {otherCarrier && !otherCarrierClosed && ( setOtherCarrierClosed(true)}> {createInterpolateElement( sprintf( /* translators: 1: locker or ParcelShop (twice). is the place the customer chose, e.g. "Post Office - The Markets (LS2 7DZ)". */ __('The customer chose at checkout. This service delivers to a %1$s, so tell them which %1$s to collect from.', 'parcel2go-shipping'), place ), { place: {shortPlace(otherCarrier)} } )} )} {current && (

{pickupPointText(current)}

{confirmed && places !== null && places.length > 1 && ( )}
)} {replacedCustomerChoice && ( {createInterpolateElement( sprintf( /* translators: %s: locker or ParcelShop. is the place the customer chose, e.g. "InPost Locker - Trinity Leeds (LS2 8LP)". */ __('The customer chose at checkout. Tell them which %s to collect from.', 'parcel2go-shipping'), place ), { place: {shortPlace(replacedCustomerChoice)} } )} )} {!current && (

{sprintf( /* translators: %s: locker or ParcelShop */ __('This service delivers to a %s the customer collects from. Choose one near their address.', 'parcel2go-shipping'), place )}

)} {lost && ( {sprintf( /* translators: %s: locker or ParcelShop */ __('This %s is no longer available. Choose another near the customer and let them know.', 'parcel2go-shipping'), place )} )} {error && ( {error} )} {places !== null && places.length === 0 && ( {__('There are no pickup points near the customer for this service. Choose another service.', 'parcel2go-shipping')} )} {showPicker && ( { const picked = places.find((p) => String(p.id) === value); if (picked) { onPick(toOrderPoint(picked, provider)); setChanging(false); } }} /> )}
)}
); }