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.

Fit the catalog products table inside its card

Damian Legawiec34ae500b672e
diff --git a/packages/dashboard-ui/src/spree/product-membership-list.tsx b/packages/dashboard-ui/src/spree/product-membership-list.tsx
index 33cc33b46e..321df99428 100644
--- a/packages/dashboard-ui/src/spree/product-membership-list.tsx
+++ b/packages/dashboard-ui/src/spree/product-membership-list.tsx
@@ -37,6 +37,8 @@ import { SearchInput } from './search-input'
export interface SubRowLayout {
/** Cells before the product-name column (selection checkbox, drag handle). */
leadingCells: number
+ /** How many extra columns the caller's own `extraColumns` set contributes. */
+ extraColumnCount: number
/** Cells after the extra columns (the row-action column). */
trailingCells: number
}
@@ -108,6 +110,12 @@ export interface ProductMembershipListProps {
*/
renderSubRows?: (row: ProductMembershipRow, layout: SubRowLayout) => ReactNode
extraColumns?: {
+ /**
+ * How many columns this set contributes. Stated rather than counted from
+ * the rendered headers, so a sub-row can span the columns it does not
+ * fill without the table coming up short.
+ */
+ columnCount?: number
headers: ReactNode
renderCells: (row: ProductMembershipRow) => ReactNode
}
@@ -216,6 +224,7 @@ export function ProductMembershipList({
// than leaving each caller to re-derive it and drift.
const subRowLayout: SubRowLayout = {
leadingCells: (curatable ? 1 : 0) + (reorderable ? 1 : 0),
+ extraColumnCount: extraColumns?.columnCount ?? 0,
trailingCells: 1,
}
@@ -255,7 +264,12 @@ export function ProductMembershipList({
</TableHead>
)}
{reorderable && <TableHead className="w-8" />}
- <TableHead>{labels.columnProduct}</TableHead>
+ {/* `w-full max-w-0`: table layout sizes a column to its
+ content, so a long product or variant name would push the
+ columns beside it out of the card — which clips them
+ rather than scrolling. This makes the product column take
+ whatever is left instead, and its content truncate. */}
+ <TableHead className="w-full max-w-0">{labels.columnProduct}</TableHead>
{extraColumns?.headers}
<TableHead className="w-10" />
</TableRow>
@@ -359,8 +373,8 @@ function MembershipRow({
{reorderable && <DragHandle attributes={attributes} listeners={listeners} />}
</TableCell>
)}
- <TableCell>
- <span className={cn('flex items-center gap-3', removed && 'line-through')}>
+ <TableCell className="max-w-0">
+ <span className={cn('flex min-w-0 items-center gap-3', removed && 'line-through')}>
<Thumbnail src={row.thumbnailUrl} size="sm" />
{renderTitle ? renderTitle(row) : <span className="truncate text-sm">{row.name}</span>}
{added && labels.pendingAddedBadge && (
@@ -416,6 +430,7 @@ export function mergeExtraColumns(
...sets: NonNullable<ProductMembershipListProps['extraColumns']>[]
): NonNullable<ProductMembershipListProps['extraColumns']> {
return {
+ columnCount: sets.reduce((total, set) => total + (set.columnCount ?? 0), 0),
headers: (
<>
{sets.map((set, index) => (
diff --git a/packages/dashboard/src/components/spree/catalog-price-columns.tsx b/packages/dashboard/src/components/spree/catalog-price-columns.tsx
index 0828a86d36..526fe6aef6 100644
--- a/packages/dashboard/src/components/spree/catalog-price-columns.tsx
+++ b/packages/dashboard/src/components/spree/catalog-price-columns.tsx
@@ -26,6 +26,9 @@ const AGREEMENT_SOURCES = ['explicit', 'automatic'] as const
* staged removal keeps showing its price: it is still on the list until Save,
* and the struck-through row already says it is leaving.
*/
+/** Columns `catalogPriceColumns` contributes, and a variant sub-row fills itself. */
+const CATALOG_PRICE_COLUMN_COUNT = 2
+
export function catalogPriceColumns({
headers,
}: {
@@ -35,6 +38,7 @@ export function catalogPriceColumns({
}
}) {
return {
+ columnCount: CATALOG_PRICE_COLUMN_COUNT,
headers: (
<>
<TableHead className="w-32 text-right">{headers.price}</TableHead>
@@ -160,7 +164,13 @@ function TierBadge({ price }: { price: CatalogPrice }) {
export function catalogVariantRows({ products }: { products: CatalogProduct[] }) {
const byId = new Map(products.map((product) => [product.id, product.catalog_variants ?? []]))
- return (row: ProductMembershipRow, { leadingCells, trailingCells }: SubRowLayout) => {
+ return (
+ row: ProductMembershipRow,
+ { leadingCells, extraColumnCount, trailingCells }: SubRowLayout,
+ ) => {
+ // A variant row fills this set's own columns (price, source); every other
+ // set's columns belong to the product and are spanned.
+ const spannedCells = Math.max(extraColumnCount - CATALOG_PRICE_COLUMN_COUNT, 0) + trailingCells
// A staged addition has no server row yet, so it has nothing to list.
const variants = row.pending === 'added' ? [] : (byId.get(row.id) ?? [])
if (variants.length === 0) return null
@@ -170,13 +180,19 @@ export function catalogVariantRows({ products }: { products: CatalogProduct[] })
{/* One spanning cell rather than several empty ones: these are
spacers to line the row up, not columns of their own. */}
{leadingCells > 0 && <TableCell colSpan={leadingCells} />}
- <TableCell className="text-muted-foreground">
- <span className="flex items-center gap-2 pl-11">
- <span className="truncate">
+ {/* The name gives up space last: a flex item's default minimum is its
+ own content, so both halves have to be told they may shrink, and
+ the SKU is told to shrink first — it identifies the variant only
+ once the name already has. */}
+ <TableCell className="max-w-0 text-muted-foreground">
+ <span className="flex min-w-0 items-center gap-2 pl-11">
+ <span className="shrink-[1] truncate">
{price.label ?? i18n.t('admin.catalogs.prices.variant_default')}
</span>
{price.sku && (
- <span className="truncate font-mono text-xs opacity-70">{price.sku}</span>
+ <span className="min-w-0 shrink-[4] truncate font-mono text-xs opacity-70">
+ {price.sku}
+ </span>
)}
</span>
</TableCell>
@@ -189,7 +205,11 @@ export function catalogVariantRows({ products }: { products: CatalogProduct[] })
<TableCell>
<PriceSourceBadge source={price.source} />
</TableCell>
- {trailingCells > 0 && <TableCell colSpan={trailingCells} />}
+ {/* The quantity-term columns and the row-action column, spanned
+ rather than filled: terms are stated per product, so a variant row
+ has nothing to say in them. Counted from the table's own layout so
+ the row can never come up short and stretch the table. */}
+ {spannedCells > 0 && <TableCell colSpan={spannedCells} />}
</tr>
))
}
diff --git a/packages/dashboard/src/components/spree/catalog-terms-card.tsx b/packages/dashboard/src/components/spree/catalog-terms-card.tsx
index 6bd8aaa260..8960a2d7d4 100644
--- a/packages/dashboard/src/components/spree/catalog-terms-card.tsx
+++ b/packages/dashboard/src/components/spree/catalog-terms-card.tsx
@@ -296,6 +296,7 @@ export function catalogTermColumns({
}
return {
+ columnCount: 2,
// The shared table's own cells rather than hand-rolled ones: TableHead
// carries the header's rule and background, TableCell the row border, so
// raw elements read as a separate block that ignores the row hover.
@@ -339,7 +340,10 @@ export function catalogTermColumns({
placeholder={entry?.mixed ? headers.mixed : catalogMinimum || headers.defaultHint}
value={entry?.minimum_order_quantity ?? ''}
onChange={(event) => set(row.id, 'minimum_order_quantity', event.target.value)}
- className="h-8"
+ // No spinner: it reserves room these narrow columns do not have,
+ // clipping the "Default" placeholder that says the product
+ // inherits the catalog's own term.
+ className="h-8 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
/>
{minimumInvalid && <p className="mt-1 text-destructive text-xs">{headers.invalid}</p>}
</TableCell>
@@ -354,7 +358,7 @@ export function catalogTermColumns({
placeholder={entry?.mixed ? headers.mixed : catalogMultiple || headers.defaultHint}
value={entry?.order_multiple ?? ''}
onChange={(event) => set(row.id, 'order_multiple', event.target.value)}
- className="h-8"
+ className="h-8 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
/>
{multipleInvalid && <p className="mt-1 text-destructive text-xs">{headers.invalid}</p>}
</TableCell>
diff --git a/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
index b48c8deaa0..8d1695c84e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/catalogs/products_controller.rb
@@ -46,11 +46,30 @@ module Spree
Array(@collection).flat_map { |product| product.variants.to_a }
end
- # Every row lists its variants and prices each of them, so the
- # variants and their prices are loaded with the page rather than
- # a query per product (docs/plans/6.0-volume-pricing.md).
+ # Every row lists its variants and prices each of them, so what the
+ # rows read comes down with the page rather than a query per
+ # variant: the prices the resolver picks from, the option values
+ # that name the variant, and the stock levels behind the
+ # in-stock and purchasable flags
+ # (docs/plans/6.0-volume-pricing.md).
+ #
+ # `default_variant` is preloaded separately from `variants`
+ # despite being one of them: it is its own `belongs_to`, so it
+ # arrives as a different object with a cache of its own, and the
+ # product row's price and status read it rather than the
+ # collection. Its prices and publications come with it, since
+ # `price_in` and `publication_for` both query per product unless
+ # the association is already loaded.
def collection_includes
- [{ variants: :prices }]
+ [
+ { product_publications: :channel },
+ { default_variant: :prices },
+ { variants: [
+ :prices,
+ { option_values: :option_type },
+ { stock_levels: %i[stock_location active_stock_reservations] }
+ ] }
+ ]
end
def scope
diff --git a/spree/core/app/models/spree/price_list.rb b/spree/core/app/models/spree/price_list.rb
index 28faef3118..a3d112b737 100644
--- a/spree/core/app/models/spree/price_list.rb
+++ b/spree/core/app/models/spree/price_list.rb
@@ -484,12 +484,22 @@ module Spree
end
end
+ # The store's zone, read once. `currently_active?` is asked per variant on
+ # a listing, and the association behind it is a query each time
+ # (docs/plans/6.0-volume-pricing.md).
+ #
+ # @return [String]
+ def store_timezone
+ return @store_timezone if defined?(@store_timezone)
+
+ @store_timezone = store&.preferred_timezone || Rails.application.config.time_zone
+ end
+
# Returns true if the date is within the price list date range
# @param date [Time] the date to check
# @return [Boolean]
def within_date_range?(date)
- timezone = store&.preferred_timezone || Rails.application.config.time_zone
- date_in_tz = date.in_time_zone(timezone)
+ date_in_tz = date.in_time_zone(store_timezone)
return false if starts_at.present? && date_in_tz < starts_at
return false if ends_at.present? && date_in_tz > ends_at
diff --git a/spree/core/app/services/spree/catalogs/resolve_prices.rb b/spree/core/app/services/spree/catalogs/resolve_prices.rb
index 104addef31..75908c6895 100644
--- a/spree/core/app/services/spree/catalogs/resolve_prices.rb
+++ b/spree/core/app/services/spree/catalogs/resolve_prices.rb
@@ -45,9 +45,13 @@ module Spree
ids = Array(variants).map(&:id)
return if ids.empty?
+ # Accumulated rather than replaced: a caller may preload in batches,
+ # and overwriting would send every variant from an earlier batch back
+ # to a query of its own.
+ #
# An empty result still counts as loaded for that variant — a product
# nothing prices must not fall through to a query per row.
- @rows = ids.index_with { [] }.merge(price_rows_for(ids))
+ @rows = (@rows || {}).merge(ids.index_with { [] }).merge(price_rows_for(ids))
end
# The price this agreement gives for a variant.