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.

Create a supplier without leaving the purchase order

Damian Legawiec47aa5eb8e467
diff --git a/packages/dashboard-core/src/components/resource-combobox.tsx b/packages/dashboard-core/src/components/resource-combobox.tsx
index 4ac7aec1c1..5a985413a1 100644
--- a/packages/dashboard-core/src/components/resource-combobox.tsx
+++ b/packages/dashboard-core/src/components/resource-combobox.tsx
@@ -3,6 +3,7 @@ import {
ResourceCombobox as HeadlessResourceCombobox,
type ResourceComboboxProps as HeadlessResourceComboboxProps,
} from '@spree/dashboard-ui'
+import { PlusIcon } from '@spree/dashboard-ui/icons'
import { useQuery } from '@tanstack/react-query'
import { useDeferredValue, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
@@ -47,6 +48,14 @@ export interface ResourceComboboxProps<T extends ComboboxOption>
* Returning `false` drops the row from the dropdown.
*/
filterOption?: (option: T) => boolean
+
+ /**
+ * A row pinned to the bottom of the dropdown, for creating what the search
+ * could not find — "New supplier" on a purchase order, say. It sits with the
+ * results rather than beside the field so it is found where the merchant is
+ * already looking, and it closes the dropdown when chosen.
+ */
+ action?: { label: string; onSelect: () => void }
}
/**
@@ -71,6 +80,7 @@ export function ResourceCombobox<T extends ComboboxOption>({
emptyText,
disabled,
filterOption,
+ action,
id,
}: ResourceComboboxProps<T>) {
const { t } = useTranslation()
@@ -132,10 +142,29 @@ export function ResourceCombobox<T extends ComboboxOption>({
placeholder={placeholderLabel}
emptyText={emptyLabel}
listFooter={
- hiddenCount > 0 ? (
- <p className="border-t border-border px-2.5 py-2 text-muted-foreground text-xs">
- {t('admin.common.combobox_more_results', { count: hiddenCount })}
- </p>
+ hiddenCount > 0 || action ? (
+ <>
+ {hiddenCount > 0 && (
+ <p className="border-t border-border px-2.5 py-2 text-muted-foreground text-xs">
+ {t('admin.common.combobox_more_results', { count: hiddenCount })}
+ </p>
+ )}
+ {action && (
+ // `onMouseDown` rather than `onClick`: the input blurs first and
+ // the dropdown unmounts, so a click never lands.
+ <button
+ type="button"
+ className="flex w-full cursor-pointer items-center gap-2 border-t border-border px-2.5 py-2 text-left text-sm hover:bg-accent"
+ onMouseDown={(event) => {
+ event.preventDefault()
+ action.onSelect()
+ }}
+ >
+ <PlusIcon className="size-4" />
+ {action.label}
+ </button>
+ )}
+ </>
) : undefined
}
disabled={disabled}
diff --git a/packages/dashboard/src/components/spree/purchase-order-form.tsx b/packages/dashboard/src/components/spree/purchase-order-form.tsx
index fe52948dc0..558fe28be0 100644
--- a/packages/dashboard/src/components/spree/purchase-order-form.tsx
+++ b/packages/dashboard/src/components/spree/purchase-order-form.tsx
@@ -25,6 +25,7 @@ import {
SelectValue,
Textarea,
} from '@spree/dashboard-ui'
+import { PlusIcon } from '@spree/dashboard-ui/icons'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { supplierAutocompleteProps } from '../../hooks/use-suppliers'
@@ -99,6 +100,7 @@ export function PurchaseOrderForm({
const [notes, setNotes] = useState(initial.notes)
const [lines, setLines] = useState<VariantLine[]>(initial.lines)
const [creatingSupplier, setCreatingSupplier] = useState(false)
+ const [pickerOpen, setPickerOpen] = useState(false)
// A cleared cost input is not zero — it is nothing, which the server rejects
// field-by-field. `Number('')` is 0 and finite, so the emptiness has to be
@@ -131,17 +133,14 @@ export function PurchaseOrderForm({
value={supplierId}
onChange={(id) => setSupplierId(id ?? '')}
placeholder={t('admin.purchase_orders.fields.supplier_placeholder')}
+ // Buying from someone new is part of raising the order, not a
+ // detour through another screen — so it sits in the dropdown the
+ // merchant already opened looking for them.
+ action={{
+ label: t('admin.suppliers.new_title'),
+ onSelect: () => setCreatingSupplier(true),
+ }}
/>
- {/* Buying from someone new is part of raising the order, not a
- detour through settings. */}
- <Button
- type="button"
- variant="link"
- className="h-auto self-start p-0"
- onClick={() => setCreatingSupplier(true)}
- >
- {t('admin.suppliers.new_title')}
- </Button>
</Field>
<Field>
@@ -218,8 +217,14 @@ export function PurchaseOrderForm({
const itemsCard = (
<Card>
- <CardHeader>
+ <CardHeader className="flex flex-row items-center justify-between">
<CardTitle>{t('admin.purchase_orders.items_title')}</CardTitle>
+ {lines.length > 0 && (
+ <Button type="button" variant="outline" size="sm" onClick={() => setPickerOpen(true)}>
+ <PlusIcon className="size-4" />
+ {t('admin.inventory_lines.add_label')}
+ </Button>
+ )}
</CardHeader>
<CardContent>
{/* No warehouse filter, unlike a transfer: buying stock in is how a
@@ -230,6 +235,8 @@ export function PurchaseOrderForm({
currency={currency}
quantityLabel={t('admin.purchase_orders.columns.quantity_ordered')}
withCost
+ pickerOpen={pickerOpen}
+ onPickerOpenChange={setPickerOpen}
/>
</CardContent>
</Card>
diff --git a/packages/dashboard/src/components/spree/stock-transfer-form.tsx b/packages/dashboard/src/components/spree/stock-transfer-form.tsx
index cc3b02ffc5..cdc56da958 100644
--- a/packages/dashboard/src/components/spree/stock-transfer-form.tsx
+++ b/packages/dashboard/src/components/spree/stock-transfer-form.tsx
@@ -19,6 +19,7 @@ import {
SelectValue,
Textarea,
} from '@spree/dashboard-ui'
+import { PlusIcon } from '@spree/dashboard-ui/icons'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { type VariantLine, VariantLineEditor } from './variant-line-editor'
@@ -78,6 +79,7 @@ export function StockTransferForm({
const [reference, setReference] = useState(initial.reference)
const [notes, setNotes] = useState(initial.notes)
const [lines, setLines] = useState<VariantLine[]>(initial.lines)
+ const [pickerOpen, setPickerOpen] = useState(false)
// Stock cannot move to where it already is.
const destinationItems = locationItems.filter((item) => item.value !== sourceId)
@@ -173,8 +175,20 @@ export function StockTransferForm({
const itemsCard = (
<Card>
- <CardHeader>
+ <CardHeader className="flex flex-row items-center justify-between">
<CardTitle>{t('admin.stock_transfers.items_title')}</CardTitle>
+ {lines.length > 0 && (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ disabled={!sourceId}
+ onClick={() => setPickerOpen(true)}
+ >
+ <PlusIcon className="size-4" />
+ {t('admin.inventory_lines.add_label')}
+ </Button>
+ )}
</CardHeader>
<CardContent>
{/* A draft may hold no lines at all and gain them as the merchant
@@ -186,6 +200,8 @@ export function StockTransferForm({
quantityLabel={t('admin.stock_transfers.columns.quantity_shipped')}
stockLocationId={sourceId || null}
requireStockLocation
+ pickerOpen={pickerOpen}
+ onPickerOpenChange={setPickerOpen}
/>
</CardContent>
</Card>
diff --git a/packages/dashboard/src/components/spree/variant-line-editor.tsx b/packages/dashboard/src/components/spree/variant-line-editor.tsx
index 46401c68fc..a03363a596 100644
--- a/packages/dashboard/src/components/spree/variant-line-editor.tsx
+++ b/packages/dashboard/src/components/spree/variant-line-editor.tsx
@@ -2,6 +2,12 @@ import type { Variant } from '@spree/admin-sdk'
import { adminClient, formatPrice, ResourcePickerSheet } from '@spree/dashboard-core'
import {
Button,
+ Empty,
+ EmptyContent,
+ EmptyDescription,
+ EmptyHeader,
+ EmptyMedia,
+ EmptyTitle,
Input,
InputGroup,
InputGroupAddon,
@@ -14,8 +20,7 @@ import {
TableHeader,
TableRow,
} from '@spree/dashboard-ui'
-import { PlusIcon, TrashIcon } from '@spree/dashboard-ui/icons'
-import { useState } from 'react'
+import { PackageIcon, PlusIcon, TrashIcon } from '@spree/dashboard-ui/icons'
import { useTranslation } from 'react-i18next'
import { VariantLink } from './variant-link'
@@ -55,6 +60,8 @@ export function VariantLineEditor({
withCost = false,
stockLocationId,
requireStockLocation = false,
+ pickerOpen,
+ onPickerOpenChange,
}: {
lines: VariantLine[]
onChange: (lines: VariantLine[]) => void
@@ -70,13 +77,19 @@ export function VariantLineEditor({
stockLocationId?: string | null
/** Refuse to open the picker until a warehouse is chosen. */
requireStockLocation?: boolean
+ /**
+ * The picker's open state, owned by the card so its header can carry the
+ * "Add a product" button beside the title — where every other card on these
+ * screens puts its actions.
+ */
+ pickerOpen: boolean
+ onPickerOpenChange: (open: boolean) => void
}) {
const { t } = useTranslation()
// Nothing sensible to search until the source is known, and offering the
// whole catalogue invites a line the warehouse cannot send — which is only
// refused later, per line, when the transfer is marked in transit.
const locked = !!requireStockLocation && !stockLocationId
- const [pickerOpen, setPickerOpen] = useState(false)
function addVariant(variant: Variant) {
const existing = lines.find((line) => line.variant.id === variant.id)
@@ -101,29 +114,12 @@ export function VariantLineEditor({
return (
<div className="flex flex-col gap-4">
- <div className="flex items-center justify-between gap-4">
- {/* Only says anything when there is something to say: the button
- already names the action. */}
- <p className="text-muted-foreground text-sm">
- {locked ? t('admin.inventory_lines.pick_source_first') : null}
- </p>
- <Button
- type="button"
- variant="outline"
- disabled={locked}
- onClick={() => setPickerOpen(true)}
- >
- <PlusIcon className="size-4" />
- {t('admin.inventory_lines.add_label')}
- </Button>
- </div>
-
{/* A sheet rather than a one-at-a-time combobox: a delivery is picked in
one pass, and the same picker the catalogue screens use already does
searching, paging and select-all. */}
<ResourcePickerSheet<Variant>
open={pickerOpen}
- onOpenChange={setPickerOpen}
+ onOpenChange={onPickerOpenChange}
// Keyed by warehouse: the same query means different things at two
// sources, and a cached result would offer the wrong shelf's SKUs.
queryKey={`inventory-line-variant-picker:${stockLocationId ?? 'any'}`}
@@ -148,7 +144,33 @@ export function VariantLineEditor({
searchPlaceholder={t('admin.inventory_lines.search_placeholder')}
/>
- {lines.length > 0 && (
+ {lines.length === 0 ? (
+ <Empty className="border-0">
+ <EmptyHeader>
+ <EmptyMedia variant="icon">
+ <PackageIcon />
+ </EmptyMedia>
+ <EmptyTitle>{t('admin.inventory_lines.empty_title')}</EmptyTitle>
+ <EmptyDescription>
+ {locked
+ ? t('admin.inventory_lines.pick_source_first')
+ : t('admin.inventory_lines.empty_description')}
+ </EmptyDescription>
+ </EmptyHeader>
+ <EmptyContent>
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ disabled={locked}
+ onClick={() => onPickerOpenChange(true)}
+ >
+ <PlusIcon className="size-4" />
+ {t('admin.inventory_lines.add_label')}
+ </Button>
+ </EmptyContent>
+ </Empty>
+ ) : (
<div className="overflow-x-auto rounded-md border">
<Table>
<TableHeader>
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index fff0f1d0ba..e279a17d2b 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -5462,7 +5462,9 @@
"sku": "رمز SKU",
"unit_cost": "تكلفة الوحدة"
},
- "picker_title": "إضافة منتجات"
+ "picker_title": "إضافة منتجات",
+ "empty_title": "لا توجد منتجات بعد",
+ "empty_description": "أضف المنتجات التي يشملها هذا المستند."
},
"stock_history": {
"title": "سجل المخزون",
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 3b50fb6cd6..3cb182a82a 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -5433,7 +5433,9 @@
"sku": "SKU",
"unit_cost": "Stückkosten"
},
- "picker_title": "Produkte hinzufügen"
+ "picker_title": "Produkte hinzufügen",
+ "empty_title": "Noch keine Produkte",
+ "empty_description": "Fügen Sie die Produkte hinzu, für die dieser Beleg gilt."
},
"stock_history": {
"title": "Bestandsverlauf",
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index e5ddedd5db..9f61fc42cf 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -5433,7 +5433,9 @@
"sku": "SKU",
"unit_cost": "Unit cost"
},
- "picker_title": "Add products"
+ "picker_title": "Add products",
+ "empty_title": "No products yet",
+ "empty_description": "Add the products this order is for."
},
"stock_history": {
"title": "Stock history",
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index e9542e9115..5062e9b4c7 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -5433,7 +5433,9 @@
"sku": "UGS",
"unit_cost": "Coût unitaire"
},
- "picker_title": "Ajouter des produits"
+ "picker_title": "Ajouter des produits",
+ "empty_title": "Aucun produit",
+ "empty_description": "Ajoutez les produits concernés par ce document."
},
"stock_history": {
"title": "Historique du stock",
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 9c826d76d3..cd03419817 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -5495,7 +5495,9 @@
"sku": "SKU",
"unit_cost": "Koszt jednostkowy"
},
- "picker_title": "Dodaj produkty"
+ "picker_title": "Dodaj produkty",
+ "empty_title": "Brak produktów",
+ "empty_description": "Dodaj produkty, których dotyczy ten dokument."
},
"stock_history": {
"title": "Historia stanu",
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 93757c1933..ce5859b373 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -5422,7 +5422,9 @@
"sku": "SKU",
"unit_cost": "单位成本"
},
- "picker_title": "添加商品"
+ "picker_title": "添加商品",
+ "empty_title": "暂无商品",
+ "empty_description": "添加此单据涉及的商品。"
},
"stock_history": {
"title": "库存记录",