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.

1.6 KB
Raw Blame
import type { PriceBulkUpsertParams } from '@spree/admin-sdk'
import { adminClient, useResourceMutation } from '@spree/dashboard-core'

/**
 * Mutation hook for the bulk-upsert endpoint behind the price spreadsheet.
 * Invalidates every `prices` query so cards that show price counts (price
 * list "N prices configured" hints, base-price grids on other surfaces)
 * refetch automatically. Toasts and form-error mapping live in the caller
 * so the spreadsheet can render its own dirty-state UI on failure.
 *
 * `catalogs` goes with it: a catalog's assortment rows carry the price its
 * agreement charges, so writing prices leaves that listing stale — the grid
 * closes onto rows still showing what the products used to cost.
 *
 * Amounts arrive already normalized to canonical `"1234.56"` form — the editor
 * converts the merchant's localized input client-side (see
 * docs/plans/5.5-client-side-money-normalization.md), so no request locale.
 *
 * @param priceListId the list being edited, when there is one: its products
 *   card reads the rows this save writes, so that nested key is refreshed
 *   — and only that one, since refetching the list record itself would
 *   reset the page's form under a merchant's unsaved edits.
 */
export function useBulkUpsertPrices(priceListId?: string | null) {
  return useResourceMutation<{ price_count: number }, Error, PriceBulkUpsertParams>({
    mutationFn: (params) => adminClient.prices.bulkUpsert(params),
    invalidate: [
      ['prices'],
      ['catalogs'],
      ...(priceListId ? [['price-lists', priceListId, 'products']] : []),
    ],
    successMessage: false,
    errorMessage: false,
  })
}