**What** - Fixes an issue where the admin dashboard would send region prices for deleted regions. - Also, includes the implementation for `GET /admin/shipping-options/:id` and its corresponding SDK function. **Why** - When a region price for a deleted region was sent to the backend it would result in the insert hitting a not null constraint on the currency_code for prices. To avoid this the dashboard should not send region prices for deleted regions. **Additional context** - Prices for deleted regions should ideally not be returned when fetching shipping option prices. However, we don't yet have a mechanism for cleaning up region prices after a region is deleted. Fixes CC-540
129 lines
3.2 KiB
TypeScript
129 lines
3.2 KiB
TypeScript
import {
|
|
QueryKey,
|
|
useMutation,
|
|
UseMutationOptions,
|
|
useQuery,
|
|
UseQueryOptions,
|
|
} from "@tanstack/react-query"
|
|
|
|
import { FetchError } from "@medusajs/js-sdk"
|
|
import { HttpTypes } from "@medusajs/types"
|
|
import { sdk } from "../../lib/client"
|
|
import { queryClient } from "../../lib/query-client"
|
|
import { queryKeysFactory } from "../../lib/query-key-factory"
|
|
import { stockLocationsQueryKeys } from "./stock-locations"
|
|
|
|
const SHIPPING_OPTIONS_QUERY_KEY = "shipping_options" as const
|
|
export const shippingOptionsQueryKeys = queryKeysFactory(
|
|
SHIPPING_OPTIONS_QUERY_KEY
|
|
)
|
|
|
|
export const useShippingOption = (
|
|
id: string,
|
|
query?: Record<string, any>,
|
|
options?: UseQueryOptions<
|
|
HttpTypes.AdminShippingOptionResponse,
|
|
Error,
|
|
HttpTypes.AdminShippingOptionResponse,
|
|
QueryKey
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.shippingOption.retrieve(id, query),
|
|
queryKey: shippingOptionsQueryKeys.detail(id),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useShippingOptions = (
|
|
query?: HttpTypes.AdminShippingOptionListParams,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
HttpTypes.AdminShippingOptionListResponse,
|
|
FetchError,
|
|
HttpTypes.AdminShippingOptionListResponse,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryFn: () => sdk.admin.shippingOption.list(query),
|
|
queryKey: shippingOptionsQueryKeys.list(query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useCreateShippingOptions = (
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminShippingOptionResponse,
|
|
FetchError,
|
|
HttpTypes.AdminCreateShippingOption
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => sdk.admin.shippingOption.create(payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: stockLocationsQueryKeys.all,
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: shippingOptionsQueryKeys.all,
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useUpdateShippingOptions = (
|
|
id: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminShippingOptionResponse,
|
|
FetchError,
|
|
HttpTypes.AdminUpdateShippingOption
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => sdk.admin.shippingOption.update(id, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: stockLocationsQueryKeys.all,
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: shippingOptionsQueryKeys.all,
|
|
})
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const useDeleteShippingOption = (
|
|
optionId: string,
|
|
options?: UseMutationOptions<
|
|
HttpTypes.AdminShippingOptionDeleteResponse,
|
|
FetchError,
|
|
void
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => sdk.admin.shippingOption.delete(optionId),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: stockLocationsQueryKeys.all,
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: shippingOptionsQueryKeys.all,
|
|
})
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|