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.

4.7 KB
Raw Blame
import type {
  CustomFieldDefinitionCreateParams,
  CustomFieldDefinitionUpdateParams,
} from '@spree/admin-sdk'
import { i18n } from '@spree/dashboard-core'
import { requiredMessage } from '@spree/dashboard-ui'
import { z } from 'zod/v4'

// Mirrors `Spree::CustomField::FIELD_TYPE_TOKENS` on the backend. Keep in sync
// with the typelized `field_type` union on `CustomFieldDefinition`. Labels
// come from i18n via `fieldTypeLabel()` below.
export const FIELD_TYPES = [
  'short_text',
  'long_text',
  'rich_text',
  'number',
  'boolean',
  'json',
] as const

export type FieldType = (typeof FIELD_TYPES)[number]

/** Field types that may set `searchable: true` (mirrors backend validation). */
export const SEARCHABLE_FIELD_TYPES = ['short_text', 'long_text', 'number'] as const

/** Field types that may set `sortable: true` (mirrors backend validation). */
export const SORTABLE_FIELD_TYPES = ['short_text', 'number'] as const

export function fieldTypeSupportsSearchable(fieldType: string): boolean {
  return (SEARCHABLE_FIELD_TYPES as readonly string[]).includes(fieldType)
}

export function fieldTypeSupportsSortable(fieldType: string): boolean {
  return (SORTABLE_FIELD_TYPES as readonly string[]).includes(fieldType)
}

export function fieldTypeLabel(value: string): string {
  return i18n.t(`admin.fields.custom_field_definition.field_type.options.${value}`, {
    defaultValue: value,
  })
}

// Owner types whose values merchants can edit in the dashboard. The server
// allows more (see `Spree.custom_fields.enabled_resources`); the definition
// picker lists those under "Through the API only", since nothing here edits
// them. A type gains a place here when a screen gets the custom fields card.
export const DEFAULT_RESOURCE_TYPES = [
  'product',
  'variant',
  'order',
  'customer',
  'category',
  'collection',
  'option_type',
  'seller',
  'promotion',
  'gift_card',
  'store_credit',
  'stock_transfer',
  'purchase_order',
  'company',
  'payment_method',
  'delivery_method',
  'tax_rate',
  'product_type',
  'media',
  'store',
] as const

export type ResourceType = (typeof DEFAULT_RESOURCE_TYPES)[number] | (string & {})

/**
 * @param fallback shown for an owner the i18n bundle doesn't enumerate
 *   (plugin-defined resource types) — the server's name for it, when known;
 *   otherwise the value is humanized.
 */
export function resourceTypeLabel(value: string, fallback?: string): string {
  return i18n.t(`admin.fields.custom_field_definition.resource_type.options.${value}`, {
    defaultValue:
      fallback ?? value.replace(/_/g, ' ').replace(/^\w/, (letter) => letter.toUpperCase()),
  })
}

export const customFieldDefinitionSchema = z.object({
  label: z.string().min(1, { error: requiredMessage('custom_field_definition.label') }),
  namespace: z.string().min(1, { error: requiredMessage('custom_field_definition.namespace') }),
  key: z
    .string()
    .min(1, { error: requiredMessage('custom_field_definition.key') })
    .regex(/^[a-z0-9_]+$/i, {
      error: () => i18n.t('admin.fields.custom_field_definition.key.invalid_format'),
    }),
  field_type: z.enum(FIELD_TYPES),
  resource_type: z
    .string()
    .min(1, { error: requiredMessage('custom_field_definition.resource_type') }),
  storefront_visible: z.boolean(),
  searchable: z.boolean(),
  sortable: z.boolean(),
})

export type CustomFieldDefinitionFormValues = z.infer<typeof customFieldDefinitionSchema>

export const CUSTOM_FIELD_DEFINITION_DEFAULTS: CustomFieldDefinitionFormValues = {
  label: '',
  namespace: 'custom',
  key: '',
  field_type: 'short_text',
  resource_type: 'product',
  storefront_visible: false,
  searchable: false,
  sortable: false,
}

export function customFieldDefinitionValuesToCreateParams(
  v: CustomFieldDefinitionFormValues,
): CustomFieldDefinitionCreateParams {
  return {
    label: v.label,
    namespace: v.namespace,
    key: v.key,
    field_type: v.field_type,
    resource_type: v.resource_type,
    storefront_visible: v.storefront_visible,
    searchable: v.searchable,
    sortable: v.sortable,
  }
}

export function customFieldDefinitionValuesToUpdateParams(
  v: CustomFieldDefinitionFormValues,
): CustomFieldDefinitionUpdateParams {
  // `resource_type` and `field_type` are intentionally omitted — changing
  // either would orphan or invalidate stored values. `Spree::CustomField` rows
  // serialize via their own STI subclass (set from the definition at write
  // time), so flipping `field_type` post-hoc would leave existing values
  // misinterpreted by the UI. The controller still accepts these fields;
  // the UI just doesn't offer them as editable.
  return {
    label: v.label,
    namespace: v.namespace,
    key: v.key,
    storefront_visible: v.storefront_visible,
    searchable: v.searchable,
    sortable: v.sortable,
  }
}