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 { adminClient, i18n, useResourceMutation } from '@spree/dashboard-core'

type StoreCreditCreateParams = Parameters<typeof adminClient.customers.storeCredits.create>[1]
type StoreCreditUpdateParams = Parameters<typeof adminClient.customers.storeCredits.update>[2]

export type { StoreCreditUpdateParams }

// The `amount` arrives already normalized to canonical `"1234.56"` form — the
// form converts the merchant's localized input client-side (see
// docs/plans/5.5-client-side-money-normalization.md), so no request locale.
export function useCreateCustomerStoreCredit() {
  return useResourceMutation({
    mutationFn: ({ customerId, ...params }: StoreCreditCreateParams & { customerId: string }) =>
      adminClient.customers.storeCredits.create(customerId, params),
    invalidate: [['customers'], ['store-credits']],
    successMessage: i18n.t('admin.messages.store_credit_saved'),
  })
}

export function useUpdateCustomerStoreCredit(customerId: string, creditId: string) {
  return useResourceMutation({
    mutationFn: (params: StoreCreditUpdateParams) =>
      adminClient.customers.storeCredits.update(customerId, creditId, params),
    invalidate: [['customers', customerId], ['store-credits']],
    successMessage: i18n.t('admin.messages.store_credit_saved'),
  })
}

export function useDeleteCustomerStoreCredit() {
  return useResourceMutation({
    mutationFn: ({ customerId, id }: { customerId: string; id: string }) =>
      adminClient.customers.storeCredits.delete(customerId, id),
    invalidate: [['customers'], ['store-credits']],
    successMessage: i18n.t('admin.messages.store_credit_removed'),
  })
}