import { Button } from '@components/common/ui/Button.js'; import { ConfirmDialog } from '@components/common/ui/ConfirmDialog.js'; import { Label } from '@components/common/ui/Label.js'; import { toast } from '@components/common/ui/Sonner.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import axios from 'axios'; import { Check, Clock, Edit2, ExternalLink, X } from 'lucide-react'; import React from 'react'; import { hasTrackingCapability } from './carrierCaps.js'; import { PhaseBadge } from './phaseBadge.js'; export interface ShipmentItem { uuid: string; orderItemId: number; qty: number; productSku: string | null; productName: string | null; thumbnail: string | null; } export interface Shipment { uuid: string; shipmentId: number; carrier: string | null; carrierName: string | null; trackingNumber: string | null; trackingUrl: string | null; labelUrl: string | null; status: { code: string; name: string; badge: string; phase: 'pending' | 'shipped' | 'delivered' | 'canceled' | string; }; phase: 'pending' | 'shipped' | 'delivered' | 'canceled' | string; shippedAt: { text: string | null } | null; deliveredAt: { text: string | null } | null; canceledAt: { text: string | null } | null; createdAt: { text: string | null }; items: ShipmentItem[]; markDeliveredApi: string; cancelShipmentApi: string; voidShipmentLabelApi: string; } interface Carrier { code: string; name: string; capabilities?: { createLabel?: boolean; generateTrackingUrl?: boolean; voidLabel?: boolean; fetchStatus?: boolean; }; } /** * Pulls the formatted `text` off a Shipment DateTime field. The GraphQL * `DateTime` type is an object (`{ value, timezone, text(format) }`) — the * query asks for `text(format: "LLL dd, yyyy")` so this just unwraps it. * Returns empty string when the field is null (e.g. shippedAt on a shipment * that went direct pending→delivered). */ function dateText(d: { text: string | null } | null): string { return d?.text ?? ''; } /** * Per-shipment block inside the Shipments card. * * Layout per the design: * - Top row: "Shipment #N" + phase status badge | carrier · tracking * - Middle: items list (thumbnail + name + × qty) * - Timeline: Created / Shipped / Delivered with clock icons * - Action row: Mark delivered (when phase=shipped) | Edit tracking | * Cancel shipment (until delivered/canceled) | Track → (when URL) * * Editing tracking swaps the action row for an inline `EditTrackingForm` * (carrier select + tracking number, Cancel + Save). Canceled shipments * dim out and drop all actions. */ export function ShipmentRow({ shipment, carriers }: { shipment: Shipment; carriers: Carrier[]; }) { const [editing, setEditing] = React.useState(false); const isCanceled = shipment.phase === 'canceled'; const carrierEntry = shipment.carrier ? carriers.find((c) => c.code === shipment.carrier) : undefined; const carrierDisplay = shipment.carrierName ?? carrierEntry?.name ?? shipment.carrier; const trackHref = shipment.trackingUrl; const canVoidLabel = !!shipment.labelUrl && // Shipments are born in the `shipped` phase (no `pending` state under the // current model), and voidShipmentLabel's service-side gate also requires // `shipped`. The old `pending` check meant the button never rendered. shipment.phase === 'shipped' && !!carrierEntry?.capabilities?.voidLabel; const timeline = [ { label: _('Created'), date: dateText(shipment.createdAt) }, shipment.shippedAt?.text && { label: _('Shipped'), date: dateText(shipment.shippedAt) }, shipment.deliveredAt?.text && { label: _('Delivered'), date: dateText(shipment.deliveredAt) }, shipment.canceledAt?.text && { label: _('Canceled'), date: dateText(shipment.canceledAt) } ].filter(Boolean) as Array<{ label: string; date: string }>; // Confirmation now lives in the wrapping ConfirmDialog component // (shared, shadcn AlertDialog). These handlers just do the API call + toast + // reload; the dialog closes itself after onConfirm resolves. const handleMarkDelivered = async () => { try { await axios.post(shipment.markDeliveredApi); toast.success(_('Shipment marked delivered')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to mark delivered') ); } }; const handleCancel = async () => { try { await axios.post(shipment.cancelShipmentApi); toast.success(_('Shipment canceled')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to cancel shipment') ); } }; const handleVoidLabel = async () => { try { await axios.delete(shipment.voidShipmentLabelApi); toast.success(_('Label voided')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to void label') ); } }; return (
{_('Shipment')} #{shipment.shipmentId}
{shipment.carrier && (
{carrierDisplay} {shipment.trackingNumber && ( <> · {shipment.trackingNumber} )}
)}
{shipment.items.map((item) => (
{item.thumbnail ? ( {item.productName ) : (
)} {item.productName ?? item.productSku ?? _('Item')} × {item.qty}
))}
{timeline.map((t) => ( {t.label} {t.date} ))}
{editing ? ( setEditing(false)} /> ) : ( !isCanceled && (
{shipment.phase !== 'delivered' && shipment.phase !== 'canceled' && ( {_('Mark delivered')} } title={_('Mark shipment as delivered?')} description={_( 'The customer will receive a delivery confirmation email.' )} confirmLabel={_('Mark delivered')} onConfirm={handleMarkDelivered} /> )} {shipment.phase !== 'delivered' && ( )} {shipment.phase !== 'delivered' && ( {_('Cancel shipment')} } title={_('Cancel this shipment?')} description={_( "Items in this shipment will return to the order's unshipped pool. This action cannot be undone." )} confirmLabel={_('Cancel shipment')} confirmVariant="destructive" onConfirm={handleCancel} /> )} {shipment.labelUrl && ( )} {canVoidLabel && ( {_('Void Label')} } title={_('Void this shipping label?')} description={_( 'The carrier will mark the tracking number as invalid. You will need to purchase a new label if you still intend to ship.' )} confirmLabel={_('Void label')} confirmVariant="destructive" onConfirm={handleVoidLabel} /> )} {trackHref && ( {_('Track')} )}
) )}
); } function EditTrackingForm({ shipment, carriers, onCancel }: { shipment: Shipment; carriers: Carrier[]; onCancel: () => void; }) { const [carrier, setCarrier] = React.useState(shipment.carrier ?? ''); const [trackingNumber, setTrackingNumber] = React.useState( shipment.trackingNumber ?? '' ); const [saving, setSaving] = React.useState(false); // Selected-carrier capability check. Tracking input shows only when the // selected carrier has at least one tracking-related method. Switching // FROM a no-capability carrier (Custom/Other) TO a real carrier reveals // the input live so the admin can fill it in. const selectedCarrier = carriers.find((c) => c.code === carrier); const showTrackingInput = hasTrackingCapability(selectedCarrier); const handleSave = async () => { if (!carrier) { toast.error(_('Carrier is required')); return; } setSaving(true); try { // PATCH /api/shipments/:uuid is the public REST contract — no GraphQL // field needed to surface the URL; the path is stable. await axios.patch(`/api/shipments/${shipment.uuid}`, { carrier, ...(showTrackingInput && trackingNumber.trim() ? { tracking_number: trackingNumber.trim() } : {}) }); toast.success(_('Tracking info updated')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to update tracking info') ); } finally { setSaving(false); } }; return (
{showTrackingInput && (
setTrackingNumber(e.target.value)} />
)}
); }