feat(dashboard): shipping & location (#7151)
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const FULFILLMENT_PROVIDERS_QUERY_KEY = "f_providers" as const
|
||||
export const fulfillmentProvidersQueryKeys = queryKeysFactory(
|
||||
FULFILLMENT_PROVIDERS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useFulfillmentProviders = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, Error, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.fulfillmentProviders.list(query),
|
||||
queryKey: fulfillmentProvidersQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -1,14 +1,39 @@
|
||||
import { useMutation, UseMutationOptions } from "@tanstack/react-query"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
|
||||
import {
|
||||
ShippingOptionDeleteRes,
|
||||
ShippingOptionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { CreateShippingOptionReq } from "../../types/api-payloads"
|
||||
import {
|
||||
CreateShippingOptionReq,
|
||||
UpdateShippingOptionReq,
|
||||
} from "../../types/api-payloads"
|
||||
import { stockLocationsQueryKeys } from "./stock-locations"
|
||||
import { queryClient } from "../../lib/medusa"
|
||||
import { client } from "../../lib/client"
|
||||
|
||||
export const useShippingOptions = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, Error, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.shippingOptions.list(query),
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateShippingOptions = (
|
||||
options?: UseMutationOptions<
|
||||
ShippingOptionRes,
|
||||
@@ -20,9 +45,28 @@ export const useCreateShippingOptions = (
|
||||
mutationFn: (payload) => client.shippingOptions.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateShippingOptions = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
ShippingOptionRes,
|
||||
Error,
|
||||
UpdateShippingOptionReq
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.shippingOptions.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
@@ -37,7 +81,7 @@ export const useDeleteShippingOption = (
|
||||
mutationFn: () => client.shippingOptions.delete(optionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
|
||||
@@ -13,7 +13,9 @@ import {
|
||||
CreateFulfillmentSetReq,
|
||||
CreateServiceZoneReq,
|
||||
CreateStockLocationReq,
|
||||
UpdateServiceZoneReq,
|
||||
UpdateStockLocationReq,
|
||||
UpdateStockLocationSalesChannelsReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
FulfillmentSetDeleteRes,
|
||||
@@ -22,6 +24,7 @@ import {
|
||||
StockLocationListRes,
|
||||
StockLocationRes,
|
||||
} from "../../types/api-responses"
|
||||
import { salesChannelsQueryKeys } from "./sales-channels"
|
||||
|
||||
const STOCK_LOCATIONS_QUERY_KEY = "stock_locations" as const
|
||||
export const stockLocationsQueryKeys = queryKeysFactory(
|
||||
@@ -38,7 +41,7 @@ export const useStockLocation = (
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.stockLocations.retrieve(id, query),
|
||||
queryKey: stockLocationsQueryKeys.detail(id),
|
||||
queryKey: stockLocationsQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -90,7 +93,7 @@ export const useUpdateStockLocation = (
|
||||
mutationFn: (payload) => client.stockLocations.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.detail(id),
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
@@ -102,6 +105,30 @@ export const useUpdateStockLocation = (
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateStockLocationSalesChannels = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
StockLocationRes,
|
||||
Error,
|
||||
UpdateStockLocationSalesChannelsReq
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
client.stockLocations.updateSalesChannels(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteStockLocation = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<StockLocationDeleteRes, Error, void>
|
||||
@@ -134,7 +161,7 @@ export const useCreateFulfillmentSet = (
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.detail(locationId),
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
@@ -152,7 +179,33 @@ export const useCreateServiceZone = (
|
||||
client.stockLocations.createServiceZone(fulfillmentSetId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.detail(locationId),
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateServiceZone = (
|
||||
fulfillmentSetId: string,
|
||||
serviceZoneId: string,
|
||||
locationId: string,
|
||||
options?: UseMutationOptions<StockLocationRes, Error, UpdateServiceZoneReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
client.stockLocations.updateServiceZone(
|
||||
fulfillmentSetId,
|
||||
serviceZoneId,
|
||||
payload
|
||||
),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
@@ -173,6 +226,9 @@ export const useDeleteFulfillmentSet = (
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
@@ -191,6 +247,9 @@ export const useDeleteServiceZone = (
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
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"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const STORE_QUERY_KEY = "store" as const
|
||||
const storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY)
|
||||
|
||||
Reference in New Issue
Block a user