Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Restore editing and removing items from every fulfillment group

Damian Legawiec6ca94cd88556
diff --git a/packages/dashboard/e2e/order-fulfillments.spec.ts b/packages/dashboard/e2e/order-fulfillments.spec.ts
index a30366ad5c..064be7180c 100644
--- a/packages/dashboard/e2e/order-fulfillments.spec.ts
+++ b/packages/dashboard/e2e/order-fulfillments.spec.ts
@@ -61,4 +61,23 @@ test.describe('order fulfillments', () => {
await expect(card.getByRole('button', { name: /^add fulfillment$/i })).toHaveCount(0)
await expect(card.getByRole('button', { name: /^add item$/i })).toBeVisible()
})
+
+ // The item menus are the only route to editing a quantity or removing a line
+ // item now that the Items card is gone, and a type-clean refactor once left
+ // them off every row inside a fulfillment. Drive one to keep that honest.
+ test('offers edit and remove on an item row', async ({ page }) => {
+ const creds = await login(page)
+ await createDraftOrder(page, creds.store_id)
+
+ const card = fulfillmentsCard(page)
+ await expect(card.getByText(/unfulfilled \(\d+\)/i)).toBeVisible({ timeout: 15_000 })
+
+ await card
+ .getByRole('button', { name: /actions for/i })
+ .first()
+ .click()
+
+ await expect(page.getByRole('menuitem', { name: /edit quantity/i })).toBeVisible()
+ await expect(page.getByRole('menuitem', { name: /^remove$/i })).toBeVisible()
+ })
})
diff --git a/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx b/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx
index 8236a34edf..50bcd70b73 100644
--- a/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx
+++ b/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx
@@ -9,13 +9,12 @@ import {
} from '@spree/dashboard-ui'
import { EllipsisVerticalIcon, PackageIcon, PencilIcon, TrashIcon } from 'lucide-react'
import { useTranslation } from 'react-i18next'
-import type { FulfillmentItemRow } from '../../../lib/fulfillment-items'
+import { type FulfillmentItemRow, isRemovableRow } from '../../../lib/fulfillment-items'
/**
* One item inside a fulfillment group: what it looks like, what it costs and
- * how many units this group holds. The row-level menu is optional — only the
- * unfulfilled group can still edit or remove a line item, since units already
- * handed to a fulfillment are edited through the fulfillment itself.
+ * how many units this group holds. Editing quantity and removing act on the
+ * underlying line item, so they are offered wherever that line item shows up.
*/
function ItemRow({
row,
@@ -23,12 +22,15 @@ function ItemRow({
onRemove,
}: {
row: FulfillmentItemRow
- onEdit?: (row: FulfillmentItemRow) => void
- onRemove?: (row: FulfillmentItemRow) => void
+ // Required, like on the exported list: making these optional once let a
+ // caller render rows with no menu at all, which silently removed the only
+ // route to editing or removing a line item.
+ onEdit: (row: FulfillmentItemRow) => void
+ onRemove: (row: FulfillmentItemRow) => void
}) {
const { t } = useTranslation()
const confirm = useConfirm()
- const hasMenu = !!onEdit || !!onRemove
+ const hasMenu = !!row.lineItem
return (
<div className="flex items-center gap-3 py-3">
@@ -73,14 +75,14 @@ function ItemRow({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
- {onEdit && (
+ {row.lineItem && (
<DropdownMenuItem onClick={() => onEdit(row)}>
<PencilIcon className="size-4" />
{t('admin.orders.detail.dropdown.edit_quantity')}
</DropdownMenuItem>
)}
- {onEdit && onRemove && <DropdownMenuSeparator />}
- {onRemove && (
+ {isRemovableRow(row) && <DropdownMenuSeparator />}
+ {isRemovableRow(row) && (
<DropdownMenuItem
className="text-destructive focus:text-destructive"
onClick={async () => {
@@ -109,31 +111,30 @@ function ItemRow({
/**
* The item list rendered inside a fulfillment group.
*
- * @param canRemove decides per row whether removal is offered at all; a row it
- * rejects shows no remove action rather than one that would do nothing.
+ * Every row that still maps to a line item can have its quantity edited and be
+ * removed — those act on the order's line item, which exists whichever group
+ * the row happens to be displayed in. Removal is withheld only when the row
+ * shows part of a line item whose other units sit elsewhere, since deleting
+ * would take those too.
+ *
+ * @param onEdit opens the quantity dialog for the row's line item
+ * @param onRemove deletes the row's line item from the order
*/
export function FulfillmentItemList({
rows,
onEdit,
onRemove,
- canRemove,
}: {
rows: FulfillmentItemRow[]
- onEdit?: (row: FulfillmentItemRow) => void
- onRemove?: (row: FulfillmentItemRow) => void
- canRemove?: (row: FulfillmentItemRow) => boolean
+ onEdit: (row: FulfillmentItemRow) => void
+ onRemove: (row: FulfillmentItemRow) => void
}) {
if (rows.length === 0) return null
return (
<div className="divide-y">
{rows.map((row) => (
- <ItemRow
- key={row.key}
- row={row}
- onEdit={onEdit}
- onRemove={onRemove && (!canRemove || canRemove(row)) ? onRemove : undefined}
- />
+ <ItemRow key={row.key} row={row} onEdit={onEdit} onRemove={onRemove} />
))}
</div>
)
diff --git a/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx b/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx
index aac1811b3f..0977c3d341 100644
--- a/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx
+++ b/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx
@@ -446,7 +446,25 @@ const CAN_SHIP = ['ready', 'ready_for_pickup']
const CAN_CANCEL = ['pending', 'ready', 'ready_for_pickup']
const CAN_EDIT = ['pending', 'ready', 'ready_for_pickup']
-function FulfillmentRow({ order, fulfillment }: { order: Order; fulfillment: Fulfillment }) {
+/**
+ * Editing a line item is an order-level operation, so one set of handlers is
+ * shared by every group that displays that line item rather than each group
+ * owning its own dialog and mutation.
+ */
+type LineItemActions = {
+ edit: (row: FulfillmentItemRow) => void
+ remove: (row: FulfillmentItemRow) => void
+}
+
+function FulfillmentRow({
+ order,
+ fulfillment,
+ itemActions,
+}: {
+ order: Order
+ fulfillment: Fulfillment
+ itemActions: LineItemActions
+}) {
const { t } = useTranslation()
const confirm = useConfirm()
const orderId = order.id
@@ -592,7 +610,11 @@ function FulfillmentRow({ order, fulfillment }: { order: Order; fulfillment: Ful
</span>
)}
- <FulfillmentItemList rows={fulfillmentItemRows(fulfillment, order.items ?? [])} />
+ <FulfillmentItemList
+ rows={fulfillmentItemRows(fulfillment, order.items ?? [])}
+ onEdit={itemActions.edit}
+ onRemove={itemActions.remove}
+ />
{editOpen && (
<FulfillmentEditDialog
@@ -627,24 +649,17 @@ function FulfillmentRow({ order, fulfillment }: { order: Order; fulfillment: Ful
/**
* Units nobody has put into a fulfillment yet. Reads like a fulfillment group
* so the eye can compare it against the real ones, minus the status and number
- * it does not have. Line items are still editable here — once units belong to
- * a fulfillment they are edited through that fulfillment instead.
+ * it does not have.
*/
function UnfulfilledGroup({
- orderId,
rows,
- onEditItem,
+ itemActions,
}: {
- orderId: string
rows: FulfillmentItemRow[]
- onEditItem: (row: FulfillmentItemRow) => void
+ itemActions: LineItemActions
}) {
const { t } = useTranslation()
- const deleteMutation = useOrderMutation(orderId, (lineItemId: string) =>
- adminClient.orders.items.delete(orderId, lineItemId),
- )
-
const totalUnits = rows.reduce((sum, row) => sum + row.quantity, 0)
return (
@@ -656,15 +671,7 @@ function UnfulfilledGroup({
</span>
</div>
- <FulfillmentItemList
- rows={rows}
- onEdit={onEditItem}
- onRemove={(row) => row.lineItem && deleteMutation.mutate(row.lineItem.id)}
- // Removal deletes the whole line item, so it is only offered while
- // every one of its units is still unclaimed — otherwise it would
- // silently pull units out of an existing fulfillment too.
- canRemove={(row) => !!row.lineItem && row.quantity === row.lineItem.quantity}
- />
+ <FulfillmentItemList rows={rows} onEdit={itemActions.edit} onRemove={itemActions.remove} />
</div>
)
}
@@ -689,6 +696,18 @@ export function FulfillmentsCard({ order }: { order: Order }) {
const lineItems = order.items ?? []
const unfulfilled = unfulfilledItemRows(lineItems, fulfillments)
+ const deleteMutation = useOrderMutation(order.id, (lineItemId: string) =>
+ adminClient.orders.items.delete(order.id, lineItemId),
+ )
+
+ // Shared by every group: the row shows units, but both actions act on the
+ // line item behind them, which is the same record wherever it is displayed.
+ const itemActions: LineItemActions = {
+ edit: (row) =>
+ row.lineItem && setEditItem({ id: row.lineItem.id, quantity: row.lineItem.quantity }),
+ remove: (row) => row.lineItem && deleteMutation.mutate(row.lineItem.id),
+ }
+
// Manual creation is a completed-order operation: Fulfillments::Create
// rejects it otherwise ("Fulfillments can only be created manually on
// completed orders"). A draft's fulfillments come from the delivery step.
@@ -730,17 +749,15 @@ export function FulfillmentsCard({ order }: { order: Order }) {
) : (
<CardContent className="flex flex-col gap-4">
{unfulfilled.length > 0 && (
- <UnfulfilledGroup
- orderId={order.id}
- rows={unfulfilled}
- onEditItem={(row) =>
- row.lineItem &&
- setEditItem({ id: row.lineItem.id, quantity: row.lineItem.quantity })
- }
- />
+ <UnfulfilledGroup rows={unfulfilled} itemActions={itemActions} />
)}
{fulfillments.map((fulfillment) => (
- <FulfillmentRow key={fulfillment.id} order={order} fulfillment={fulfillment} />
+ <FulfillmentRow
+ key={fulfillment.id}
+ order={order}
+ fulfillment={fulfillment}
+ itemActions={itemActions}
+ />
))}
</CardContent>
)}
diff --git a/packages/dashboard/src/lib/fulfillment-items.test.ts b/packages/dashboard/src/lib/fulfillment-items.test.ts
index 4879231d1f..494d662c9d 100644
--- a/packages/dashboard/src/lib/fulfillment-items.test.ts
+++ b/packages/dashboard/src/lib/fulfillment-items.test.ts
@@ -4,6 +4,7 @@ import {
type GroupableFulfillment,
type GroupableLineItem,
hasFulfillableUnits,
+ isRemovableRow,
unfulfilledItemRows,
} from './fulfillment-items'
@@ -149,6 +150,37 @@ describe('unfulfilledItemRows', () => {
})
})
+describe('isRemovableRow', () => {
+ it('allows removing a row holding every unit of its line item', () => {
+ const [row] = fulfillmentItemRows(
+ fulfillment({ fulfillment_items: [{ line_item_id: 'li_1', quantity: 2 }] }),
+ [lineItem({ quantity: 2 })],
+ )
+
+ expect(isRemovableRow(row)).toBe(true)
+ })
+
+ // The other units live in a different group; deleting the line item would
+ // silently take those too.
+ it('refuses a row holding only part of its line item', () => {
+ const [row] = fulfillmentItemRows(
+ fulfillment({ fulfillment_items: [{ line_item_id: 'li_1', quantity: 1 }] }),
+ [lineItem({ quantity: 3 })],
+ )
+
+ expect(isRemovableRow(row)).toBe(false)
+ })
+
+ it('refuses a row with no line item behind it', () => {
+ const [row] = fulfillmentItemRows(
+ fulfillment({ fulfillment_items: [{ variant_id: 'variant_9', quantity: 1 }] }),
+ [],
+ )
+
+ expect(isRemovableRow(row)).toBe(false)
+ })
+})
+
describe('hasFulfillableUnits', () => {
it('is true while units remain unclaimed', () => {
expect(hasFulfillableUnits([lineItem({ quantity: 2 })], [])).toBe(true)
diff --git a/packages/dashboard/src/lib/fulfillment-items.ts b/packages/dashboard/src/lib/fulfillment-items.ts
index 59edd42ce3..f5e26b888d 100644
--- a/packages/dashboard/src/lib/fulfillment-items.ts
+++ b/packages/dashboard/src/lib/fulfillment-items.ts
@@ -150,6 +150,16 @@ export function unfulfilledItemRows(
})
}
+/**
+ * Whether removing this row would delete only the units it displays. Removal
+ * acts on the whole line item, so a row showing part of one — the rest sitting
+ * in another group — would take those other units with it. Offering removal
+ * only on whole line items keeps the action honest about its blast radius.
+ */
+export function isRemovableRow(row: FulfillmentItemRow): boolean {
+ return !!row.lineItem && row.quantity === row.lineItem.quantity
+}
+
/**
* Whether anything is left to put into a new fulfillment: either units nobody
* has claimed, or units sitting in a fulfillment that has not shipped yet and