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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix the channel-seeding reset wiping the new-product form
Damian Legawiecbf5b115dd643
diff --git a/packages/dashboard/src/components/spree/products/publishing-card.tsx b/packages/dashboard/src/components/spree/products/publishing-card.tsxindex c1752a689c..d5e110f04e 100644--- a/packages/dashboard/src/components/spree/products/publishing-card.tsx+++ b/packages/dashboard/src/components/spree/products/publishing-card.tsx@@ -82,12 +82,18 @@ export function PublishingCard({// diffing current values against defaults — so the seeded array// still reads as dirty unless we update defaults too.//- // Solution: `form.reset` with the new publications merged into the- // CURRENT defaults (not current values). `keepDirtyValues: true`- // preserves anything the merchant typed in the brief render window- // before channels resolved, and keeps those fields marked dirty- // against the new baseline. useFieldArray's array subscription picks- // up the reset and updates its internal `fields` list correctly.+ // Solution: `form.reset` with the seed merged into the form's pristine+ // defaults, snapshotted via `getValues()` on FIRST render — at that+ // moment values ARE the defaults, and `getValues()` is synchronously+ // complete. `keepDirtyValues: true` preserves anything the merchant+ // typed before channels resolved, dirty flags included. Do NOT source+ // the reset from `formState.defaultValues`: it sits behind RHF's lazy+ // proxy and can read back empty inside an effect, and resetting with+ // that object silently wipes the rest of the form (the new-product page+ // lost its seeded default variant whenever the channels query resolved+ // from a warm cache). `resetField`/`setValue` pairs don't work either —+ // an array root isn't a registered field, so they miss values or+ // defaults.//// Guard with a ref so the merchant unticking the seeded channel doesn't// re-add it on the next render.@@ -98,24 +104,23 @@ export function PublishingCard({// filters out — invisible products without a warning in the admin.const defaultChannelId = channelsResponse?.data.find((c) => c.default && c.active)?.idconst seededRef = useRef(false)+ const pristineDefaultsRef = useRef<ProductFormValues | null>(null)+ if (seedDefaultChannel && pristineDefaultsRef.current === null) {+ pristineDefaultsRef.current = form.getValues()+ }useEffect(() => {if (!seedDefaultChannel) returnif (seededRef.current) returnif (!defaultChannelId) returnseededRef.current = trueif (publicationsArray.fields.length > 0) return- const currentDefaults = form.formState.defaultValues ?? {}form.reset({- ...currentDefaults,+ ...(pristineDefaultsRef.current as ProductFormValues),product_publications: [- {- channel_id: defaultChannelId,- published_at: null,- unpublished_at: null,- },+ { channel_id: defaultChannelId, published_at: null, unpublished_at: null },],- } as ProductFormValues,+ },{ keepDirtyValues: true },)}, [seedDefaultChannel, defaultChannelId, publicationsArray, form])