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.

5.6 KB
Raw Blame
import type { GiftCard } from '@spree/admin-sdk'
import { GIFT_CARD_STATUSES, subsetOf } from '@spree/core'
import { defineTable, Money } from '@spree/dashboard-core'
import { Badge, RelativeTime, ResourceNameCell, StatusBadge } from '@spree/dashboard-ui'
import { GiftIcon } from '@spree/dashboard-ui/icons'
import i18n from 'i18next'
import { adminUserAutocompleteProps } from '../hooks/use-admin-users'
import { customerAutocompleteProps } from '../hooks/use-customers'
import { giftCardBatchAutocompleteProps } from '../hooks/use-gift-cards'
import { erasedFieldValue } from '../lib/erased-customer'
import { translatedLabel } from '../lib/translated-label'

// The filter reads the stored status. `expired` is shown on a card past its
// expiration date, but the column still says `active`, so it cannot be filtered.
const FILTERABLE_STATUSES = subsetOf(GIFT_CARD_STATUSES, [
  'active',
  'partially_redeemed',
  'redeemed',
  'canceled',
])

function statusLabel(value: string): string {
  return translatedLabel('admin.gift_cards.statuses', value)
}

defineTable<GiftCard>('gift-cards', {
  title: i18n.t('admin.nav.gift_cards'),
  docsPath: 'loyalty/gift-cards',
  description: i18n.t('admin.table_descriptions.gift_cards'),
  searchParam: 'search',
  searchPlaceholder: i18n.t('admin.gift_cards.table.search_placeholder'),
  defaultSort: { field: 'created_at', direction: 'desc' },
  emptyIcon: <GiftIcon className="size-8 text-muted-foreground" />,
  emptyMessage: i18n.t('admin.gift_cards.table.empty'),
  columns: [
    {
      key: 'code',
      label: i18n.t('admin.fields.gift_card.code.label'),
      sortable: true,
      default: true,
      render: (g) => (
        <ResourceNameCell
          id={g.id}
          dataAttr="data-gift-card-id"
          name={g.code}
          secondary={
            g.customer ? erasedFieldValue(g.customer.email, g.customer.anonymized) : undefined
          }
        />
      ),
    },
    {
      key: 'status',
      label: i18n.t('admin.fields.status.label'),
      // Ransack filters off the underlying `status` column — `display_status`
      // is a presentation-only override that returns 'expired' for active
      // cards past their `expires_at`. Server still indexes `status`.
      ransackAttribute: 'status',
      filterable: true,
      filterType: 'enum',
      filterOptions: FILTERABLE_STATUSES.map((value) => ({ value, label: statusLabel(value) })),
      quickFilter: true,
      default: true,
      render: (g) => {
        const status = g.status ?? 'active'
        return <StatusBadge status={status} label={statusLabel(status)} />
      },
    },
    {
      key: 'amount',
      label: i18n.t('admin.fields.gift_card.amount.label'),
      default: true,
      className: 'text-right tabular-nums whitespace-nowrap font-medium',
      render: (g) => <Money amount={g.amount} currency={g.currency} />,
    },
    {
      key: 'amount_used',
      label: i18n.t('admin.gift_cards.columns.used'),
      default: true,
      className: 'text-right tabular-nums whitespace-nowrap',
      render: (g) => <Money amount={g.amount_used} currency={g.currency} />,
    },
    {
      key: 'amount_remaining',
      label: i18n.t('admin.gift_cards.columns.remaining'),
      default: true,
      className: 'text-right tabular-nums whitespace-nowrap',
      render: (g) => <Money amount={g.amount_remaining} currency={g.currency} />,
    },
    {
      key: 'currency',
      label: i18n.t('admin.fields.gift_card.currency.label'),
      sortable: true,
      filterable: true,
      filterType: 'currency',
      default: false,
      render: (g) => g.currency,
    },
    {
      key: 'gift_card_batch',
      label: i18n.t('admin.gift_cards.columns.batch'),
      // Filter narrows to cards belonging to one or more batches via the
      // `gift_card_batch_id_in` Ransack predicate (whitelisted on the
      // GiftCard model). Picker renders inline batch prefixes.
      ransackAttribute: 'gift_card_batch_id',
      filterable: true,
      filterType: 'resource',
      filterResource: giftCardBatchAutocompleteProps('gift-card-batch-picker'),
      default: false,
      render: (g) =>
        g.gift_card_batch ? (
          <Badge variant="outline">{g.gift_card_batch.prefix ?? g.gift_card_batch.id}</Badge>
        ) : (
          '—'
        ),
    },
    {
      key: 'customer',
      label: i18n.t('admin.gift_cards.columns.customer'),
      // Whitelisted `customer_id` on the GiftCard model.
      ransackAttribute: 'customer_id',
      filterable: true,
      filterType: 'resource',
      filterResource: customerAutocompleteProps('gift-card-customer-filter'),
      default: false,
      render: (g) => (g.customer ? erasedFieldValue(g.customer.email, g.customer.anonymized) : '—'),
    },
    {
      key: 'created_by',
      label: i18n.t('admin.gift_cards.columns.issued_by'),
      // GiftCard whitelists `code`, `customer_id`, `status`, `gift_card_batch_id`.
      // `created_by_id` is not whitelisted yet — Ransack will reject the
      // predicate without it; we add it server-side alongside this column.
      ransackAttribute: 'created_by_id',
      filterable: true,
      filterType: 'resource',
      filterResource: adminUserAutocompleteProps('gift-card-created-by-filter'),
      default: false,
      render: (g) => g.created_by?.email ?? '—',
    },
    {
      key: 'expires_at',
      label: i18n.t('admin.fields.expires_at.label'),
      sortable: true,
      filterable: true,
      filterType: 'date',
      default: true,
      className: 'text-sm text-muted-foreground whitespace-nowrap',
      render: (g) => (g.expires_at ? <RelativeTime iso={g.expires_at} /> : '—'),
    },
  ],
})