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.

Clean up the product types diff

Damian Legawiec7b6811157fc9
diff --git a/packages/dashboard/src/hooks/use-option-types.ts b/packages/dashboard/src/hooks/use-option-types.ts
index 891ea73c4c..3a3f1395fa 100644
--- a/packages/dashboard/src/hooks/use-option-types.ts
+++ b/packages/dashboard/src/hooks/use-option-types.ts
@@ -25,9 +25,7 @@ export function optionTypeAutocompleteProps(queryKey: string) {
hydrate: (ids: string[]) => adminClient.optionTypes.list({ id_in: ids, limit: ids.length }),
getOptionLabel: (optionType: OptionType) =>
optionType.label ?? optionType.name ?? optionType.id,
- placeholder: i18n.t('admin.products.options.search_placeholder', {
- defaultValue: 'Search option types…',
- }),
+ placeholder: i18n.t('admin.product_types.option_type_search_placeholder'),
emptyText: i18n.t('admin.product_types.no_option_types'),
}
}
@@ -55,7 +53,7 @@ export function useOptionTypesByIds(ids: string[] | undefined) {
const idList = ids ?? []
return useQuery({
queryKey: useResourceKey('option-types', 'by-ids', idList),
- queryFn: () => adminClient.optionTypes.list({ id_in: idList, limit: idList.length }),
+ queryFn: () => optionTypeAutocompleteProps('by-ids').hydrate(idList),
enabled: idList.length > 0,
staleTime: 1000 * 60 * 5,
})
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/products/index.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/products/index.tsx
index b28b36b28d..33fbd40356 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/products/index.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/products/index.tsx
@@ -366,7 +366,7 @@ function ProductsPage() {
tableKey="products"
queryKey="products"
queryFn={(params) => adminClient.products.list(params)}
- defaultParams={{ expand: ['channels', 'product_type'] }}
+ defaultParams={{ expand: ['channels'] }}
searchParams={searchParams}
bulkActions={bulkActions}
rowActions={renderRowActions}
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/product-types.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/product-types.tsx
index 6f54714268..dd8a467e9c 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/product-types.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/product-types.tsx
@@ -463,7 +463,6 @@ function ProductTypeFormFields({ form }: { form: UseFormReturn<ProductTypeFormVa
render={({ field }) => (
<ResourceMultiAutocomplete
{...optionTypeAutocompleteProps('product-type-option-types')}
- placeholder={t('admin.product_types.option_type_search_placeholder')}
value={field.value}
onChange={field.onChange}
/>
diff --git a/packages/dashboard/src/tables/products.tsx b/packages/dashboard/src/tables/products.tsx
index d9d2740840..50a1757961 100644
--- a/packages/dashboard/src/tables/products.tsx
+++ b/packages/dashboard/src/tables/products.tsx
@@ -175,6 +175,9 @@ defineTable('products', {
// attribute on Product, but product_type is a ransackable association.
filterResource: productTypeAutocompleteProps('products-table-product-type-filter'),
ransackAttribute: 'product_type_id',
+ // Requested only while the column is on — ResourceTable unions the
+ // expands of visible columns into the list request.
+ expand: 'product_type',
default: false,
className: 'text-sm text-muted-foreground',
render: (product) => product.product_type?.name ?? '—',
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb
index bb8e061148..91bc94ddf7 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb
@@ -31,6 +31,7 @@ module Spree
def product_listing_includes
[
:tax_category,
+ :product_type,
{ product_publications: :channel },
{ primary_media: [attachment_attachment: :blob] },
{ default_variant: [:prices, { stock_items: [:stock_location, :active_stock_reservations] }] },
diff --git a/spree/core/db/sample_data/product_types.rb b/spree/core/db/sample_data/product_types.rb
index 9df8691338..c4a771fb4d 100644
--- a/spree/core/db/sample_data/product_types.rb
+++ b/spree/core/db/sample_data/product_types.rb
@@ -19,8 +19,8 @@ definitions = Spree::MetafieldDefinition.where(
resource_type: 'Spree::Product'
).index_by(&:key)
-# required: every product of the type must fill it before it can be activated.
-# optional: shown on the form, filled where it applies.
+# `required` is advisory — the dashboard marks the field, nothing rejects a
+# blank value. It still says which fields the type considers essential.
product_types = [
{
name: 'Kitchen Appliance',
@@ -66,25 +66,19 @@ product_types.each do |attributes|
product_type.option_types << color_option_type
end
- attributes[:required_fields].each_with_index do |key, index|
- definition = definitions[key]
- next if definition.nil?
+ # One pass: required fields first, so sort_order falls out of the position
+ # in the merged list rather than needing an offset.
+ fields = attributes[:required_fields].map { |key| [key, true] } +
+ attributes[:optional_fields].map { |key| [key, false] }
- join = product_type.product_type_custom_field_definitions.
- find_or_initialize_by(custom_field_definition_id: definition.id)
- join.required = true
- join.sort_order = index
- join.save!
- end
-
- attributes[:optional_fields].each_with_index do |key, index|
+ fields.each_with_index do |(key, required), index|
definition = definitions[key]
next if definition.nil?
join = product_type.product_type_custom_field_definitions.
find_or_initialize_by(custom_field_definition_id: definition.id)
- join.required = false
- join.sort_order = attributes[:required_fields].length + index
+ join.required = required
+ join.sort_order = index
join.save!
end
end