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.

Strike a withdrawn audience through instead of dropping the row

Damian Legawiec58634b81b296
diff --git a/packages/dashboard/src/components/spree/catalog-terms-card.tsx b/packages/dashboard/src/components/spree/catalog-terms-card.tsx
index f4f6c11ee0..1aab1b2f75 100644
--- a/packages/dashboard/src/components/spree/catalog-terms-card.tsx
+++ b/packages/dashboard/src/components/spree/catalog-terms-card.tsx
@@ -156,8 +156,11 @@ function OrderMinimums({
return (
<section className="flex flex-col gap-3">
- <div className="flex items-center justify-between">
- <div>
+ {/* The description wraps within its own column rather than running
+ under the button: `min-w-0` lets the text block shrink, and the
+ button refuses to. */}
+ <div className="flex items-start justify-between gap-3">
+ <div className="min-w-0">
<h3 className="flex items-center gap-1.5 font-medium text-sm">
{t('admin.catalogs.terms.minimums_title')}
<TermHelp text={t('admin.catalogs.terms.help.order_minimum')} />
@@ -171,6 +174,7 @@ function OrderMinimums({
type="button"
variant="outline"
size="sm"
+ className="shrink-0"
disabled={!nextCurrency}
onClick={() => update([...rows, { currency: nextCurrency, amount: '' }])}
>
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/products/catalogs/$catalogId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/products/catalogs/$catalogId.tsx
index eb3a83577a..9f10e8ad87 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/products/catalogs/$catalogId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/products/catalogs/$catalogId.tsx
@@ -17,6 +17,7 @@ import {
CardHeader,
CardTitle,
Checkbox,
+ cn,
Dialog,
DialogBody,
DialogContent,
@@ -40,7 +41,7 @@ import {
Textarea,
} from '@spree/dashboard-ui'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
-import { PlusIcon, TrashIcon } from 'lucide-react'
+import { PlusIcon, TrashIcon, Undo2Icon } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { Controller, type UseFormReturn, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
@@ -409,8 +410,22 @@ function CatalogAssignmentsCard({
// Removal needs no confirm: nothing is written until Save, and Discard
// puts it back — the same reason the assortment rows drop theirs.
+ //
+ // A row that exists server-side is marked rather than dropped, so it stays
+ // visible struck through with an undo, like a product staged for removal.
+ // One the merchant only just added has nothing to undo back to, so it goes.
function remove(index: number) {
- update(assignments.filter((_, i) => i !== index))
+ const entry = assignments[index]
+ if (!entry.id) {
+ update(assignments.filter((_, i) => i !== index))
+ return
+ }
+
+ update(assignments.map((row, i) => (i === index ? { ...row, removed: true } : row)))
+ }
+
+ function restore(index: number) {
+ update(assignments.map((row, i) => (i === index ? { ...row, removed: false } : row)))
}
return (
@@ -434,9 +449,17 @@ function CatalogAssignmentsCard({
{assignments.map((assignment, index) => (
<div
key={`${assignment.assignable_type}-${assignment.assignable_id}`}
- className="flex items-center justify-between gap-2"
+ className={cn(
+ 'flex items-center justify-between gap-2',
+ assignment.removed && 'opacity-60',
+ )}
>
- <span className="flex min-w-0 items-center gap-2">
+ <span
+ className={cn(
+ 'flex min-w-0 items-center gap-2',
+ assignment.removed && 'line-through',
+ )}
+ >
<Badge variant="outline">
{t(`admin.catalogs.assignable_types.${assignment.assignable_type}`)}
</Badge>
@@ -444,17 +467,28 @@ function CatalogAssignmentsCard({
{assignment.assignable_name ?? assignment.assignable_id}
</span>
</span>
- {canEdit && (
- <Button
- variant="ghost"
- size="icon-xs"
- type="button"
- onClick={() => remove(index)}
- aria-label={t('admin.actions.remove')}
- >
- <TrashIcon className="size-4" />
- </Button>
- )}
+ {canEdit &&
+ (assignment.removed ? (
+ <Button
+ variant="ghost"
+ size="icon-xs"
+ type="button"
+ onClick={() => restore(index)}
+ aria-label={t('admin.actions.restore')}
+ >
+ <Undo2Icon className="size-4" />
+ </Button>
+ ) : (
+ <Button
+ variant="ghost"
+ size="icon-xs"
+ type="button"
+ onClick={() => remove(index)}
+ aria-label={t('admin.actions.remove')}
+ >
+ <TrashIcon className="size-4" />
+ </Button>
+ ))}
</div>
))}
</div>
@@ -466,7 +500,23 @@ function CatalogAssignmentsCard({
open
onOpenChange={setAddOpen}
assigned={assignments}
- onAdd={(entry) => update([...assignments, entry])}
+ onAdd={(entry) => {
+ // Re-picking an audience staged for withdrawal restores it: it
+ // is still assigned server-side, so adding a second row would
+ // render it twice and then re-create what the save just left in
+ // place.
+ const staged = assignments.findIndex(
+ (row) =>
+ row.assignable_type === entry.assignable_type &&
+ row.assignable_id === entry.assignable_id,
+ )
+ if (staged >= 0) {
+ restore(staged)
+ return
+ }
+
+ update([...assignments, entry])
+ }}
/>
)}
</Card>
@@ -519,8 +569,13 @@ function AssignCatalogDialog({
const { search, hydrate } = assignableSearch(assignableType)
+ // A row staged for withdrawal is not a duplicate — picking it again is how
+ // the merchant takes the removal back.
const duplicate = assigned.some(
- (entry) => entry.assignable_type === assignableType && entry.assignable_id === assignableId,
+ (entry) =>
+ !entry.removed &&
+ entry.assignable_type === assignableType &&
+ entry.assignable_id === assignableId,
)
function handleSubmit() {
diff --git a/packages/dashboard/src/schemas/catalog.ts b/packages/dashboard/src/schemas/catalog.ts
index e383a8bda4..c00f4362c5 100644
--- a/packages/dashboard/src/schemas/catalog.ts
+++ b/packages/dashboard/src/schemas/catalog.ts
@@ -167,6 +167,12 @@ export interface AssignmentEntry {
assignable_type: 'company' | 'customer_group'
assignable_id: string
assignable_name?: string | null
+ /**
+ * Staged for withdrawal. The row stays on screen struck through with an
+ * undo, the way a product staged for removal does — Save is what actually
+ * withdraws it, so it is dropped from the payload rather than the list.
+ */
+ removed?: boolean
}
/** Values the catalog form holds. */
@@ -212,10 +218,14 @@ export function catalogValuesToParams(
// in one transaction rather than a sequence a failure can leave half
// applied. Per-product terms stay their own request — an agreement may
// name thousands of them.
- assignments: values.assignments.map((entry) => ({
- assignable_type: entry.assignable_type,
- assignable_id: entry.assignable_id,
- })),
+ // Rows staged for withdrawal are simply absent: the endpoint replaces
+ // the whole audience, so leaving one out is what removes it.
+ assignments: values.assignments
+ .filter((entry) => !entry.removed)
+ .map((entry) => ({
+ assignable_type: entry.assignable_type,
+ assignable_id: entry.assignable_id,
+ })),
order_minimums: values.order_minimums.map((row) => ({
currency: row.currency,
amount: row.amount,