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.

Fix: Stop re-adding product type categories when opening a product (#14808)

Damian Legawieca7eacce8f893
diff --git a/packages/dashboard-core/src/products/product-form-cards.tsx b/packages/dashboard-core/src/products/product-form-cards.tsx
index 524853cdcc..b899befa75 100644
--- a/packages/dashboard-core/src/products/product-form-cards.tsx
+++ b/packages/dashboard-core/src/products/product-form-cards.tsx
@@ -969,13 +969,31 @@ export function CategorizationCard({ form }: FormCardProps) {
.filter(Boolean)
}, [selectedProductType?.option_type_labels, selectedProductType?.option_type_ids, optionTypes])
+ // Seed type categories only when the merchant picks or changes the product
+ // type — not when an existing product opens and the type record loads async.
+ const previousProductTypeIdRef = useRef<string | null | undefined>(undefined)
+
useEffect(() => {
// Only where the panel files products at all: a seller's client registers
// no categories, so seeding them would dirty the form with ids their API
// drops on save — an unsaved-changes prompt over a field they cannot see.
if (!client.categories) return
- const categoryIds = selectedProductType?.category_ids
+ const currentTypeId = selectedProductTypeId ?? null
+
+ if (previousProductTypeIdRef.current === undefined) {
+ previousProductTypeIdRef.current = currentTypeId
+ return
+ }
+
+ if (!currentTypeId || selectedProductType?.id !== currentTypeId) return
+
+ const typeChanged = previousProductTypeIdRef.current !== currentTypeId
+ if (!typeChanged) return
+
+ previousProductTypeIdRef.current = currentTypeId
+
+ const categoryIds = selectedProductType.category_ids
if (!categoryIds?.length) return
const current = (form.getValues('category_ids') as string[] | undefined) ?? []
@@ -983,7 +1001,7 @@ export function CategorizationCard({ form }: FormCardProps) {
if (missing.length === 0) return
form.setValue('category_ids', [...current, ...missing], { shouldDirty: true })
- }, [selectedProductType, form, client.categories])
+ }, [selectedProductTypeId, selectedProductType, form, client.categories])
// Automatic collections rebuild their members from rules, so a hand-picked
// membership would be dropped on the next regeneration — offer manual only.
// Memoized: `initialItems` feeds a useEffect + useMemo inside
diff --git a/packages/dashboard/e2e/products-helpers.ts b/packages/dashboard/e2e/products-helpers.ts
index 37024aa95a..4160ad2d10 100644
--- a/packages/dashboard/e2e/products-helpers.ts
+++ b/packages/dashboard/e2e/products-helpers.ts
@@ -185,3 +185,29 @@ export const customFieldsCard = (page: Page) => card(page, /^Custom fields$/)
export const inventoryCard = (page: Page) => card(page, /^Inventory$/)
export const pricesCard = (page: Page) => card(page, /^Prices$/)
export const publishingCard = (page: Page) => card(page, /^Publishing$/)
+export const categorizationCard = (page: Page) => card(page, /^Categorization$/)
+
+function productCategoriesCombobox(page: Page) {
+ return categorizationCard(page)
+ .getByRole('group')
+ .filter({ has: page.getByText('Categories', { exact: true }) })
+ .getByRole('combobox')
+}
+
+/** Add a category chip on the product form's Categorization card. */
+export async function addProductCategory(page: Page, categoryName: string): Promise<void> {
+ await productCategoriesCombobox(page).fill(categoryName)
+ await page
+ .getByRole('option', { name: new RegExp(`^${categoryName}$`, 'i') })
+ .first()
+ .click()
+}
+
+/** Remove a category chip from the product form's Categorization card. */
+export async function removeProductCategory(page: Page, categoryName: string): Promise<void> {
+ const card = categorizationCard(page)
+ await card
+ .locator('[data-slot="combobox-chip"]', { hasText: categoryName })
+ .locator('[data-slot="combobox-chip-remove"]')
+ .click()
+}
diff --git a/packages/dashboard/e2e/products-type-categories.spec.ts b/packages/dashboard/e2e/products-type-categories.spec.ts
new file mode 100644
index 0000000000..6726267168
--- /dev/null
+++ b/packages/dashboard/e2e/products-type-categories.spec.ts
@@ -0,0 +1,100 @@
+import { expect, type Page, test } from '@playwright/test'
+import { gotoIndex, login } from './helpers'
+import {
+ addProductCategory,
+ categorizationCard,
+ PRODUCTS_PATH,
+ removeProductCategory,
+} from './products-helpers'
+
+const CATEGORIES_PATH = (storeId: string) => `/${storeId}/products/categories`
+const PRODUCT_TYPES_PATH = (storeId: string) => `/${storeId}/settings/product-types`
+
+async function createTopLevelCategory(page: Page, name: string) {
+ await page.getByRole('button', { name: /new category/i }).click()
+ await expect(page.getByRole('heading', { name: /new category/i })).toBeVisible({
+ timeout: 15_000,
+ })
+ await page.locator('#category-name').fill(name)
+ await page.getByRole('button', { name: /^save$/i }).click()
+ await expect(page).toHaveURL(/\/products\/categories\/(?!new(?:\?|$))/, { timeout: 15_000 })
+ await page.getByRole('button', { name: /^back$/i }).click()
+ await expect(page).toHaveURL(/\/products\/categories(?:\?|$)/, { timeout: 15_000 })
+}
+
+async function pickCategoryInSheet(page: Page, categoryName: string) {
+ await page
+ .getByRole('dialog')
+ .getByRole('combobox', { name: /search categories/i })
+ .fill(categoryName)
+ await page
+ .getByRole('option', { name: new RegExp(`^${categoryName}$`, 'i') })
+ .first()
+ .click()
+}
+
+test.describe('product type default categories', () => {
+ test('does not re-add a removed type category after reload', async ({ page }) => {
+ test.setTimeout(120_000)
+ const creds = await login(page)
+ const stamp = Date.now()
+ const parentCategory = `E2E Type Parent ${stamp}`
+ const otherCategory = `E2E Type Other ${stamp}`
+ const typeName = `E2E Type ${stamp}`
+ const productName = `E2E Type Category Product ${stamp}`
+
+ await gotoIndex(page, CATEGORIES_PATH(creds.store_id), /new category/i)
+ await createTopLevelCategory(page, parentCategory)
+ await createTopLevelCategory(page, otherCategory)
+
+ await gotoIndex(page, PRODUCT_TYPES_PATH(creds.store_id), /add product type/i)
+ await page.getByRole('button', { name: /add product type/i }).click()
+ await expect(page.getByRole('heading', { name: /new product type/i })).toBeVisible()
+ await page.locator('#name').fill(typeName)
+ await pickCategoryInSheet(page, parentCategory)
+ await page.getByRole('button', { name: /create product type/i }).click()
+ await expect(page.getByText(typeName)).toBeVisible({ timeout: 15_000 })
+
+ await gotoIndex(page, PRODUCTS_PATH(creds.store_id), /add product/i)
+ await page.getByRole('button', { name: /add product/i }).click()
+ await expect(page.getByRole('heading', { name: /^new product$/i })).toBeVisible()
+ await page.getByLabel(/^name$/i).fill(productName)
+
+ const categorization = categorizationCard(page)
+ await categorization.getByRole('combobox').first().click()
+ await page.getByRole('option', { name: typeName, exact: true }).click()
+ await addProductCategory(page, otherCategory)
+
+ await page.getByRole('button', { name: /^create product$/i }).click()
+ await expect(page).toHaveURL(new RegExp(`/${creds.store_id}/products/prod_[^/]+$`), {
+ timeout: 15_000,
+ })
+
+ await expect(
+ categorization.locator('[data-slot="combobox-chip"]', { hasText: parentCategory }),
+ ).toBeVisible()
+ await expect(
+ categorization.locator('[data-slot="combobox-chip"]', { hasText: otherCategory }),
+ ).toBeVisible()
+
+ await removeProductCategory(page, parentCategory)
+ await expect(
+ categorization.locator('[data-slot="combobox-chip"]', { hasText: parentCategory }),
+ ).toHaveCount(0)
+
+ await page.getByRole('button', { name: /save product/i }).click()
+ await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled({
+ timeout: 15_000,
+ })
+
+ await page.reload()
+
+ await expect(
+ categorization.locator('[data-slot="combobox-chip"]', { hasText: parentCategory }),
+ ).toHaveCount(0)
+ await expect(
+ categorization.locator('[data-slot="combobox-chip"]', { hasText: otherCategory }),
+ ).toHaveCount(1)
+ await expect(page.getByRole('button', { name: /save product/i })).toBeDisabled()
+ })
+})