feat(dashboard): Migrate to new hooks and API client (#6963)

This commit is contained in:
Kasper Fabricius Kristensen
2024-04-05 18:27:08 +02:00
committed by GitHub
parent 5ba74ec5fc
commit 8a5c6928f7
195 changed files with 3919 additions and 6028 deletions

View File

@@ -0,0 +1,113 @@
import {
MutationOptions,
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateApiKeyReq, UpdateApiKeyReq } from "../../types/api-payloads"
import {
ApiKeyDeleteRes,
ApiKeyListRes,
ApiKeyRes,
} from "../../types/api-responses"
const API_KEYS_QUERY_KEY = "api_keys" as const
export const apiKeysQueryKeys = queryKeysFactory(API_KEYS_QUERY_KEY)
export const useApiKey = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ApiKeyRes, Error, ApiKeyRes, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.apiKeys.retrieve(id, query),
queryKey: apiKeysQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}
export const useApiKeys = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ApiKeyListRes, Error, ApiKeyListRes, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.apiKeys.list(query),
queryKey: apiKeysQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateApiKey = (
options?: UseMutationOptions<ApiKeyRes, Error, CreateApiKeyReq>
) => {
return useMutation({
mutationFn: (payload) => client.apiKeys.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateApiKey = (
id: string,
options?: UseMutationOptions<ApiKeyRes, Error, UpdateApiKeyReq>
) => {
return useMutation({
mutationFn: (payload) => client.apiKeys.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useRevokeApiKey = (
id: string,
options?: UseMutationOptions<ApiKeyRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.apiKeys.revoke(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
})
}
export const useDeleteApiKey = (
id: string,
options?: MutationOptions<ApiKeyDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.apiKeys.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
})
}

View File

@@ -0,0 +1,22 @@
import { UseMutationOptions, useMutation } from "@tanstack/react-query"
import { client } from "../../lib/client"
import { EmailPassReq } from "../../types/api-payloads"
import { EmailPassRes } from "../../types/api-responses"
export const useEmailPassLogin = (
options?: UseMutationOptions<EmailPassRes, Error, EmailPassReq>
) => {
return useMutation({
mutationFn: (payload) => client.auth.authenticate.emailPass(payload),
onSuccess: async (data: { token: string }, variables, context) => {
const { token } = data
// Create a new session with the token
await client.auth.login(token)
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,123 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import {
CreateProductCollectionReq,
UpdateProductCollectionReq,
} from "../../types/api-payloads"
import {
ProductCollectionDeleteRes,
ProductCollectionListRes,
ProductCollectionRes,
} from "../../types/api-responses"
const COLLECTION_QUERY_KEY = "collections" as const
export const collectionsQueryKeys = queryKeysFactory(COLLECTION_QUERY_KEY)
export const useCollection = (
id: string,
options?: Omit<
UseQueryOptions<
ProductCollectionRes,
Error,
ProductCollectionRes,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: collectionsQueryKeys.detail(id),
queryFn: async () => client.collections.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const useCollections = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
ProductCollectionListRes,
Error,
ProductCollectionListRes,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: collectionsQueryKeys.list(query),
queryFn: async () => client.collections.list(query),
...options,
})
return { ...data, ...rest }
}
export const useUpdateCollection = (
id: string,
options?: UseMutationOptions<
ProductCollectionRes,
Error,
UpdateProductCollectionReq
>
) => {
return useMutation({
mutationFn: (payload: UpdateProductCollectionReq) =>
client.collections.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
queryClient.invalidateQueries({
queryKey: collectionsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useCreateCollection = (
options?: UseMutationOptions<
ProductCollectionRes,
Error,
CreateProductCollectionReq
>
) => {
return useMutation({
mutationFn: (payload) => client.collections.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteCollection = (
id: string,
options?: UseMutationOptions<ProductCollectionDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.collections.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
queryClient.invalidateQueries({
queryKey: collectionsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,39 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CurrencyListRes, CurrencyRes } from "../../types/api-responses"
const CURRENCIES_QUERY_KEY = "currencies" as const
const currenciesQueryKeys = queryKeysFactory(CURRENCIES_QUERY_KEY)
export const useCurrencies = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<CurrencyListRes, Error, CurrencyListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.currencies.list(query),
queryKey: currenciesQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCurrency = (
id: string,
options?: Omit<
UseQueryOptions<CurrencyRes, Error, CurrencyRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: currenciesQueryKeys.detail(id),
queryFn: async () => client.currencies.retrieve(id),
...options,
})
return { ...data, ...rest }
}

View File

@@ -0,0 +1,77 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateCustomerReq, UpdateCustomerReq } from "../../types/api-payloads"
import { CustomerListRes, CustomerRes } from "../../types/api-responses"
const CUSTOMERS_QUERY_KEY = "customers" as const
const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY)
export const useCustomer = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<CustomerRes, Error, CustomerRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: customersQueryKeys.detail(id),
queryFn: async () => client.customers.retrieve(id, query),
...options,
})
return { ...data, ...rest }
}
export const useCustomers = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<CustomerListRes, Error, CustomerListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.customers.list(query),
queryKey: customersQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateCustomer = (
options?: UseMutationOptions<CustomerRes, Error, CreateCustomerReq>
) => {
return useMutation({
mutationFn: (payload) => client.customers.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateCustomer = (
id: string,
options?: UseMutationOptions<CustomerRes, Error, UpdateCustomerReq>
) => {
return useMutation({
mutationFn: (payload) => client.customers.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,94 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateInviteReq } from "../../types/api-payloads"
import {
InviteDeleteRes,
InviteListRes,
InviteRes,
} from "../../types/api-responses"
const INVITES_QUERY_KEY = "invites" as const
const invitesQueryKeys = queryKeysFactory(INVITES_QUERY_KEY)
export const useInvite = (
id: string,
options?: Omit<
UseQueryOptions<InviteRes, Error, InviteRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: invitesQueryKeys.detail(id),
queryFn: async () => client.invites.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const useInvites = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<InviteListRes, Error, InviteListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.invites.list(query),
queryKey: invitesQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateInvite = (
options?: UseMutationOptions<InviteRes, Error, CreateInviteReq>
) => {
return useMutation({
mutationFn: (payload) => client.invites.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useResendInvite = (
id: string,
options?: UseMutationOptions<InviteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.invites.resend(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteInvite = (
id: string,
options?: UseMutationOptions<InviteDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.invites.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,40 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { ProductTypeListRes, ProductTypeRes } from "../../types/api-responses"
const PRODUCT_TYPES_QUERY_KEY = "product_types" as const
const productTypesQueryKeys = queryKeysFactory(PRODUCT_TYPES_QUERY_KEY)
export const useProductType = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ProductTypeRes, Error, ProductTypeRes, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.productTypes.retrieve(id, query),
queryKey: productTypesQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}
export const useProductTypes = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ProductTypeListRes, Error, ProductTypeListRes, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.productTypes.list(query),
queryKey: productTypesQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}

View File

@@ -0,0 +1,40 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { ProductListRes, ProductRes } from "../../types/api-responses"
const PRODUCTS_QUERY_KEY = "products" as const
export const productsQueryKeys = queryKeysFactory(PRODUCTS_QUERY_KEY)
export const useProduct = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ProductRes, Error, ProductRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.products.retrieve(id, query),
queryKey: productsQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}
export const useProducts = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<ProductListRes, Error, ProductListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.products.list(query),
queryKey: productsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}

View File

@@ -0,0 +1,41 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { AdminGetPromotionsParams } from "@medusajs/medusa"
import { client } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { PromotionListRes, PromotionRes } from "../../types/api-responses"
const PROMOTIONS_QUERY_KEY = "promotions" as const
const promotionsQueryKeys = queryKeysFactory(PROMOTIONS_QUERY_KEY)
export const usePromotion = (
id: string,
options?: Omit<
UseQueryOptions<PromotionRes, Error, PromotionRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: promotionsQueryKeys.detail(id),
queryFn: async () => client.promotions.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const usePromotions = (
query?: AdminGetPromotionsParams,
options?: Omit<
UseQueryOptions<PromotionListRes, Error, PromotionListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: promotionsQueryKeys.list(query),
queryFn: async () => client.promotions.list(query),
...options,
})
return { ...data, ...rest }
}

View File

@@ -0,0 +1,96 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateRegionReq } from "../../types/api-payloads"
import {
RegionDeleteRes,
RegionListRes,
RegionRes,
} from "../../types/api-responses"
const REGIONS_QUERY_KEY = "regions" as const
const regionsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
export const useRegion = (
id: string,
options?: Omit<
UseQueryOptions<RegionRes, Error, RegionRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: regionsQueryKeys.detail(id),
queryFn: async () => client.regions.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const useRegions = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<RegionListRes, Error, RegionListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.regions.list(query),
queryKey: regionsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateRegion = (
options?: UseMutationOptions<RegionRes, Error, CreateRegionReq>
) => {
return useMutation({
mutationFn: (payload) => client.regions.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateRegion = (
id: string,
options?: UseMutationOptions<RegionRes, Error, CreateRegionReq>
) => {
return useMutation({
mutationFn: (payload) => client.regions.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteRegion = (
id: string,
options?: UseMutationOptions<RegionDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.regions.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,190 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import {
AddProductsSalesChannelReq,
CreateSalesChannelReq,
RemoveProductsSalesChannelReq,
UpdateSalesChannelReq,
} from "../../types/api-payloads"
import {
SalesChannelDeleteRes,
SalesChannelListRes,
SalesChannelRes,
} from "../../types/api-responses"
import { productsQueryKeys } from "./products"
const SALES_CHANNELS_QUERY_KEY = "sales-channels" as const
const salesChannelsQueryKeys = queryKeysFactory(SALES_CHANNELS_QUERY_KEY)
export const useSalesChannel = (
id: string,
options?: Omit<
UseQueryOptions<SalesChannelRes, Error, SalesChannelRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: salesChannelsQueryKeys.detail(id),
queryFn: async () => client.salesChannels.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const useSalesChannels = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<SalesChannelListRes, Error, SalesChannelListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.salesChannels.list(query),
queryKey: salesChannelsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateSalesChannel = (
options?: UseMutationOptions<SalesChannelRes, Error, CreateSalesChannelReq>
) => {
return useMutation({
mutationFn: (payload) => client.salesChannels.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateSalesChannel = (
id: string,
options?: UseMutationOptions<SalesChannelRes, Error, UpdateSalesChannelReq>
) => {
return useMutation({
mutationFn: (payload) => client.salesChannels.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteSalesChannel = (
id: string,
options?: UseMutationOptions<SalesChannelDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.salesChannels.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.detail(id),
})
// Invalidate all products to ensure they are updated if they were linked to the sales channel
queryClient.invalidateQueries({
queryKey: productsQueryKeys.all,
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useSalesChannelRemoveProducts = (
id: string,
options?: UseMutationOptions<
SalesChannelRes,
Error,
RemoveProductsSalesChannelReq
>
) => {
return useMutation({
mutationFn: (payload) => client.salesChannels.removeProducts(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.detail(id),
})
// Invalidate the products that were removed
for (const product of variables?.product_ids || []) {
queryClient.invalidateQueries({
queryKey: productsQueryKeys.detail(product),
})
}
// Invalidate the products list query
queryClient.invalidateQueries({
queryKey: productsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useSalesChannelAddProducts = (
id: string,
options?: UseMutationOptions<
SalesChannelRes,
Error,
AddProductsSalesChannelReq
>
) => {
return useMutation({
mutationFn: (payload) => client.salesChannels.addProducts(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: salesChannelsQueryKeys.detail(id),
})
// Invalidate the products that were removed
for (const product of variables?.product_ids || []) {
queryClient.invalidateQueries({
queryKey: productsQueryKeys.detail(product),
})
}
// Invalidate the products list query
queryClient.invalidateQueries({
queryKey: productsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,117 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import {
CreateStockLocationReq,
UpdateStockLocationReq,
} from "../../types/api-payloads"
import {
StockLocationDeleteRes,
StockLocationListRes,
StockLocationRes,
} from "../../types/api-responses"
const STOCK_LOCATIONS_QUERY_KEY = "stock_locations" as const
const stockLocationsQueryKeys = queryKeysFactory(STOCK_LOCATIONS_QUERY_KEY)
export const useStockLocation = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<StockLocationRes, Error, StockLocationRes, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.stockLocations.retrieve(id, query),
queryKey: stockLocationsQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}
export const useStockLocations = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
StockLocationListRes,
Error,
StockLocationListRes,
QueryKey
>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.stockLocations.list(query),
queryKey: stockLocationsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateStockLocation = (
options?: UseMutationOptions<StockLocationRes, Error, CreateStockLocationReq>
) => {
return useMutation({
mutationFn: (payload) => client.stockLocations.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateStockLocation = (
id: string,
options?: UseMutationOptions<StockLocationRes, Error, UpdateStockLocationReq>
) => {
return useMutation({
mutationFn: (payload) => client.stockLocations.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.detail(id),
})
queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteStockLocation = (
id: string,
options?: UseMutationOptions<StockLocationDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.stockLocations.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.lists(),
})
queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.detail(id),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,48 @@
import {
MutationOptions,
QueryKey,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { queryKeysFactory } from "medusa-react"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { UpdateStoreReq } from "../../types/api-payloads"
import { StoreRes } from "../../types/api-responses"
const STORE_QUERY_KEY = "store" as const
const storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY)
export const useStore = (
options?: Omit<
UseQueryOptions<StoreRes, Error, StoreRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.stores.retrieve(),
queryKey: storeQueryKeys.details(),
...options,
})
return {
...data,
...rest,
}
}
export const useUpdateStore = (
id: string,
options?: MutationOptions<StoreRes, Error, UpdateStoreReq>
) => {
return useMutation({
mutationFn: (payload) => client.stores.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: storeQueryKeys.details() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,104 @@
import {
QueryKey,
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { UpdateUserReq } from "../../types/api-payloads"
import { UserDeleteRes, UserListRes, UserRes } from "../../types/api-responses"
const USERS_QUERY_KEY = "users" as const
const usersQueryKeys = {
...queryKeysFactory(USERS_QUERY_KEY),
me: () => [USERS_QUERY_KEY, "me"],
}
export const useMe = (
options?: UseQueryOptions<UserRes, Error, UserRes, QueryKey>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.users.me(),
queryKey: usersQueryKeys.me(),
...options,
})
return {
...data,
...rest,
}
}
export const useUser = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<UserRes, Error, UserRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.users.retrieve(id, query),
queryKey: usersQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}
export const useUsers = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<UserListRes, Error, UserListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.users.list(query),
queryKey: usersQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useUpdateUser = (
id: string,
options?: UseMutationOptions<UserRes, Error, UpdateUserReq>
) => {
return useMutation({
mutationFn: (payload) => client.users.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) })
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
// We invalidate the me query in case the user updates their own profile
queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteUser = (
id: string,
options?: UseMutationOptions<UserDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.users.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: usersQueryKeys.detail(id) })
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
// We invalidate the me query in case the user updates their own profile
queryClient.invalidateQueries({ queryKey: usersQueryKeys.me() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

View File

@@ -0,0 +1,55 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import {
WorkflowExecutionListRes,
WorkflowExecutionRes,
} from "../../types/api-responses"
const WORKFLOW_EXECUTIONS_QUERY_KEY = "workflow_executions" as const
const workflowExecutionsQueryKeys = queryKeysFactory(
WORKFLOW_EXECUTIONS_QUERY_KEY
)
export const useWorkflowExecutions = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
WorkflowExecutionListRes,
Error,
WorkflowExecutionListRes,
QueryKey
>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.workflowExecutions.list(query),
queryKey: workflowExecutionsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useWorkflowExecution = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<
WorkflowExecutionRes,
Error,
WorkflowExecutionRes,
QueryKey
>,
"queryKey" | "queryFn"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.workflowExecutions.retrieve(id, query),
queryKey: workflowExecutionsQueryKeys.detail(id),
...options,
})
return { ...data, ...rest }
}