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.

Refresh the cached store after a market write (#14452)

Damian Legawiecd14b2232ebd1
diff --git a/packages/dashboard/src/hooks/use-markets.test.ts b/packages/dashboard/src/hooks/use-markets.test.ts
new file mode 100644
index 0000000000..97d603c133
--- /dev/null
+++ b/packages/dashboard/src/hooks/use-markets.test.ts
@@ -0,0 +1,70 @@
+import { STORE_QUERY_RESOURCE, withStoreScope } from '@spree/dashboard-core'
+import { QueryClient } from '@tanstack/react-query'
+import { describe, expect, it } from 'vitest'
+import { marketUpdateInvalidations, marketWriteInvalidations } from './use-markets'
+
+const STORE_ID = 'store_abc123'
+
+/**
+ * Replays what `useResourceMutation` does on success — scope each logical key
+ * to the store, then invalidate — and reports which of the seeded queries went
+ * stale. Driving a real QueryClient means the assertions go through TanStack's
+ * own prefix matching rather than a hand-rolled key comparison.
+ */
+function staleKeysAfter(invalidate: ReturnType<typeof marketUpdateInvalidations>): string[] {
+ const queryClient = new QueryClient()
+
+ const seeded: Record<string, unknown[]> = {
+ store: [STORE_QUERY_RESOURCE, STORE_ID],
+ marketList: ['markets', STORE_ID, 'all'],
+ market: ['markets', STORE_ID, 'market_1'],
+ otherMarket: ['markets', STORE_ID, 'market_2'],
+ unrelated: ['products', STORE_ID],
+ }
+
+ for (const key of Object.values(seeded)) {
+ queryClient.setQueryData(key, {})
+ }
+
+ for (const key of invalidate) {
+ queryClient.invalidateQueries({ queryKey: withStoreScope(key, STORE_ID) })
+ }
+
+ return Object.entries(seeded)
+ .filter(([, key]) => queryClient.getQueryState(key)?.isInvalidated)
+ .map(([name]) => name)
+}
+
+describe('market write invalidations', () => {
+ // A store's supported currencies and locales are the union of its markets',
+ // so a market write leaves the cached store payload stale. Without this,
+ // pickers reading `useStore()` keep offering removed currencies/locales.
+ it('invalidates the store payload so derived currencies and locales refresh', () => {
+ expect(staleKeysAfter(marketWriteInvalidations)).toContain('store')
+ expect(staleKeysAfter(marketUpdateInvalidations('market_1'))).toContain('store')
+ })
+
+ it('invalidates every market query', () => {
+ const stale = staleKeysAfter(marketWriteInvalidations)
+
+ expect(stale).toContain('marketList')
+ expect(stale).toContain('market')
+ })
+
+ it('leaves unrelated resources cached', () => {
+ expect(staleKeysAfter(marketWriteInvalidations)).not.toContain('unrelated')
+ expect(staleKeysAfter(marketUpdateInvalidations('market_1'))).not.toContain('unrelated')
+ })
+
+ it('scopes invalidation to the current store', () => {
+ const queryClient = new QueryClient()
+ const otherStoreKey = [STORE_QUERY_RESOURCE, 'store_other']
+ queryClient.setQueryData(otherStoreKey, {})
+
+ for (const key of marketWriteInvalidations) {
+ queryClient.invalidateQueries({ queryKey: withStoreScope(key, STORE_ID) })
+ }
+
+ expect(queryClient.getQueryState(otherStoreKey)?.isInvalidated).toBe(false)
+ })
+})
diff --git a/packages/dashboard/src/hooks/use-markets.ts b/packages/dashboard/src/hooks/use-markets.ts
index 24df551ae8..6ab616db81 100644
--- a/packages/dashboard/src/hooks/use-markets.ts
+++ b/packages/dashboard/src/hooks/use-markets.ts
@@ -1,11 +1,12 @@
import type { Market, MarketCreateParams, MarketUpdateParams } from '@spree/admin-sdk'
import {
adminClient,
+ STORE_QUERY_RESOURCE,
useResourceKey,
useResourceKeyBuilder,
useResourceMutation,
} from '@spree/dashboard-core'
-import { useQuery, useQueryClient } from '@tanstack/react-query'
+import { type QueryKey, useQuery, useQueryClient } from '@tanstack/react-query'
import i18n from 'i18next'
interface UseMarketsParams {
@@ -46,10 +47,27 @@ export function useMarket(id: string | undefined) {
})
}
+/**
+ * Query keys every market write invalidates.
+ *
+ * A store's supported currencies and locales are the union of its markets', so
+ * a market write makes the cached store payload stale — pickers built on
+ * `useStore()` would keep offering currencies and locales the markets no longer
+ * carry. `STORE_QUERY_RESOURCE` refreshes them alongside the market lists.
+ *
+ * Exported so the invalidation contract is assertable without rendering hooks.
+ */
+export const marketWriteInvalidations: QueryKey[] = [['markets'], [STORE_QUERY_RESOURCE]]
+
+/** As above, plus the single-market key the update writes through. */
+export function marketUpdateInvalidations(id: string): QueryKey[] {
+ return [['markets'], ['markets', id], [STORE_QUERY_RESOURCE]]
+}
+
export function useCreateMarket() {
return useResourceMutation<Market, Error, MarketCreateParams>({
mutationFn: (params) => adminClient.markets.create(params),
- invalidate: [['markets']],
+ invalidate: marketWriteInvalidations,
successMessage: i18n.t('admin.markets.messages.created'),
errorMessage: i18n.t('admin.markets.errors.failed_to_create'),
})
@@ -58,7 +76,7 @@ export function useCreateMarket() {
export function useUpdateMarket(id: string) {
return useResourceMutation<Market, Error, MarketUpdateParams>({
mutationFn: (params) => adminClient.markets.update(id, params),
- invalidate: [['markets'], ['markets', id]],
+ invalidate: marketUpdateInvalidations(id),
successMessage: i18n.t('admin.markets.messages.updated'),
errorMessage: i18n.t('admin.markets.errors.failed_to_update'),
})
@@ -70,7 +88,7 @@ export function useDeleteMarket() {
return useResourceMutation<void, Error, string>({
mutationFn: (id) => adminClient.markets.delete(id),
- invalidate: [['markets']],
+ invalidate: marketWriteInvalidations,
successMessage: i18n.t('admin.markets.messages.deleted'),
errorMessage: i18n.t('admin.markets.errors.failed_to_delete'),
onSuccess: (_data, id) => {