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.

2.1 KB
Raw Blame
import { useResourceKey, useResourceKeyBuilder } from '@spree/dashboard-core'
import { toastManager } from '@spree/dashboard-ui'
import type { Order } from '@spree/seller-sdk'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { sellerClient } from '../api-client'

/** One order, with everything the page renders about it. */
export function useOrder(orderId: string) {
  return useQuery({
    queryKey: useResourceKey('seller-order', orderId),
    // The shares of the basket's payment are expand-gated, and the payment
    // card is what reads them.
    queryFn: () => sellerClient().orders.get(orderId, { expand: ['payment_splits'] }),
    enabled: !!orderId,
  })
}

/** Withdrawing from an order this seller cannot fulfil. */
export function useCancelOrder(orderId: string) {
  return useOrderMutation(
    orderId,
    (params?: {
      cancel_reason_id?: string
      cancel_note?: string
      refund_payments?: boolean
      notify_customer?: boolean
    }) => sellerClient().orders.cancel(orderId, params),
  )
}

/** The seller's working note, and what the buyer asked for. */
export function useUpdateOrderNotes(orderId: string) {
  return useOrderMutation(orderId, (params: { customer_note?: string; internal_note?: string }) =>
    sellerClient().orders.notes.update(orderId, params),
  )
}

/**
 * A write against the order itself. Refreshes the order and the list behind
 * it — a canceled order reads differently in both places.
 */
export function useOrderMutation<TParams, TResult = Order>(
  orderId: string,
  mutationFn: (params: TParams) => Promise<TResult>,
) {
  const queryClient = useQueryClient()
  const buildKey = useResourceKeyBuilder()

  return useMutation({
    mutationFn,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: buildKey('seller-order', orderId) })
      queryClient.invalidateQueries({ queryKey: buildKey('seller-orders') })
    },
    onError: (error) => {
      toastManager.add({
        type: 'error',
        title: error instanceof Error ? error.message : String(error),
      })
    },
  })
}