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: email template editor did not suggest address fields
Damian Legawiec172acf3b085a
diff --git a/packages/dashboard/src/components/spree/email-templates/preview-card.tsx b/packages/dashboard/src/components/spree/email-templates/preview-card.tsxindex a182e38766..a9e83b53a6 100644--- a/packages/dashboard/src/components/spree/email-templates/preview-card.tsx+++ b/packages/dashboard/src/components/spree/email-templates/preview-card.tsx@@ -31,7 +31,11 @@ import {useEmailTemplates,} from '../../../hooks/use-email-templates'import { emailTemplateName } from '../../../lib/email-template-name'-import { flattenVariables, templateVariables } from '../../../lib/email-template-variables'+import {+ documentedPaths,+ flattenVariables,+ templateVariables,+} from '../../../lib/email-template-variables'import { EmailPreviewFrame, type EmailPreviewWidth } from './email-preview-frame'/** How long typing pauses before a preview is rendered again. */@@ -111,9 +115,9 @@ export function useLivePreview(info: descriptions.get(path),}))const known = new Set(fromPreview.map((completion) => completion.label))- const documentedOnly = documented- .filter((variable) => !known.has(variable.name))- .map((variable) => ({ label: variable.name, info: descriptions.get(variable.name) }))+ const documentedOnly = documentedPaths(templateId)+ .filter((path) => !known.has(path))+ .map((path) => ({ label: path, info: descriptions.get(path) }))return [...fromPreview, ...documentedOnly]}, [data, templateId, t])diff --git a/packages/dashboard/src/lib/email-template-variables.test.ts b/packages/dashboard/src/lib/email-template-variables.test.tsindex 53f7939437..091d6be489 100644--- a/packages/dashboard/src/lib/email-template-variables.test.ts+++ b/packages/dashboard/src/lib/email-template-variables.test.ts@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest'-import { flattenVariables } from './email-template-variables'+import { documentedPaths, flattenVariables } from './email-template-variables'describe('flattenVariables', () => {it('lists dotted paths with samples, reading a list through its first item', () => {@@ -24,3 +24,13 @@ describe('flattenVariables', () => {expect(flattenVariables(deep).map((entry) => entry.path)).not.toContain('a.b.c.d.e.f')})})++describe('documentedPaths', () => {+ it('suggests address fields even when the sample order has no address', () => {+ const paths = documentedPaths('spree.order_mailer.confirm_email')++ expect(paths).toContain('order.billing_address.first_name')+ expect(paths).toContain('order.shipping_address.city')+ expect(paths).toContain('store.name')+ })+})diff --git a/packages/dashboard/src/lib/email-template-variables.ts b/packages/dashboard/src/lib/email-template-variables.tsindex ae6daf48c9..04ebe07e9a 100644--- a/packages/dashboard/src/lib/email-template-variables.ts+++ b/packages/dashboard/src/lib/email-template-variables.ts@@ -1,4 +1,5 @@import type {+ Address,Company,Customer,EmailFulfillment,@@ -39,6 +40,31 @@ const ORDER_FIELDS = ['payments',] as const satisfies FieldsOf<EmailOrder>+/**+ * Fields of objects a sample record often lacks (a pickup order has no+ * shipping address), so they are suggested even when the preview has none.+ */+const ADDRESS_FIELDS = [+ 'first_name',+ 'last_name',+ 'full_name',+ 'company',+ 'address1',+ 'address2',+ 'city',+ 'postal_code',+ 'state_name',+ 'state_code',+ 'country_name',+ 'country_code',+ 'phone',+] as const satisfies FieldsOf<Address>++const NESTED_FIELDS: Record<string, readonly string[]> = {+ billing_address: ADDRESS_FIELDS,+ shipping_address: ADDRESS_FIELDS,+}+const order = variable('order', ORDER_FIELDS)const resend = variable('resend')@@ -115,6 +141,20 @@ export function templateVariables(templateId: string): EmailTemplateVariable[] {return [...(EMAIL_TEMPLATE_VARIABLES[templateId] ?? []), ...SHARED_EMAIL_VARIABLES]}+/**+ * Every dotted path the manifest documents for a template: each variable,+ * its highlighted fields, and the fields of those that are objects.+ */+export function documentedPaths(templateId: string): string[] {+ return templateVariables(templateId).flatMap(({ name, fields = [] }) => [+ name,+ ...fields.flatMap((field) => [+ `${name}.${field}`,+ ...(NESTED_FIELDS[field] ?? []).map((nested) => `${name}.${field}.${nested}`),+ ]),+ ])+}+/** Spree's own Liquid filters, on top of Liquid's standard ones. */export const EMAIL_TEMPLATE_FILTERS = ['money', 'money_with_currency', 'date', 't'] as const