feat(admin-sdk,admin-bundler,admin-shared,medusa): Restructure admin packages (#8988)
**What** - Renames /admin-next -> /admin - Renames @medusajs/admin-sdk -> @medusajs/admin-bundler - Creates a new package called @medusajs/admin-sdk that will hold all tooling relevant to creating admin extensions. This is currently `defineRouteConfig` and `defineWidgetConfig`, but will eventually also export methods for adding custom fields, register translation, etc. - cc: @shahednasser we should update the examples in the docs so these functions are imported from `@medusajs/admin-sdk`. People will also need to install the package in their project, as it's no longer a transient dependency. - cc: @olivermrbl we might want to publish a changelog when this is merged, as it is a breaking change, and will require people to import the `defineXConfig` from the new package instead of `@medusajs/admin-shared`. - Updates CODEOWNERS so /admin packages does not require a review from the UI team.
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import {
|
||||
MutationOptions,
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { salesChannelsQueryKeys } from "./sales-channels"
|
||||
|
||||
const API_KEYS_QUERY_KEY = "api_keys" as const
|
||||
export const apiKeysQueryKeys = queryKeysFactory(API_KEYS_QUERY_KEY)
|
||||
|
||||
export const useApiKey = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.apiKey.retrieve(id),
|
||||
queryKey: apiKeysQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useApiKeys = (
|
||||
query?: HttpTypes.AdminGetApiKeysParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetApiKeysParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminApiKeyListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.apiKey.list(query),
|
||||
queryKey: apiKeysQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateApiKey = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateApiKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.apiKey.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateApiKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.apiKey.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<HttpTypes.AdminApiKeyResponse, FetchError, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.apiKey.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<
|
||||
HttpTypes.AdminApiKeyDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.apiKey.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useBatchRemoveSalesChannelsFromApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["remove"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.apiKey.batchSalesChannels(id, { remove: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: salesChannelsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useBatchAddSalesChannelsToApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["add"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.apiKey.batchSalesChannels(id, { add: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: salesChannelsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { UseMutationOptions, useMutation } from "@tanstack/react-query"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
export const useSignInWithEmailPassword = (
|
||||
options?: UseMutationOptions<
|
||||
string,
|
||||
FetchError,
|
||||
HttpTypes.AdminSignUpWithEmailPassword
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.auth.login("user", "emailpass", payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useSignUpWithEmailPass = (
|
||||
options?: UseMutationOptions<
|
||||
string,
|
||||
FetchError,
|
||||
HttpTypes.AdminSignInWithEmailPassword
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.auth.register("user", "emailpass", payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useLogout = (options?: UseMutationOptions<void, FetchError>) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.auth.logout(),
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { HttpTypes, LinkMethodRequest } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { promotionsQueryKeys } from "./promotions"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const REGIONS_QUERY_KEY = "campaigns" as const
|
||||
export const campaignsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
|
||||
|
||||
export const useCampaign = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminGetCampaignParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminCampaignResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCampaignResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: campaignsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.campaign.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCampaigns = (
|
||||
query?: HttpTypes.AdminGetCampaignsParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminCampaignListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCampaignListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.campaign.list(query),
|
||||
queryKey: campaignsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateCampaign = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCampaignResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateCampaign
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.campaign.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateCampaign = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCampaignResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCampaign
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.campaign.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteCampaign = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.DeleteResponse<"campaign">,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.campaign.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddOrRemoveCampaignPromotions = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCampaignResponse,
|
||||
FetchError,
|
||||
LinkMethodRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.campaign.batchPromotions(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() })
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { productsQueryKeys } from "./products"
|
||||
|
||||
const CATEGORIES_QUERY_KEY = "categories" as const
|
||||
export const categoriesQueryKeys = queryKeysFactory(CATEGORIES_QUERY_KEY)
|
||||
|
||||
export const useProductCategory = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminProductCategoryParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductCategoryResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductCategoryResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: categoriesQueryKeys.detail(id, query),
|
||||
queryFn: () => sdk.admin.productCategory.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useProductCategories = (
|
||||
query?: HttpTypes.AdminProductCategoryListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductCategoryListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductCategoryListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: categoriesQueryKeys.list(query),
|
||||
queryFn: () => sdk.admin.productCategory.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProductCategory = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductCategoryResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateProductCategory
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productCategory.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: categoriesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductCategoryResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProductCategory
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productCategory.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: categoriesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: categoriesQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProductCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductCategoryDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.productCategory.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: categoriesQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: categoriesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductCategoryProducts = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductCategoryResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProductCategoryProducts
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.productCategory.updateProducts(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: categoriesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: categoriesQueryKeys.detail(id),
|
||||
})
|
||||
/**
|
||||
* Invalidate products list query to ensure that the products collections are updated.
|
||||
*/
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,618 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { returnsQueryKeys } from "./returns"
|
||||
|
||||
const CLAIMS_QUERY_KEY = "claims" as const
|
||||
export const claimsQueryKeys = queryKeysFactory(CLAIMS_QUERY_KEY)
|
||||
|
||||
export const useClaim = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminClaimListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.claim.retrieve(id, query),
|
||||
queryKey: claimsQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useClaims = (
|
||||
query?: HttpTypes.AdminClaimListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminClaimListParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.claim.list(query),
|
||||
queryKey: claimsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateClaim = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateClaim
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateClaim) =>
|
||||
sdk.admin.claim.create(payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelClaim = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.claim.cancel(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteClaim = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimDeleteResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.claim.delete(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddClaimItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddClaimItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddClaimItems) =>
|
||||
sdk.admin.claim.addItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateClaimItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateClaimItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateClaimItem & { actionId: string }) => {
|
||||
return sdk.admin.claim.updateItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveClaimItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.return.removeReturnItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddClaimInboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddClaimInboundItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddClaimInboundItems) =>
|
||||
sdk.admin.claim.addInboundItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateClaimInboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateClaimInboundItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateClaimInboundItem & { actionId: string }) => {
|
||||
return sdk.admin.claim.updateInboundItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveClaimInboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError, string>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.claim.removeInboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddClaimInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimAddInboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminClaimAddInboundShipping) =>
|
||||
sdk.admin.claim.addInboundShipping(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateClaimInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimUpdateInboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminClaimUpdateInboundShipping & { actionId: string }) =>
|
||||
sdk.admin.claim.updateInboundShipping(id, actionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteClaimInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError, string>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.claim.deleteInboundShipping(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddClaimOutboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddClaimOutboundItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddClaimOutboundItems) =>
|
||||
sdk.admin.claim.addOutboundItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateClaimOutboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateClaimOutboundItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateClaimOutboundItem & { actionId: string }) => {
|
||||
return sdk.admin.claim.updateOutboundItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveClaimOutboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError, string>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.claim.removeOutboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddClaimOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimAddOutboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminClaimAddOutboundShipping) =>
|
||||
sdk.admin.claim.addOutboundShipping(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateClaimOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminClaimUpdateOutboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminClaimUpdateOutboundShipping & { actionId: string }) =>
|
||||
sdk.admin.claim.updateOutboundShipping(id, actionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteClaimOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError, string>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.claim.deleteOutboundShipping(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useClaimConfirmRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminClaimResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminRequestClaim
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminRequestClaim) =>
|
||||
sdk.admin.claim.request(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.all,
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelClaimRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminClaimResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.claim.cancelRequest(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: claimsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { FindParams, HttpTypes, PaginatedResponse } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { productsQueryKeys } from "./products"
|
||||
|
||||
const COLLECTION_QUERY_KEY = "collections" as const
|
||||
export const collectionsQueryKeys = queryKeysFactory(COLLECTION_QUERY_KEY)
|
||||
|
||||
export const useCollection = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: collectionsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.productCollection.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCollections = (
|
||||
query?: FindParams & HttpTypes.AdminCollectionFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>,
|
||||
FetchError,
|
||||
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: collectionsQueryKeys.list(query),
|
||||
queryFn: async () => sdk.admin.productCollection.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUpdateCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCollection
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productCollection.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 useUpdateCollectionProducts = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCollectionProducts
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.productCollection.updateProducts(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: collectionsQueryKeys.detail(id),
|
||||
})
|
||||
/**
|
||||
* Invalidate products list query to ensure that the products collections are updated.
|
||||
*/
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateCollection = (
|
||||
options?: UseMutationOptions<
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateCollection
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productCollection.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCollectionDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.productCollection.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: collectionsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const CURRENCIES_QUERY_KEY = "currencies" as const
|
||||
const currenciesQueryKeys = queryKeysFactory(CURRENCIES_QUERY_KEY)
|
||||
|
||||
export const useCurrencies = (
|
||||
query?: HttpTypes.AdminCurrencyListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminCurrencyListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCurrencyListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.currency.list(query),
|
||||
queryKey: currenciesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCurrency = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminCurrencyParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminCurrencyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCurrencyResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: currenciesQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.currency.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { customersQueryKeys } from "./customers"
|
||||
|
||||
const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
|
||||
export const customerGroupsQueryKeys = queryKeysFactory(
|
||||
CUSTOMER_GROUPS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useCustomerGroup = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.customerGroup.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCustomerGroups = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetCustomerGroupsParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminCustomerGroupListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.customerGroup.list(query),
|
||||
queryKey: customerGroupsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateCustomerGroup = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateCustomerGroup
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.customerGroup.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateCustomerGroup = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCustomerGroup
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.customerGroup.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteCustomerGroup = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerGroupDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.customerGroup.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddCustomersToGroup = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["add"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.customerGroup.batchCustomers(id, { add: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customersQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveCustomersFromGroup = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerGroupResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["remove"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.customerGroup.batchCustomers(id, { remove: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customerGroupsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customersQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes, PaginatedResponse } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const CUSTOMERS_QUERY_KEY = "customers" as const
|
||||
export const customersQueryKeys = queryKeysFactory(CUSTOMERS_QUERY_KEY)
|
||||
|
||||
export const useCustomer = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ customer: HttpTypes.AdminCustomer },
|
||||
FetchError,
|
||||
{ customer: HttpTypes.AdminCustomer },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: customersQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.customer.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCustomers = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>,
|
||||
FetchError,
|
||||
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.customer.list(query),
|
||||
queryKey: customersQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateCustomer = (
|
||||
options?: UseMutationOptions<
|
||||
{ customer: HttpTypes.AdminCustomer },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateCustomer
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.customer.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateCustomer = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
{ customer: HttpTypes.AdminCustomer },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCustomer
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.customer.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: customersQueryKeys.detail(id) })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteCustomer = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminCustomerDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.customer.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: customersQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: customersQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,565 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { returnsQueryKeys } from "./returns"
|
||||
|
||||
const EXCHANGES_QUERY_KEY = "exchanges" as const
|
||||
export const exchangesQueryKeys = queryKeysFactory(EXCHANGES_QUERY_KEY)
|
||||
|
||||
export const useExchange = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminExchangeListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.exchange.retrieve(id, query),
|
||||
queryKey: exchangesQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useExchanges = (
|
||||
query?: HttpTypes.AdminExchangeListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminExchangeListParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.exchange.list(query),
|
||||
queryKey: exchangesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateExchange = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateExchange
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateExchange) =>
|
||||
sdk.admin.exchange.create(payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelExchange = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminExchangeResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.exchange.cancel(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteExchange = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeDeleteResponse,
|
||||
FetchError
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.exchange.delete(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddExchangeItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddExchangeItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddExchangeItems) =>
|
||||
sdk.admin.exchange.addItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateExchangeItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateExchangeItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateExchangeItem & { actionId: string }) => {
|
||||
return sdk.admin.exchange.updateItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveExchangeItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.return.removeReturnItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddExchangeInboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddExchangeInboundItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddExchangeInboundItems) =>
|
||||
sdk.admin.exchange.addInboundItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateExchangeInboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateExchangeInboundItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateExchangeInboundItem & { actionId: string }) => {
|
||||
return sdk.admin.exchange.updateInboundItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveExchangeInboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.removeInboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddExchangeInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeAddInboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminExchangeAddInboundShipping) =>
|
||||
sdk.admin.exchange.addInboundShipping(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateExchangeInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeUpdateInboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminExchangeUpdateInboundShipping & { actionId: string }) =>
|
||||
sdk.admin.exchange.updateInboundShipping(id, actionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteExchangeInboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.deleteInboundShipping(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddExchangeOutboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddExchangeOutboundItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddExchangeOutboundItems) =>
|
||||
sdk.admin.exchange.addOutboundItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateExchangeOutboundItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateExchangeOutboundItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateExchangeOutboundItem & { actionId: string }) => {
|
||||
return sdk.admin.exchange.updateOutboundItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveExchangeOutboundItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.removeOutboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddExchangeOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeAddOutboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminExchangeAddOutboundShipping) =>
|
||||
sdk.admin.exchange.addOutboundShipping(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateExchangeOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExchangeUpdateOutboundShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminExchangeUpdateOutboundShipping & { actionId: string }) =>
|
||||
sdk.admin.exchange.updateOutboundShipping(id, actionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteExchangeOutboundShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.deleteOutboundShipping(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useExchangeConfirmRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExchangeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminRequestExchange
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminRequestExchange) =>
|
||||
sdk.admin.exchange.request(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.all,
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelExchangeRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminExchangeResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.exchange.cancelRequest(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: exchangesQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const FULFILLMENT_PROVIDERS_QUERY_KEY = "fulfillment_providers" as const
|
||||
export const fulfillmentProvidersQueryKeys = queryKeysFactory(
|
||||
FULFILLMENT_PROVIDERS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useFulfillmentProviders = (
|
||||
query?: HttpTypes.AdminFulfillmentProviderListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminFulfillmentProviderListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminFulfillmentProviderListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.fulfillmentProvider.list(query),
|
||||
queryKey: fulfillmentProvidersQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { shippingOptionsQueryKeys } from "./shipping-options"
|
||||
import { stockLocationsQueryKeys } from "./stock-locations"
|
||||
|
||||
const FULFILLMENT_SETS_QUERY_KEY = "fulfillment_sets" as const
|
||||
export const fulfillmentSetsQueryKeys = queryKeysFactory(
|
||||
FULFILLMENT_SETS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useDeleteFulfillmentSet = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseMutationOptions<
|
||||
HttpTypes.AdminFulfillmentSetDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>,
|
||||
"mutationFn"
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.fulfillmentSet.delete(id),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentSetsQueryKeys.detail(id),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentSetsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
// We need to invalidate all related entities. We invalidate using `all` keys to ensure that all relevant entities are invalidated.
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
refetchType: "all",
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: shippingOptionsQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useFulfillmentSetServiceZone = (
|
||||
fulfillmentSetId: string,
|
||||
serviceZoneId: string,
|
||||
query?: HttpTypes.SelectParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminServiceZoneResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminServiceZoneResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () =>
|
||||
sdk.admin.fulfillmentSet.retrieveServiceZone(
|
||||
fulfillmentSetId,
|
||||
serviceZoneId,
|
||||
query
|
||||
),
|
||||
queryKey: fulfillmentSetsQueryKeys.detail(fulfillmentSetId, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateFulfillmentSetServiceZone = (
|
||||
fulfillmentSetId: string,
|
||||
options?: Omit<
|
||||
UseMutationOptions<
|
||||
HttpTypes.AdminFulfillmentSetResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateFulfillmentSetServiceZone,
|
||||
QueryKey
|
||||
>,
|
||||
"mutationFn"
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.fulfillmentSet.createServiceZone(fulfillmentSetId, payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentSetsQueryKeys.lists(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateFulfillmentSetServiceZone = (
|
||||
fulfillmentSetId: string,
|
||||
serviceZoneId: string,
|
||||
options?: Omit<
|
||||
UseMutationOptions<
|
||||
HttpTypes.AdminFulfillmentSetResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateFulfillmentSetServiceZone,
|
||||
QueryKey
|
||||
>,
|
||||
"mutationFn"
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.fulfillmentSet.updateServiceZone(
|
||||
fulfillmentSetId,
|
||||
serviceZoneId,
|
||||
payload
|
||||
),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentSetsQueryKeys.lists(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteFulfillmentServiceZone = (
|
||||
fulfillmentSetId: string,
|
||||
serviceZoneId: string,
|
||||
options?: Omit<
|
||||
UseMutationOptions<
|
||||
HttpTypes.AdminServiceZoneDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>,
|
||||
"mutationFn"
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () =>
|
||||
sdk.admin.fulfillmentSet.deleteServiceZone(
|
||||
fulfillmentSetId,
|
||||
serviceZoneId
|
||||
),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentSetsQueryKeys.lists(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: shippingOptionsQueryKeys.lists(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { useMutation, UseMutationOptions } from "@tanstack/react-query"
|
||||
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const FULFILLMENTS_QUERY_KEY = "fulfillments" as const
|
||||
export const fulfillmentsQueryKeys = queryKeysFactory(FULFILLMENTS_QUERY_KEY)
|
||||
|
||||
export const useCreateFulfillment = (
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: any) => sdk.admin.fulfillment.create(payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: fulfillmentsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelFulfillment = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.fulfillment.cancel(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: fulfillmentsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateFulfillmentShipment = (
|
||||
fulfillmentId: string,
|
||||
options?: UseMutationOptions<
|
||||
{ fulfillment: HttpTypes.AdminFulfillment },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateFulfillmentShipment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateFulfillmentShipment) =>
|
||||
sdk.admin.fulfillment.createShipment(fulfillmentId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
export * from "./api-keys"
|
||||
export * from "./auth"
|
||||
export * from "./campaigns"
|
||||
export * from "./categories"
|
||||
export * from "./collections"
|
||||
export * from "./currencies"
|
||||
export * from "./customer-groups"
|
||||
export * from "./customers"
|
||||
export * from "./fulfillment"
|
||||
export * from "./fulfillment-providers"
|
||||
export * from "./fulfillment-sets"
|
||||
export * from "./inventory"
|
||||
export * from "./invites"
|
||||
export * from "./notification"
|
||||
export * from "./orders"
|
||||
export * from "./payment-collections"
|
||||
export * from "./payments"
|
||||
export * from "./price-lists"
|
||||
export * from "./product-types"
|
||||
export * from "./product-variants"
|
||||
export * from "./products"
|
||||
export * from "./promotions"
|
||||
export * from "./refund-reasons"
|
||||
export * from "./regions"
|
||||
export * from "./reservations"
|
||||
export * from "./sales-channels"
|
||||
export * from "./shipping-options"
|
||||
export * from "./shipping-profiles"
|
||||
export * from "./stock-locations"
|
||||
export * from "./store"
|
||||
export * from "./tags"
|
||||
export * from "./tax-rates"
|
||||
export * from "./tax-regions"
|
||||
export * from "./users"
|
||||
export * from "./workflow-executions"
|
||||
@@ -0,0 +1,238 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const INVENTORY_ITEMS_QUERY_KEY = "inventory_items" as const
|
||||
export const inventoryItemsQueryKeys = queryKeysFactory(
|
||||
INVENTORY_ITEMS_QUERY_KEY
|
||||
)
|
||||
|
||||
const INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels" as const
|
||||
export const inventoryItemLevelsQueryKeys = queryKeysFactory(
|
||||
INVENTORY_ITEM_LEVELS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useInventoryItems = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminInventoryItemListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInventoryItemListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.inventoryItem.list(query),
|
||||
queryKey: inventoryItemsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useInventoryItem = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.inventoryItem.retrieve(id, query),
|
||||
queryKey: inventoryItemsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateInventoryItem = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateInventoryItem
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateInventoryItem) =>
|
||||
sdk.admin.inventoryItem.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateInventoryItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateInventoryItem
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateInventoryItem) =>
|
||||
sdk.admin.inventoryItem.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.detail(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteInventoryItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.inventoryItem.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.detail(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteInventoryItemLevel = (
|
||||
inventoryItemId: string,
|
||||
locationId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () =>
|
||||
sdk.admin.inventoryItem.deleteLevel(inventoryItemId, locationId),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useInventoryItemLevels = (
|
||||
inventoryItemId: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminInventoryLevelListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInventoryLevelListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.inventoryItem.listLevels(inventoryItemId, query),
|
||||
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUpdateInventoryLevel = (
|
||||
inventoryItemId: string,
|
||||
locationId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateInventoryLevel
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateInventoryLevel) =>
|
||||
sdk.admin.inventoryItem.updateLevel(inventoryItemId, locationId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useBatchUpdateInventoryLevels = (
|
||||
inventoryItemId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminInventoryItemResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchUpdateInventoryLevelLocation
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminBatchUpdateInventoryLevelLocation) =>
|
||||
sdk.admin.inventoryItem.batchUpdateLevels(inventoryItemId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import {
|
||||
AdminInviteResponse,
|
||||
HttpTypes,
|
||||
PaginatedResponse,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const INVITES_QUERY_KEY = "invites" as const
|
||||
const invitesQueryKeys = queryKeysFactory(INVITES_QUERY_KEY)
|
||||
|
||||
export const useInvite = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ invite: HttpTypes.AdminInviteResponse },
|
||||
FetchError,
|
||||
{ invite: HttpTypes.AdminInviteResponse },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: invitesQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.invite.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useInvites = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>,
|
||||
FetchError,
|
||||
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.invite.list(query),
|
||||
queryKey: invitesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateInvite = (
|
||||
options?: UseMutationOptions<
|
||||
{ invite: AdminInviteResponse },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateInvite
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.invite.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useResendInvite = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
{ invite: AdminInviteResponse },
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.invite.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<
|
||||
HttpTypes.AdminInviteDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.invite.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: invitesQueryKeys.detail(id) })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAcceptInvite = (
|
||||
inviteToken: string,
|
||||
options?: UseMutationOptions<
|
||||
{ user: HttpTypes.AdminUserResponse },
|
||||
FetchError,
|
||||
HttpTypes.AdminAcceptInvite & { auth_token: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => {
|
||||
const { auth_token, ...rest } = payload
|
||||
|
||||
return sdk.admin.invite.accept(
|
||||
{ invite_token: inviteToken, ...rest },
|
||||
{},
|
||||
{
|
||||
Authorization: `Bearer ${auth_token}`,
|
||||
}
|
||||
)
|
||||
},
|
||||
onSuccess: (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const NOTIFICATION_QUERY_KEY = "notification" as const
|
||||
export const notificationQueryKeys = queryKeysFactory(NOTIFICATION_QUERY_KEY)
|
||||
|
||||
export const useNotification = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminNotificationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminNotificationResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: notificationQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.notification.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useNotifications = (
|
||||
query?: HttpTypes.AdminNotificationListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminNotificationListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminNotificationListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.notification.list(query),
|
||||
queryKey: notificationQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
import { useMutation, UseMutationOptions } from "@tanstack/react-query"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
export const useCreateOrderEdit = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInitiateOrderEditRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminInitiateOrderEditRequest) =>
|
||||
sdk.admin.orderEdit.initiateRequest(payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRequestOrderEdit = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.orderEdit.request(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.changes(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useConfirmOrderEdit = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.orderEdit.confirm(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.changes(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelOrderEdit = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.orderEdit.cancelRequest(orderId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.changes(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddOrderEditItems = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddOrderEditItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddOrderEditItems) =>
|
||||
sdk.admin.orderEdit.addItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Update (quantity) of an item that was originally on the order.
|
||||
*/
|
||||
export const useUpdateOrderEditOriginalItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateOrderEditItem & { itemId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
itemId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateOrderEditItem & { itemId: string }) => {
|
||||
return sdk.admin.orderEdit.updateOriginalItem(id, itemId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Update (quantity) of an item that was added to the order edit.
|
||||
*/
|
||||
export const useUpdateOrderEditAddedItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateOrderEditItem & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateOrderEditItem & { actionId: string }) => {
|
||||
return sdk.admin.orderEdit.updateAddedItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove item that was added to the edit.
|
||||
* To remove an original item on the order, set quantity to 0.
|
||||
*/
|
||||
export const useRemoveOrderEditItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderEditPreviewResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.orderEdit.removeAddedItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
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, TQueryKey } from "../../lib/query-key-factory"
|
||||
|
||||
const ORDERS_QUERY_KEY = "orders" as const
|
||||
const _orderKeys = queryKeysFactory(ORDERS_QUERY_KEY) as TQueryKey<
|
||||
"orders",
|
||||
any,
|
||||
string
|
||||
> & {
|
||||
preview: (orderId: string) => any
|
||||
changes: (orderId: string) => any
|
||||
}
|
||||
|
||||
_orderKeys.preview = function (id: string) {
|
||||
return [this.detail(id), "preview"]
|
||||
}
|
||||
|
||||
_orderKeys.changes = function (id: string) {
|
||||
return [this.detail(id), "changes"]
|
||||
}
|
||||
|
||||
export const ordersQueryKeys = _orderKeys
|
||||
|
||||
export const useOrder = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.order.retrieve(id, query),
|
||||
queryKey: ordersQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useOrderPreview = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminOrderFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminOrderPreviewResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminOrderPreviewResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.order.retrievePreview(id, query),
|
||||
queryKey: ordersQueryKeys.preview(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useOrders = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.order.list(query),
|
||||
queryKey: ordersQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useOrderChanges = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminOrderChangesFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminOrderChangesResponse,
|
||||
Error,
|
||||
HttpTypes.AdminOrderChangesResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.order.listChanges(id, query),
|
||||
queryKey: ordersQueryKeys.changes(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateOrderFulfillment = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminOrderResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateOrderFulfillment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateOrderFulfillment) =>
|
||||
sdk.admin.order.createFulfillment(orderId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelOrderFulfillment = (
|
||||
orderId: string,
|
||||
fulfillmentId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: { no_notification?: boolean }) =>
|
||||
sdk.admin.order.cancelFulfillment(orderId, fulfillmentId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateOrderShipment = (
|
||||
orderId: string,
|
||||
fulfillmentId: string,
|
||||
options?: UseMutationOptions<
|
||||
{ order: HttpTypes.AdminOrder },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateOrderShipment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateOrderShipment) =>
|
||||
sdk.admin.order.createShipment(orderId, fulfillmentId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelOrder = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (id) => sdk.admin.order.cancel(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useMutation, UseMutationOptions } from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
|
||||
const PAYMENT_COLLECTION_QUERY_KEY = "payment-collection" as const
|
||||
export const paymentCollectionQueryKeys = queryKeysFactory(
|
||||
PAYMENT_COLLECTION_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useCreatePaymentCollection = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPaymentCollectionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreatePaymentCollection
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.paymentCollection.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: paymentCollectionQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useMarkPaymentCollectionAsPaid = (
|
||||
orderId: string,
|
||||
paymentCollectionId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPaymentCollectionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminMarkPaymentCollectionAsPaid
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.paymentCollection.markAsPaid(paymentCollectionId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: paymentCollectionQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeletePaymentCollection = (
|
||||
orderId: string,
|
||||
options?: Omit<
|
||||
UseMutationOptions<
|
||||
HttpTypes.AdminDeletePaymentCollectionResponse,
|
||||
FetchError,
|
||||
string
|
||||
>,
|
||||
"mutationFn"
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => sdk.admin.paymentCollection.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: paymentCollectionQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const PAYMENT_QUERY_KEY = "payment" as const
|
||||
export const paymentQueryKeys = queryKeysFactory(PAYMENT_QUERY_KEY)
|
||||
|
||||
export const usePaymentProviders = (
|
||||
query?: HttpTypes.AdminGetPaymentProvidersParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetPaymentProvidersParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminPaymentProviderListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.payment.listPaymentProviders(query),
|
||||
queryKey: [],
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePayment = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminPaymentFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPaymentResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPaymentResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.payment.retrieve(id, query),
|
||||
queryKey: paymentQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCapturePayment = (
|
||||
orderId: string,
|
||||
paymentId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPaymentResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCapturePayment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.payment.capture(paymentId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRefundPayment = (
|
||||
orderId: string,
|
||||
paymentId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPaymentResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminRefundPayment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.payment.refund(paymentId, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { customerGroupsQueryKeys } from "./customer-groups"
|
||||
import { productsQueryKeys } from "./products"
|
||||
|
||||
const PRICE_LISTS_QUERY_KEY = "price-lists" as const
|
||||
export const priceListsQueryKeys = queryKeysFactory(PRICE_LISTS_QUERY_KEY)
|
||||
|
||||
export const usePriceList = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminPriceListListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.priceList.retrieve(id, query),
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePriceLists = (
|
||||
query?: HttpTypes.AdminPriceListListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPriceListListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPriceListListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.priceList.list(query),
|
||||
queryKey: priceListsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreatePriceList = (
|
||||
query?: HttpTypes.AdminPriceListParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreatePriceList
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.priceList.create(payload, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: customerGroupsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdatePriceList = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminPriceListParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdatePriceList
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.priceList.update(id, payload, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: customerGroupsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeletePriceList = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPriceListDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.priceList.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useBatchPriceListPrices = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminPriceListParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchPriceListPrice
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.priceList.batchPrices(id, payload, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePriceListLinkProducts = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPriceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminLinkPriceListProducts
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.priceList.linkProducts(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const PRICE_PREFERENCES_QUERY_KEY = "price-preferences" as const
|
||||
export const pricePreferencesQueryKeys = queryKeysFactory(
|
||||
PRICE_PREFERENCES_QUERY_KEY
|
||||
)
|
||||
|
||||
export const usePricePreference = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminPricePreferenceParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPricePreferenceResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPricePreferenceResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.pricePreference.retrieve(id, query),
|
||||
queryKey: pricePreferencesQueryKeys.detail(),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePricePreferences = (
|
||||
query?: HttpTypes.AdminPricePreferenceListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPricePreferenceListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPricePreferenceListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.pricePreference.list(query),
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUpsertPricePreference = (
|
||||
id?: string | undefined,
|
||||
query?: HttpTypes.AdminPricePreferenceParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPricePreferenceResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdatePricePreference | HttpTypes.AdminCreatePricePreference
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => {
|
||||
if (id) {
|
||||
return sdk.admin.pricePreference.update(id, payload, query)
|
||||
}
|
||||
return sdk.admin.pricePreference.create(payload, query)
|
||||
},
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
})
|
||||
if (id) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.detail(id),
|
||||
})
|
||||
}
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeletePricePreference = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPricePreferenceDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.pricePreference.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const PRODUCT_TYPES_QUERY_KEY = "product_types" as const
|
||||
export const productTypesQueryKeys = queryKeysFactory(PRODUCT_TYPES_QUERY_KEY)
|
||||
|
||||
export const useProductType = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminProductTypeParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.productType.retrieve(id, query),
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useProductTypes = (
|
||||
query?: HttpTypes.AdminProductTypeListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductTypeListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTypeListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.productType.list(query),
|
||||
queryKey: productTypesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProductType = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateProductType
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productType.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductType = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProductType
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productType.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProductType = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.productType.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const PRODUCT_VARIANT_QUERY_KEY = "product_variant" as const
|
||||
export const productVariantQueryKeys = queryKeysFactory(
|
||||
PRODUCT_VARIANT_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useVariants = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.productVariant.list(query),
|
||||
queryKey: productVariantQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { inventoryItemsQueryKeys } from "./inventory.tsx"
|
||||
|
||||
const PRODUCTS_QUERY_KEY = "products" as const
|
||||
export const productsQueryKeys = queryKeysFactory(PRODUCTS_QUERY_KEY)
|
||||
|
||||
const VARIANTS_QUERY_KEY = "product_variants" as const
|
||||
export const variantsQueryKeys = queryKeysFactory(VARIANTS_QUERY_KEY)
|
||||
|
||||
const OPTIONS_QUERY_KEY = "product_options" as const
|
||||
export const optionsQueryKeys = queryKeysFactory(OPTIONS_QUERY_KEY)
|
||||
|
||||
export const useCreateProductOption = (
|
||||
productId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateProductOption) =>
|
||||
sdk.admin.product.createOption(productId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: optionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductOption = (
|
||||
productId: string,
|
||||
optionId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateProductOption) =>
|
||||
sdk.admin.product.updateOption(productId, optionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: optionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: optionsQueryKeys.detail(optionId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProductOption = (
|
||||
productId: string,
|
||||
optionId: string,
|
||||
options?: UseMutationOptions<any, FetchError, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.product.deleteOption(productId, optionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: optionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: optionsQueryKeys.detail(optionId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useProductVariant = (
|
||||
productId: string,
|
||||
variantId: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () =>
|
||||
sdk.admin.product.retrieveVariant(productId, variantId, query),
|
||||
queryKey: variantsQueryKeys.detail(variantId),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useProductVariants = (
|
||||
productId: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.product.listVariants(productId, query),
|
||||
queryKey: variantsQueryKeys.list({ productId, ...query }),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProductVariant = (
|
||||
productId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateProductVariant) =>
|
||||
sdk.admin.product.createVariant(productId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductVariant = (
|
||||
productId: string,
|
||||
variantId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateProductVariant) =>
|
||||
sdk.admin.product.updateVariant(productId, variantId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: variantsQueryKeys.detail(variantId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductVariantsBatch = (
|
||||
productId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (
|
||||
payload: HttpTypes.AdminBatchProductVariantRequest["update"]
|
||||
) =>
|
||||
sdk.admin.product.batchVariants(productId, {
|
||||
update: payload,
|
||||
}),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.details() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useProductVariantsInventoryItemsBatch = (
|
||||
productId: string,
|
||||
options?: UseMutationOptions<any, FetchError, any>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (
|
||||
payload: HttpTypes.AdminBatchProductVariantInventoryItemRequest
|
||||
) => sdk.admin.product.batchVariantInventoryItems(productId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.details() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteVariant = (
|
||||
productId: string,
|
||||
variantId: string,
|
||||
options?: UseMutationOptions<any, FetchError, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.product.deleteVariant(productId, variantId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: variantsQueryKeys.detail(variantId),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(productId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useProduct = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.product.retrieve(id, query),
|
||||
queryKey: productsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useProducts = (
|
||||
query?: HttpTypes.AdminProductListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.product.list(query),
|
||||
queryKey: productsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProduct = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateProduct
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.product.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
|
||||
// if `manage_inventory` is true on created variants that will create inventory items automatically
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProduct = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProduct
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.product.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.detail(id) })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProduct = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.product.delete(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: productsQueryKeys.detail(id) })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useExportProducts = (
|
||||
query?: HttpTypes.AdminProductListParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminExportProductResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminExportProductRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.product.export(payload, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useImportProducts = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminImportProductResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminImportProductRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.product.import(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useConfirmImportProducts = (
|
||||
options?: UseMutationOptions<{}, FetchError, string>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.product.confirmImport(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { campaignsQueryKeys } from "./campaigns"
|
||||
|
||||
const PROMOTIONS_QUERY_KEY = "promotions" as const
|
||||
export const promotionsQueryKeys = {
|
||||
...queryKeysFactory(PROMOTIONS_QUERY_KEY),
|
||||
// TODO: handle invalidations properly
|
||||
listRules: (
|
||||
id: string | null,
|
||||
ruleType: string,
|
||||
query?: HttpTypes.AdminGetPromotionRuleParams
|
||||
) => [PROMOTIONS_QUERY_KEY, id, ruleType, query],
|
||||
listRuleAttributes: (ruleType: string, promotionType?: string) => [
|
||||
PROMOTIONS_QUERY_KEY,
|
||||
ruleType,
|
||||
promotionType,
|
||||
],
|
||||
listRuleValues: (
|
||||
ruleType: string,
|
||||
ruleValue: string,
|
||||
query: HttpTypes.AdminGetPromotionsRuleValueParams
|
||||
) => [PROMOTIONS_QUERY_KEY, ruleType, ruleValue, query],
|
||||
}
|
||||
|
||||
export const usePromotion = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: promotionsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.promotion.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePromotionRules = (
|
||||
id: string | null,
|
||||
ruleType: string,
|
||||
query?: HttpTypes.AdminGetPromotionRuleParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetPromotionRuleParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminPromotionRuleListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: promotionsQueryKeys.listRules(id, ruleType, query),
|
||||
queryFn: async () => sdk.admin.promotion.listRules(id, ruleType, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePromotions = (
|
||||
query?: HttpTypes.AdminGetPromotionsParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminPromotionListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminPromotionListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: promotionsQueryKeys.list(query),
|
||||
queryFn: async () => sdk.admin.promotion.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePromotionRuleAttributes = (
|
||||
ruleType: string,
|
||||
promotionType?: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminRuleAttributeOptionsListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminRuleAttributeOptionsListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: promotionsQueryKeys.listRuleAttributes(ruleType, promotionType),
|
||||
queryFn: async () =>
|
||||
sdk.admin.promotion.listRuleAttributes(ruleType, promotionType),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePromotionRuleValues = (
|
||||
ruleType: string,
|
||||
ruleValue: string,
|
||||
query?: HttpTypes.AdminGetPromotionsRuleValueParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminRuleValueOptionsListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminRuleValueOptionsListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: promotionsQueryKeys.listRuleValues(
|
||||
ruleType,
|
||||
ruleValue,
|
||||
query || {}
|
||||
),
|
||||
queryFn: async () =>
|
||||
sdk.admin.promotion.listRuleValues(ruleType, ruleValue, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useDeletePromotion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.DeleteResponse<"promotion">,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.promotion.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: promotionsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreatePromotion = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreatePromotion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.promotion.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdatePromotion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdatePromotion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.promotion.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePromotionAddRules = (
|
||||
id: string,
|
||||
ruleType: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.BatchAddPromotionRulesReq
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.promotion.addRules(id, ruleType, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePromotionRemoveRules = (
|
||||
id: string,
|
||||
ruleType: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.BatchRemovePromotionRulesReq
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.promotion.removeRules(id, ruleType, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePromotionUpdateRules = (
|
||||
id: string,
|
||||
ruleType: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminPromotionResponse,
|
||||
FetchError,
|
||||
HttpTypes.BatchUpdatePromotionRulesReq
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.promotion.updateRules(id, ruleType, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const REFUND_REASON_QUERY_KEY = "refund-reason" as const
|
||||
export const refundReasonQueryKeys = queryKeysFactory(REFUND_REASON_QUERY_KEY)
|
||||
|
||||
export const useRefundReasons = (
|
||||
query?: HttpTypes.RefundReasonFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.RefundReasonsResponse,
|
||||
FetchError,
|
||||
HttpTypes.RefundReasonsResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.refundReason.list(query),
|
||||
queryKey: [],
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import { HttpTypes, PaginatedResponse } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { pricePreferencesQueryKeys } from "./price-preferences"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const REGIONS_QUERY_KEY = "regions" as const
|
||||
export const regionsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
|
||||
|
||||
export const useRegion = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
FetchError,
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: regionsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.region.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useRegions = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
|
||||
FetchError,
|
||||
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.region.list(query),
|
||||
queryKey: regionsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateRegion = (
|
||||
options?: UseMutationOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.region.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
{ region: HttpTypes.AdminRegion },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.region.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminRegionDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.region.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import {
|
||||
inventoryItemLevelsQueryKeys,
|
||||
inventoryItemsQueryKeys,
|
||||
} from "./inventory.tsx"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const RESERVATION_ITEMS_QUERY_KEY = "reservation_items" as const
|
||||
export const reservationItemsQueryKeys = queryKeysFactory(
|
||||
RESERVATION_ITEMS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useReservationItem = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminReservationParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminReservationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminReservationResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: reservationItemsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.reservation.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useReservationItems = (
|
||||
query?: HttpTypes.AdminGetReservationsParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetReservationsParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminReservationListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.reservation.list(query),
|
||||
queryKey: reservationItemsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUpdateReservationItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReservationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateReservation
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateReservation) =>
|
||||
sdk.admin.reservation.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: reservationItemsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: reservationItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateReservationItem = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReservationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateReservation
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateReservation) =>
|
||||
sdk.admin.reservation.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: reservationItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteReservationItem = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReservationDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.reservation.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: reservationItemsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: reservationItemsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: inventoryItemLevelsQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const RETURN_REASONS_QUERY_KEY = "return_reasons" as const
|
||||
export const returnReasonsQueryKeys = queryKeysFactory(RETURN_REASONS_QUERY_KEY)
|
||||
|
||||
export const useReturnReasons = (
|
||||
query?: HttpTypes.AdminReturnReasonListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminReturnReasonListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminReturnReasonListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.returnReason.list(query),
|
||||
queryKey: returnReasonsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useReturnReason = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminReturnReasonParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminReturnReasonResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminReturnReasonResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.returnReason.retrieve(id, query),
|
||||
queryKey: returnReasonsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateReturnReason = (
|
||||
query?: HttpTypes.AdminReturnReasonParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnReasonResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateReturnReason
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: async (data) => sdk.admin.returnReason.create(data, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
export const useUpdateReturnReason = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminReturnReasonParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnReasonResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateReturnReason
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: async (data) => sdk.admin.returnReason.update(id, data, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.detail(data.return_reason.id, query),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteReturnReason = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnReasonDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.returnReason.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnReasonsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,641 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const RETURNS_QUERY_KEY = "returns" as const
|
||||
export const returnsQueryKeys = queryKeysFactory(RETURNS_QUERY_KEY)
|
||||
|
||||
export const useReturn = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<any, FetchError, any, QueryKey>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.return.retrieve(id, query),
|
||||
queryKey: returnsQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useReturns = (
|
||||
query?: HttpTypes.AdminReturnFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminReturnFilters,
|
||||
FetchError,
|
||||
HttpTypes.AdminReturnsResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: async () => sdk.admin.return.list(query),
|
||||
queryKey: returnsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useInitiateReturn = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInitiateReturnRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminInitiateReturnRequest) =>
|
||||
sdk.admin.return.initiateRequest(payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelReturn = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminReturnResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.return.cancel(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
refetchType: "all",
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* REQUEST RETURN
|
||||
*/
|
||||
|
||||
export const useConfirmReturnRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminConfirmReturnRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminConfirmReturnRequest) =>
|
||||
sdk.admin.return.confirmRequest(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelReturnRequest = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminReturnResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.return.cancelRequest(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
refetchType: "all",
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddReturnItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddReturnItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddReturnItems) =>
|
||||
sdk.admin.return.addReturnItem(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateReturnItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateReturnItems & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateReturnItems & { actionId: string }) => {
|
||||
return sdk.admin.return.updateReturnItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveReturnItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.return.removeReturnItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateReturn = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateReturnRequest
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminUpdateReturnRequest) => {
|
||||
return sdk.admin.return.updateRequest(id, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddReturnShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddReturnShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminAddReturnShipping) =>
|
||||
sdk.admin.return.addReturnShipping(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateReturnShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminAddReturnShipping
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminAddReturnShipping & { actionId: string }) =>
|
||||
sdk.admin.return.updateReturnShipping(id, actionId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteReturnShipping = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.return.deleteReturnShipping(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* RECEIVE RETURN
|
||||
*/
|
||||
|
||||
export const useInitiateReceiveReturn = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminInitiateReceiveReturn
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminInitiateReceiveReturn) =>
|
||||
sdk.admin.return.initiateReceive(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddReceiveItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminReceiveItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminReceiveItems) =>
|
||||
sdk.admin.return.receiveItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateReceiveItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateReceiveItems & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateReceiveItems & { actionId: string }) => {
|
||||
return sdk.admin.return.updateReceiveItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveReceiveItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) => {
|
||||
return sdk.admin.return.removeReceiveItem(id, actionId)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useAddDismissItems = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminDismissItems
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminDismissItems) =>
|
||||
sdk.admin.return.dismissItems(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateDismissItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateDismissItems & { actionId: string }
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
actionId,
|
||||
...payload
|
||||
}: HttpTypes.AdminUpdateReceiveItems & { actionId: string }) => {
|
||||
return sdk.admin.return.updateDismissItem(id, actionId, payload)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useRemoveDismissItem = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (actionId: string) => {
|
||||
return sdk.admin.return.removeDismissItem(id, actionId)
|
||||
},
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useConfirmReturnReceive = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminReturnResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminConfirmReceiveReturn
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminConfirmReceiveReturn) =>
|
||||
sdk.admin.return.confirmReceive(id, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelReceiveReturn = (
|
||||
id: string,
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<HttpTypes.AdminReturnResponse, FetchError>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.return.cancelReceive(id),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
refetchType: "all", // For some reason RQ will refetch this but will return stale record from the cache
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import {
|
||||
AdminSalesChannelListResponse,
|
||||
AdminSalesChannelResponse,
|
||||
HttpTypes,
|
||||
} from "@medusajs/types"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { productsQueryKeys } from "./products"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const SALES_CHANNELS_QUERY_KEY = "sales-channels" as const
|
||||
export const salesChannelsQueryKeys = queryKeysFactory(SALES_CHANNELS_QUERY_KEY)
|
||||
|
||||
export const useSalesChannel = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminSalesChannelResponse,
|
||||
FetchError,
|
||||
AdminSalesChannelResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: salesChannelsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.salesChannel.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useSalesChannels = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminSalesChannelListResponse,
|
||||
FetchError,
|
||||
AdminSalesChannelListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.salesChannel.list(query),
|
||||
queryKey: salesChannelsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateSalesChannel = (
|
||||
options?: UseMutationOptions<
|
||||
AdminSalesChannelResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateSalesChannel
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.salesChannel.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: salesChannelsQueryKeys.lists(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateSalesChannel = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
AdminSalesChannelResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateSalesChannel
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.salesChannel.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<
|
||||
HttpTypes.AdminSalesChannelDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.salesChannel.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<
|
||||
AdminSalesChannelResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["remove"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.salesChannel.batchProducts(id, { remove: 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 || []) {
|
||||
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<
|
||||
AdminSalesChannelResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["add"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.salesChannel.batchProducts(id, { add: 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 || []) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.detail(product),
|
||||
})
|
||||
}
|
||||
|
||||
// Invalidate the products list query
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
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
|
||||
)
|
||||
|
||||
// TODO: Endpoint is not implemented yet
|
||||
// export const useShippingOption = (
|
||||
// id: string,
|
||||
// options?: UseQueryOptions<
|
||||
// HttpTypes.AdminShippingOptionResponse,
|
||||
// Error,
|
||||
// HttpTypes.AdminShippingOptionResponse,
|
||||
// QueryKey
|
||||
// >
|
||||
// ) => {
|
||||
// const { data, ...rest } = useQuery({
|
||||
// queryFn: () => sdk.admin.shippingOption.retrieve(id),
|
||||
// queryKey: shippingOptionsQueryKeys.retrieve(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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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"
|
||||
|
||||
const SHIPPING_PROFILE_QUERY_KEY = "shipping_profile" as const
|
||||
export const shippingProfileQueryKeys = queryKeysFactory(
|
||||
SHIPPING_PROFILE_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useCreateShippingProfile = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminShippingProfileResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateShippingProfile
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.shippingProfile.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: shippingProfileQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useShippingProfile = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: UseQueryOptions<
|
||||
HttpTypes.AdminShippingProfileResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminShippingProfileResponse,
|
||||
QueryKey
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.shippingProfile.retrieve(id, query),
|
||||
queryKey: shippingProfileQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useShippingProfiles = (
|
||||
query?: HttpTypes.AdminShippingProfileListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminShippingProfileListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminShippingProfileListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.shippingProfile.list(query),
|
||||
queryKey: shippingProfileQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useDeleteShippingProfile = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminShippingProfileDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.shippingProfile.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: shippingProfileQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: shippingProfileQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} 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 { fulfillmentProvidersQueryKeys } from "./fulfillment-providers"
|
||||
|
||||
const STOCK_LOCATIONS_QUERY_KEY = "stock_locations" as const
|
||||
export const stockLocationsQueryKeys = queryKeysFactory(
|
||||
STOCK_LOCATIONS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useStockLocation = (
|
||||
id: string,
|
||||
query?: HttpTypes.SelectParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.stockLocation.retrieve(id, query),
|
||||
queryKey: stockLocationsQueryKeys.detail(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useStockLocations = (
|
||||
query?: HttpTypes.AdminStockLocationListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminStockLocationListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminStockLocationListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.stockLocation.list(query),
|
||||
queryKey: stockLocationsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateStockLocation = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateStockLocation
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.stockLocation.create(payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateStockLocation = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateStockLocation
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.stockLocation.update(id, payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateStockLocationSalesChannels = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateStockLocationSalesChannels
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.stockLocation.updateSalesChannels(id, payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteStockLocation = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.stockLocation.delete(id),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.lists(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateStockLocationFulfillmentSet = (
|
||||
locationId: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateStockLocationFulfillmentSet
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.stockLocation.createFulfillmentSet(locationId, payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.all,
|
||||
refetchType: "all",
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateStockLocationFulfillmentProviders = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminStockLocationResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.stockLocation.updateFulfillmentProviders(id, payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: stockLocationsQueryKeys.details(),
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: fulfillmentProvidersQueryKeys.all,
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import {
|
||||
MutationOptions,
|
||||
QueryKey,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} 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 { pricePreferencesQueryKeys } from "./price-preferences"
|
||||
|
||||
const STORE_QUERY_KEY = "store" as const
|
||||
export const storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY)
|
||||
|
||||
/**
|
||||
* Workaround to keep the V1 version of retrieving the store.
|
||||
*/
|
||||
export async function retrieveActiveStore(
|
||||
query?: HttpTypes.AdminStoreParams
|
||||
): Promise<HttpTypes.AdminStoreResponse> {
|
||||
const response = await sdk.admin.store.list(query)
|
||||
|
||||
const activeStore = response.stores?.[0]
|
||||
|
||||
if (!activeStore) {
|
||||
throw new FetchError("No active store found", "Not Found", 404)
|
||||
}
|
||||
|
||||
return { store: activeStore }
|
||||
}
|
||||
|
||||
export const useStore = (
|
||||
query?: HttpTypes.SelectParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminStoreResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminStoreResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => retrieveActiveStore(query),
|
||||
queryKey: storeQueryKeys.details(),
|
||||
...options,
|
||||
})
|
||||
|
||||
return {
|
||||
...data,
|
||||
...rest,
|
||||
}
|
||||
}
|
||||
|
||||
export const useUpdateStore = (
|
||||
id: string,
|
||||
options?: MutationOptions<
|
||||
HttpTypes.AdminStoreResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateStore
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.store.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.list(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: pricePreferencesQueryKeys.details(),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: storeQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const TAGS_QUERY_KEY = "tags" as const
|
||||
export const productTagsQueryKeys = queryKeysFactory(TAGS_QUERY_KEY)
|
||||
|
||||
export const useProductTag = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminProductTagParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductTagResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTagResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: productTagsQueryKeys.detail(id, query),
|
||||
queryFn: async () => sdk.admin.productTag.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useProductTags = (
|
||||
query?: HttpTypes.AdminProductTagListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductTagListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTagListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: productTagsQueryKeys.list(query),
|
||||
queryFn: async () => sdk.admin.productTag.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProductTag = (
|
||||
query?: HttpTypes.AdminProductTagParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTagResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateProductTag
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: async (data) => sdk.admin.productTag.create(data, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTagsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductTag = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminProductTagParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTagResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProductTag
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: async (data) => sdk.admin.productTag.update(id, data, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTagsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTagsQueryKeys.detail(data.product_tag.id, query),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProductTag = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTagDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: async () => sdk.admin.productTag.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTagsQueryKeys.lists(),
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTagsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { taxRegionsQueryKeys } from "./tax-regions"
|
||||
|
||||
const TAX_RATES_QUERY_KEY = "tax_rates" as const
|
||||
export const taxRatesQueryKeys = queryKeysFactory(TAX_RATES_QUERY_KEY)
|
||||
|
||||
export const useTaxRate = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: taxRatesQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.taxRate.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useTaxRates = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminTaxRateListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminTaxRateListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.taxRate.list(query),
|
||||
queryKey: taxRatesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUpdateTaxRate = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateTaxRate
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.taxRate.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: taxRatesQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateTaxRate = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRateResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateTaxRate
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.taxRate.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteTaxRate = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRateDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.taxRate.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRatesQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: taxRatesQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const TAX_REGIONS_QUERY_KEY = "tax_regions" as const
|
||||
export const taxRegionsQueryKeys = queryKeysFactory(TAX_REGIONS_QUERY_KEY)
|
||||
|
||||
export const useTaxRegion = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminTaxRegionParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: taxRegionsQueryKeys.detail(id),
|
||||
queryFn: async () => sdk.admin.taxRegion.retrieve(id, query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useTaxRegions = (
|
||||
query?: HttpTypes.AdminTaxRegionListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminTaxRegionListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminTaxRegionListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.taxRegion.list(query),
|
||||
queryKey: taxRegionsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateTaxRegion = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRegionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateTaxRegion
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.taxRegion.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.all })
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteTaxRegion = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminTaxRegionDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.taxRegion.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: taxRegionsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
// Invalidate all detail queries, as the deleted tax region may have been a sublevel region
|
||||
queryClient.invalidateQueries({ queryKey: taxRegionsQueryKeys.details() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const USERS_QUERY_KEY = "users" as const
|
||||
const usersQueryKeys = {
|
||||
...queryKeysFactory(USERS_QUERY_KEY),
|
||||
me: () => [USERS_QUERY_KEY, "me"],
|
||||
}
|
||||
|
||||
export const useMe = (
|
||||
query?: HttpTypes.AdminUserParams,
|
||||
options?: UseQueryOptions<
|
||||
HttpTypes.AdminUserResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUserResponse,
|
||||
QueryKey
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.user.me(query),
|
||||
queryKey: usersQueryKeys.me(),
|
||||
...options,
|
||||
})
|
||||
|
||||
return {
|
||||
...data,
|
||||
...rest,
|
||||
}
|
||||
}
|
||||
|
||||
export const useUser = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminUserParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminUserResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUserResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.user.retrieve(id, query),
|
||||
queryKey: usersQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useUsers = (
|
||||
query?: HttpTypes.AdminUserListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminUserListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUserListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.user.list(query),
|
||||
queryKey: usersQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateUser = (
|
||||
query?: HttpTypes.AdminUserParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminUserResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateUser,
|
||||
QueryKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.user.create(payload, query),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: usersQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateUser = (
|
||||
id: string,
|
||||
query?: HttpTypes.AdminUserParams,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminUserResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateUser,
|
||||
QueryKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.user.update(id, payload, query),
|
||||
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<
|
||||
HttpTypes.AdminUserDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.user.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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const WORKFLOW_EXECUTIONS_QUERY_KEY = "workflow_executions" as const
|
||||
export const workflowExecutionsQueryKeys = queryKeysFactory(
|
||||
WORKFLOW_EXECUTIONS_QUERY_KEY
|
||||
)
|
||||
|
||||
export const useWorkflowExecutions = (
|
||||
query?: HttpTypes.AdminGetWorkflowExecutionsParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminGetWorkflowExecutionsParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminWorkflowExecutionListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.workflowExecution.list(query),
|
||||
queryKey: workflowExecutionsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useWorkflowExecution = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminWorkflowExecutionResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminWorkflowExecutionResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.workflowExecution.retrieve(id),
|
||||
queryKey: workflowExecutionsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { PromotionDTO } from "@medusajs/types"
|
||||
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import {
|
||||
CodeCell,
|
||||
CodeHeader,
|
||||
} from "../../../components/table/table-cells/common/code-cell"
|
||||
import {
|
||||
TextCell,
|
||||
TextHeader,
|
||||
} from "../../../components/table/table-cells/common/text-cell"
|
||||
import { StatusCell } from "../../../components/table/table-cells/promotion/status-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<PromotionDTO>()
|
||||
|
||||
export const usePromotionTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.display({
|
||||
id: "code",
|
||||
header: () => <CodeHeader text={t("fields.code")} />,
|
||||
cell: ({ row }) => <CodeCell code={row.original.code!} />,
|
||||
}),
|
||||
|
||||
columnHelper.display({
|
||||
id: "method",
|
||||
header: () => <TextHeader text={t("promotions.fields.campaign")} />,
|
||||
cell: ({ row }) => {
|
||||
const text = row.original.is_automatic
|
||||
? "Automatic"
|
||||
: "Promotion Code"
|
||||
|
||||
return <TextCell text={text} />
|
||||
},
|
||||
}),
|
||||
|
||||
columnHelper.display({
|
||||
id: "status",
|
||||
header: () => <TextHeader text={t("fields.status")} />,
|
||||
cell: ({ row }) => <StatusCell promotion={row.original} />,
|
||||
}),
|
||||
],
|
||||
[]
|
||||
) as ColumnDef<PromotionDTO>[]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export * from "./use-campaign-table-columns"
|
||||
export * from "./use-collection-table-columns"
|
||||
export * from "./use-customer-group-table-columns"
|
||||
export * from "./use-customer-table-columns"
|
||||
export * from "./use-order-table-columns"
|
||||
export * from "./use-product-table-columns"
|
||||
export * from "./use-product-tag-table-columns"
|
||||
export * from "./use-product-type-table-columns"
|
||||
export * from "./use-region-table-columns"
|
||||
export * from "./use-return-reason-table-columns"
|
||||
export * from "./use-sales-channel-table-columns"
|
||||
export * from "./use-tax-rates-table-columns"
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
|
||||
import { AdminCampaign } from "@medusajs/types"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { DateCell } from "../../../components/table/table-cells/common/date-cell"
|
||||
import {
|
||||
TextCell,
|
||||
TextHeader,
|
||||
} from "../../../components/table/table-cells/common/text-cell"
|
||||
import {
|
||||
DescriptionCell,
|
||||
DescriptionHeader,
|
||||
} from "../../../components/table/table-cells/sales-channel/description-cell"
|
||||
import {
|
||||
NameCell,
|
||||
NameHeader,
|
||||
} from "../../../components/table/table-cells/sales-channel/name-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<AdminCampaign>()
|
||||
|
||||
export const useCampaignTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("name", {
|
||||
header: () => <NameHeader />,
|
||||
cell: ({ getValue }) => <NameCell name={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("description", {
|
||||
header: () => <DescriptionHeader />,
|
||||
cell: ({ getValue }) => <DescriptionCell description={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("campaign_identifier", {
|
||||
header: () => <TextHeader text={t("campaigns.fields.identifier")} />,
|
||||
cell: ({ getValue }) => {
|
||||
const value = getValue()
|
||||
return <TextCell text={value} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("starts_at", {
|
||||
header: () => <TextHeader text={t("campaigns.fields.start_date")} />,
|
||||
cell: ({ getValue }) => {
|
||||
const value = getValue()
|
||||
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
||||
const date = new Date(value)
|
||||
|
||||
return <DateCell date={date} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("ends_at", {
|
||||
header: () => <TextHeader text={t("campaigns.fields.end_date")} />,
|
||||
cell: ({ getValue }) => {
|
||||
const value = getValue()
|
||||
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
||||
const date = new Date(value)
|
||||
|
||||
return <DateCell date={date} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { TextCell } from "../../../components/table/table-cells/common/text-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminCollection>()
|
||||
|
||||
export const useCollectionTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("title", {
|
||||
header: t("fields.title"),
|
||||
cell: ({ getValue }) => <TextCell text={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("handle", {
|
||||
header: t("fields.handle"),
|
||||
cell: ({ getValue }) => <TextCell text={`/${getValue()}`} />,
|
||||
}),
|
||||
columnHelper.accessor("products", {
|
||||
header: t("fields.products"),
|
||||
cell: ({ getValue }) => {
|
||||
const count = getValue()?.length || undefined
|
||||
|
||||
return <TextCell text={count} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
|
||||
import { useTranslation } from "react-i18next"
|
||||
import {
|
||||
TextCell,
|
||||
TextHeader,
|
||||
} from "../../../components/table/table-cells/common/text-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminCustomerGroup>()
|
||||
|
||||
export const useCustomerGroupTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("name", {
|
||||
header: () => <TextHeader text={t("fields.name")} />,
|
||||
cell: ({ getValue }) => <TextCell text={getValue() || "-"} />,
|
||||
}),
|
||||
columnHelper.accessor("customers", {
|
||||
header: () => <TextHeader text={t("customers.domain")} />,
|
||||
cell: ({ getValue }) => {
|
||||
const count = getValue()?.length ?? 0
|
||||
|
||||
return <TextCell text={count} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
|
||||
import {
|
||||
EmailCell,
|
||||
EmailHeader,
|
||||
} from "../../../components/table/table-cells/common/email-cell"
|
||||
import {
|
||||
NameCell,
|
||||
NameHeader,
|
||||
} from "../../../components/table/table-cells/common/name-cell"
|
||||
import {
|
||||
AccountCell,
|
||||
AccountHeader,
|
||||
} from "../../../components/table/table-cells/customer/account-cell/account-cell"
|
||||
import {
|
||||
FirstSeenCell,
|
||||
FirstSeenHeader,
|
||||
} from "../../../components/table/table-cells/customer/first-seen-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>()
|
||||
|
||||
export const useCustomerTableColumns = () => {
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("email", {
|
||||
header: () => <EmailHeader />,
|
||||
cell: ({ getValue }) => <EmailCell email={getValue()} />,
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "name",
|
||||
header: () => <NameHeader />,
|
||||
cell: ({
|
||||
row: {
|
||||
original: { first_name, last_name },
|
||||
},
|
||||
}) => <NameCell firstName={first_name} lastName={last_name} />,
|
||||
}),
|
||||
columnHelper.accessor("has_account", {
|
||||
header: () => <AccountHeader />,
|
||||
cell: ({ getValue }) => <AccountCell hasAccount={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("created_at", {
|
||||
header: () => <FirstSeenHeader />,
|
||||
cell: ({ getValue }) => <FirstSeenCell createdAt={getValue()} />,
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import {
|
||||
TextCell,
|
||||
TextHeader,
|
||||
} from "../../../components/table/table-cells/common/text-cell"
|
||||
import { formatProvider } from "../../../lib/format-provider"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminFulfillmentProvider>()
|
||||
|
||||
export const useFulfillmentProviderTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("id", {
|
||||
header: () => <TextHeader text={"Provider"} />,
|
||||
cell: ({ getValue }) => <TextCell text={formatProvider(getValue())} />,
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import {
|
||||
ColumnDef,
|
||||
ColumnDefBase,
|
||||
createColumnHelper,
|
||||
} from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import {
|
||||
DateCell,
|
||||
DateHeader,
|
||||
} from "../../../components/table/table-cells/common/date-cell"
|
||||
import { CountryCell } from "../../../components/table/table-cells/order/country-cell"
|
||||
import {
|
||||
CustomerCell,
|
||||
CustomerHeader,
|
||||
} from "../../../components/table/table-cells/order/customer-cell"
|
||||
import {
|
||||
DisplayIdCell,
|
||||
DisplayIdHeader,
|
||||
} from "../../../components/table/table-cells/order/display-id-cell"
|
||||
import {
|
||||
FulfillmentStatusCell,
|
||||
FulfillmentStatusHeader,
|
||||
} from "../../../components/table/table-cells/order/fulfillment-status-cell"
|
||||
import {
|
||||
PaymentStatusCell,
|
||||
PaymentStatusHeader,
|
||||
} from "../../../components/table/table-cells/order/payment-status-cell"
|
||||
import {
|
||||
SalesChannelCell,
|
||||
SalesChannelHeader,
|
||||
} from "../../../components/table/table-cells/order/sales-channel-cell"
|
||||
import {
|
||||
TotalCell,
|
||||
TotalHeader,
|
||||
} from "../../../components/table/table-cells/order/total-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
// We have to use any here, as the type of Order is so complex that it lags the TS server
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminOrder>()
|
||||
|
||||
type UseOrderTableColumnsProps = {
|
||||
exclude?: string[]
|
||||
}
|
||||
|
||||
export const useOrderTableColumns = (props: UseOrderTableColumnsProps) => {
|
||||
const { exclude = [] } = props ?? {}
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("display_id", {
|
||||
header: () => <DisplayIdHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const id = getValue()
|
||||
|
||||
return <DisplayIdCell displayId={id!} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("created_at", {
|
||||
header: () => <DateHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const date = new Date(getValue())
|
||||
|
||||
return <DateCell date={date} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("customer", {
|
||||
header: () => <CustomerHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const customer = getValue()
|
||||
|
||||
return <CustomerCell customer={customer} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("sales_channel", {
|
||||
header: () => <SalesChannelHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const channel = getValue()
|
||||
|
||||
return <SalesChannelCell channel={channel} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("payment_status", {
|
||||
header: () => <PaymentStatusHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const status = getValue()
|
||||
|
||||
return <PaymentStatusCell status={status} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("fulfillment_status", {
|
||||
header: () => <FulfillmentStatusHeader />,
|
||||
cell: ({ getValue }) => {
|
||||
const status = getValue()
|
||||
|
||||
return <FulfillmentStatusCell status={status} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("total", {
|
||||
header: () => <TotalHeader />,
|
||||
cell: ({ getValue, row }) => {
|
||||
const total = getValue()
|
||||
const currencyCode = row.original.currency_code
|
||||
|
||||
return <TotalCell currencyCode={currencyCode} total={total} />
|
||||
},
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
const country = row.original.shipping_address?.country
|
||||
|
||||
return <CountryCell country={country} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
|
||||
const isAccessorColumnDef = (
|
||||
c: any
|
||||
): c is ColumnDef<HttpTypes.AdminOrder> & { accessorKey: string } => {
|
||||
return c.accessorKey !== undefined
|
||||
}
|
||||
|
||||
const isDisplayColumnDef = (
|
||||
c: any
|
||||
): c is ColumnDef<HttpTypes.AdminOrder> & { id: string } => {
|
||||
return c.id !== undefined
|
||||
}
|
||||
|
||||
const shouldExclude = <TDef extends ColumnDefBase<HttpTypes.AdminOrder, any>>(
|
||||
c: TDef
|
||||
) => {
|
||||
if (isAccessorColumnDef(c)) {
|
||||
return exclude.includes(c.accessorKey)
|
||||
} else if (isDisplayColumnDef(c)) {
|
||||
return exclude.includes(c.id)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return columns.filter(
|
||||
(c) => !shouldExclude(c)
|
||||
) as ColumnDef<HttpTypes.AdminOrder>[]
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
|
||||
import {
|
||||
CollectionCell,
|
||||
CollectionHeader,
|
||||
} from "../../../components/table/table-cells/product/collection-cell/collection-cell"
|
||||
import {
|
||||
ProductCell,
|
||||
ProductHeader,
|
||||
} from "../../../components/table/table-cells/product/product-cell"
|
||||
import {
|
||||
ProductStatusCell,
|
||||
ProductStatusHeader,
|
||||
} from "../../../components/table/table-cells/product/product-status-cell"
|
||||
import {
|
||||
SalesChannelHeader,
|
||||
SalesChannelsCell,
|
||||
} from "../../../components/table/table-cells/product/sales-channels-cell"
|
||||
import {
|
||||
VariantCell,
|
||||
VariantHeader,
|
||||
} from "../../../components/table/table-cells/product/variant-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminProduct>()
|
||||
|
||||
export const useProductTableColumns = () => {
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.display({
|
||||
id: "product",
|
||||
header: () => <ProductHeader />,
|
||||
cell: ({ row }) => <ProductCell product={row.original} />,
|
||||
}),
|
||||
columnHelper.accessor("collection", {
|
||||
header: () => <CollectionHeader />,
|
||||
cell: ({ row }) => (
|
||||
<CollectionCell collection={row.original.collection} />
|
||||
),
|
||||
}),
|
||||
columnHelper.accessor("sales_channels", {
|
||||
header: () => <SalesChannelHeader />,
|
||||
cell: ({ row }) => (
|
||||
<SalesChannelsCell salesChannels={row.original.sales_channels} />
|
||||
),
|
||||
}),
|
||||
columnHelper.accessor("variants", {
|
||||
header: () => <VariantHeader />,
|
||||
cell: ({ row }) => <VariantCell variants={row.original.variants} />,
|
||||
}),
|
||||
columnHelper.accessor("status", {
|
||||
header: () => <ProductStatusHeader />,
|
||||
cell: ({ row }) => <ProductStatusCell status={row.original.status} />,
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { DateCell } from "../../../components/table/table-cells/common/date-cell"
|
||||
import { TextCell } from "../../../components/table/table-cells/common/text-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminProductTag>()
|
||||
|
||||
export const useProductTagTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("value", {
|
||||
header: () => t("fields.value"),
|
||||
cell: ({ getValue }) => <TextCell text={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("created_at", {
|
||||
header: () => t("fields.createdAt"),
|
||||
|
||||
cell: ({ getValue }) => {
|
||||
return <DateCell date={getValue()} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("updated_at", {
|
||||
header: () => t("fields.updatedAt"),
|
||||
cell: ({ getValue }) => {
|
||||
return <DateCell date={getValue()} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { DateCell } from "../../../components/table/table-cells/common/date-cell"
|
||||
import { TextCell } from "../../../components/table/table-cells/common/text-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminProductType>()
|
||||
|
||||
export const useProductTypeTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("value", {
|
||||
header: () => t("fields.value"),
|
||||
cell: ({ getValue }) => <TextCell text={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("created_at", {
|
||||
header: () => t("fields.createdAt"),
|
||||
|
||||
cell: ({ getValue }) => {
|
||||
return <DateCell date={getValue()} />
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor("updated_at", {
|
||||
header: () => t("fields.updatedAt"),
|
||||
cell: ({ getValue }) => {
|
||||
return <DateCell date={getValue()} />
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
|
||||
import {
|
||||
CountriesCell,
|
||||
CountriesHeader,
|
||||
} from "../../../components/table/table-cells/region/countries-cell"
|
||||
import {
|
||||
PaymentProvidersCell,
|
||||
PaymentProvidersHeader,
|
||||
} from "../../../components/table/table-cells/region/payment-providers-cell"
|
||||
import {
|
||||
RegionCell,
|
||||
RegionHeader,
|
||||
} from "../../../components/table/table-cells/region/region-cell"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminRegion>()
|
||||
|
||||
export const useRegionTableColumns = () => {
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("name", {
|
||||
header: () => <RegionHeader />,
|
||||
cell: ({ getValue }) => <RegionCell name={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("countries", {
|
||||
header: () => <CountriesHeader />,
|
||||
cell: ({ getValue }) => <CountriesCell countries={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("payment_providers", {
|
||||
header: () => <PaymentProvidersHeader />,
|
||||
cell: ({ getValue }) => (
|
||||
<PaymentProvidersCell paymentProviders={getValue()} />
|
||||
),
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { Badge } from "@medusajs/ui"
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminReturnReason>()
|
||||
|
||||
export const useReturnReasonTableColumns = () => {
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("value", {
|
||||
cell: ({ getValue }) => <Badge size="2xsmall">{getValue()}</Badge>,
|
||||
}),
|
||||
columnHelper.accessor("label", {
|
||||
cell: ({ row }) => {
|
||||
const { label, description } = row.original
|
||||
return (
|
||||
<div className="flex h-full w-full flex-col justify-center py-4">
|
||||
<span className="truncate font-medium">{label}</span>
|
||||
<span className="truncate">
|
||||
{description ? description : "-"}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { StatusCell } from "../../../components/table/table-cells/common/status-cell"
|
||||
import { TextHeader } from "../../../components/table/table-cells/common/text-cell"
|
||||
import {
|
||||
DescriptionCell,
|
||||
DescriptionHeader,
|
||||
} from "../../../components/table/table-cells/sales-channel/description-cell"
|
||||
import {
|
||||
NameCell,
|
||||
NameHeader,
|
||||
} from "../../../components/table/table-cells/sales-channel/name-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<HttpTypes.AdminSalesChannel>()
|
||||
|
||||
export const useSalesChannelTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.accessor("name", {
|
||||
header: () => <NameHeader />,
|
||||
cell: ({ getValue }) => <NameCell name={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("description", {
|
||||
header: () => <DescriptionHeader />,
|
||||
cell: ({ getValue }) => <DescriptionCell description={getValue()} />,
|
||||
}),
|
||||
columnHelper.accessor("is_disabled", {
|
||||
header: () => <TextHeader text={t("fields.status")} />,
|
||||
cell: ({ getValue }) => {
|
||||
const value = getValue()
|
||||
return (
|
||||
<StatusCell color={value ? "grey" : "green"}>
|
||||
{value ? t("general.disabled") : t("general.enabled")}
|
||||
</StatusCell>
|
||||
)
|
||||
},
|
||||
}),
|
||||
],
|
||||
[t]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { createColumnHelper } from "@tanstack/react-table"
|
||||
import { useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { TaxRateResponse } from "@medusajs/types"
|
||||
import {
|
||||
TextCell,
|
||||
TextHeader,
|
||||
} from "../../../components/table/table-cells/common/text-cell"
|
||||
|
||||
import {
|
||||
TypeCell,
|
||||
TypeHeader,
|
||||
} from "../../../components/table/table-cells/taxes/type-cell"
|
||||
|
||||
const columnHelper = createColumnHelper<TaxRateResponse>()
|
||||
|
||||
export const useTaxRateTableColumns = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
columnHelper.display({
|
||||
id: "name",
|
||||
header: () => <TextHeader text={t("fields.name")} />,
|
||||
cell: ({ row }) => <TextCell text={row.original.name} />,
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "province",
|
||||
header: () => <TextHeader text={t("fields.province")} />,
|
||||
cell: ({ row }) => (
|
||||
<TextCell text={row.original.tax_region.province_code} />
|
||||
),
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "rate",
|
||||
header: () => <TextHeader text={t("fields.rate")} />,
|
||||
cell: ({ row }) => <TextCell text={`${row.original.rate} %`} />,
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "is_combinable",
|
||||
header: () => <TypeHeader text={t("fields.type")} />,
|
||||
cell: ({ row }) => (
|
||||
<TypeCell is_combinable={row.original.is_combinable} />
|
||||
),
|
||||
}),
|
||||
columnHelper.display({
|
||||
id: "code",
|
||||
header: () => <TextHeader text={t("fields.code")} />,
|
||||
cell: ({ row }) => <TextCell text={row.original.code || "-"} />,
|
||||
}),
|
||||
],
|
||||
[]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const usePromotionTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
let filters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at", type: "date" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at", type: "date" },
|
||||
]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export * from "./use-collection-table-filters"
|
||||
export * from "./use-customer-group-table-filters"
|
||||
export * from "./use-customer-table-filters"
|
||||
export * from "./use-date-table-filters"
|
||||
export * from "./use-order-table-filters"
|
||||
export * from "./use-product-table-filters"
|
||||
export * from "./use-product-tag-table-filters"
|
||||
export * from "./use-product-type-table-filters"
|
||||
export * from "./use-promotion-table-filters"
|
||||
export * from "./use-region-table-filters"
|
||||
export * from "./use-sales-channel-table-filters"
|
||||
export * from "./use-shipping-option-table-filters"
|
||||
export * from "./use-tax-rate-table-filters"
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useDateTableFilters } from "./use-date-table-filters"
|
||||
|
||||
export const useCollectionTableFilters = () => {
|
||||
const dateFilters = useDateTableFilters()
|
||||
|
||||
return dateFilters
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useCustomerGroupTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
let filters: Filter[] = []
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
filters = [...filters, ...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
import { useCustomerGroups } from "../../api/customer-groups"
|
||||
|
||||
const excludeableFields = ["groups"] as const
|
||||
|
||||
export const useCustomerTableFilters = (
|
||||
exclude?: (typeof excludeableFields)[number][]
|
||||
) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const isGroupsExcluded = exclude?.includes("groups")
|
||||
|
||||
const { customer_groups } = useCustomerGroups(
|
||||
{
|
||||
limit: 1000,
|
||||
},
|
||||
{
|
||||
enabled: !isGroupsExcluded,
|
||||
}
|
||||
)
|
||||
|
||||
let filters: Filter[] = []
|
||||
|
||||
if (customer_groups && !isGroupsExcluded) {
|
||||
const customerGroupFilter: Filter = {
|
||||
key: "groups",
|
||||
label: t("customers.groups.label"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: customer_groups.map((s) => ({
|
||||
label: s.name,
|
||||
value: s.id,
|
||||
})),
|
||||
}
|
||||
|
||||
filters = [...filters, customerGroupFilter]
|
||||
}
|
||||
|
||||
const hasAccountFilter: Filter = {
|
||||
key: "has_account",
|
||||
label: t("fields.account"),
|
||||
type: "select",
|
||||
options: [
|
||||
{
|
||||
label: t("customers.registered"),
|
||||
value: "true",
|
||||
},
|
||||
{
|
||||
label: t("customers.guest"),
|
||||
value: "false",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
filters = [...filters, hasAccountFilter, ...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useDateTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
return dateFilters
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import type { Filter } from "../../../components/table/data-table"
|
||||
import { useRegions } from "../../api/regions"
|
||||
import { useSalesChannels } from "../../api/sales-channels"
|
||||
|
||||
export const useOrderTableFilters = (): Filter[] => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const { regions } = useRegions({
|
||||
limit: 1000,
|
||||
fields: "id,name",
|
||||
})
|
||||
|
||||
const { sales_channels } = useSalesChannels({
|
||||
limit: 1000,
|
||||
fields: "id,name",
|
||||
})
|
||||
|
||||
let filters: Filter[] = []
|
||||
|
||||
if (regions) {
|
||||
const regionFilter: Filter = {
|
||||
key: "region_id",
|
||||
label: t("fields.region"),
|
||||
type: "select",
|
||||
options: regions.map((r) => ({
|
||||
label: r.name,
|
||||
value: r.id,
|
||||
})),
|
||||
multiple: true,
|
||||
searchable: true,
|
||||
}
|
||||
|
||||
filters = [...filters, regionFilter]
|
||||
}
|
||||
|
||||
if (sales_channels) {
|
||||
const salesChannelFilter: Filter = {
|
||||
key: "sales_channel_id",
|
||||
label: t("fields.salesChannel"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
searchable: true,
|
||||
options: sales_channels.map((s) => ({
|
||||
label: s.name,
|
||||
value: s.id,
|
||||
})),
|
||||
}
|
||||
|
||||
filters = [...filters, salesChannelFilter]
|
||||
}
|
||||
|
||||
const paymentStatusFilter: Filter = {
|
||||
key: "payment_status",
|
||||
label: t("orders.payment.statusLabel"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: [
|
||||
{
|
||||
label: t("orders.payment.status.notPaid"),
|
||||
value: "not_paid",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.awaiting"),
|
||||
value: "awaiting",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.captured"),
|
||||
value: "captured",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.refunded"),
|
||||
value: "refunded",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.partiallyRefunded"),
|
||||
value: "partially_refunded",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.canceled"),
|
||||
value: "canceled",
|
||||
},
|
||||
{
|
||||
label: t("orders.payment.status.requiresAction"),
|
||||
value: "requires_action",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const fulfillmentStatusFilter: Filter = {
|
||||
key: "fulfillment_status",
|
||||
label: t("orders.fulfillment.statusLabel"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: [
|
||||
{
|
||||
label: t("orders.fulfillment.status.notFulfilled"),
|
||||
value: "not_fulfilled",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.fulfilled"),
|
||||
value: "fulfilled",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.partiallyFulfilled"),
|
||||
value: "partially_fulfilled",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.returned"),
|
||||
value: "returned",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.partiallyReturned"),
|
||||
value: "partially_returned",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.shipped"),
|
||||
value: "shipped",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.partiallyShipped"),
|
||||
value: "partially_shipped",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.canceled"),
|
||||
value: "canceled",
|
||||
},
|
||||
{
|
||||
label: t("orders.fulfillment.status.requiresAction"),
|
||||
value: "requires_action",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: "Created At", key: "created_at" },
|
||||
{ label: "Updated At", key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
filters = [
|
||||
...filters,
|
||||
// TODO: enable when Payment, Fulfillments <> Orders are linked
|
||||
// paymentStatusFilter,
|
||||
// fulfillmentStatusFilter,
|
||||
...dateFilters,
|
||||
]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
import { useProductTags } from "../../api"
|
||||
import { useProductTypes } from "../../api/product-types"
|
||||
import { useSalesChannels } from "../../api/sales-channels"
|
||||
|
||||
const excludeableFields = [
|
||||
"sales_channel_id",
|
||||
"collections",
|
||||
"categories",
|
||||
"product_types",
|
||||
"product_tags",
|
||||
] as const
|
||||
|
||||
export const useProductTableFilters = (
|
||||
exclude?: (typeof excludeableFields)[number][]
|
||||
) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const isProductTypeExcluded = exclude?.includes("product_types")
|
||||
|
||||
const { product_types } = useProductTypes(
|
||||
{
|
||||
limit: 1000,
|
||||
offset: 0,
|
||||
},
|
||||
{
|
||||
enabled: !isProductTypeExcluded,
|
||||
}
|
||||
)
|
||||
|
||||
const isProductTagExcluded = exclude?.includes("product_tags")
|
||||
|
||||
const { product_tags } = useProductTags({
|
||||
limit: 1000,
|
||||
offset: 0,
|
||||
})
|
||||
|
||||
// const { product_tags } = useAdminProductTags({
|
||||
// limit: 1000,
|
||||
// offset: 0,
|
||||
// })
|
||||
|
||||
const isSalesChannelExcluded = exclude?.includes("sales_channel_id")
|
||||
|
||||
const { sales_channels } = useSalesChannels(
|
||||
{
|
||||
limit: 1000,
|
||||
fields: "id,name",
|
||||
},
|
||||
{
|
||||
enabled: !isSalesChannelExcluded,
|
||||
}
|
||||
)
|
||||
|
||||
const isCategoryExcluded = exclude?.includes("categories")
|
||||
|
||||
// const { product_categories } = useAdminProductCategories({
|
||||
// limit: 1000,
|
||||
// offset: 0,
|
||||
// fields: "id,name",
|
||||
// expand: "",
|
||||
// }, {
|
||||
// enabled: !isCategoryExcluded,
|
||||
// })
|
||||
|
||||
const isCollectionExcluded = exclude?.includes("collections")
|
||||
|
||||
// const { collections } = useAdminCollections(
|
||||
// {
|
||||
// limit: 1000,
|
||||
// offset: 0,
|
||||
// },
|
||||
// {
|
||||
// enabled: !isCollectionExcluded,
|
||||
// }
|
||||
// )
|
||||
|
||||
let filters: Filter[] = []
|
||||
|
||||
if (product_types && !isProductTypeExcluded) {
|
||||
const typeFilter: Filter = {
|
||||
key: "type_id",
|
||||
label: t("fields.type"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: product_types.map((t) => ({
|
||||
label: t.value,
|
||||
value: t.id,
|
||||
})),
|
||||
}
|
||||
|
||||
filters = [...filters, typeFilter]
|
||||
}
|
||||
|
||||
if (product_tags && !isProductTagExcluded) {
|
||||
const tagFilter: Filter = {
|
||||
key: "tag_id",
|
||||
label: t("fields.tag"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: product_tags.map((t) => ({
|
||||
label: t.value,
|
||||
value: t.id,
|
||||
})),
|
||||
}
|
||||
|
||||
filters = [...filters, tagFilter]
|
||||
}
|
||||
|
||||
if (sales_channels) {
|
||||
const salesChannelFilter: Filter = {
|
||||
key: "sales_channel_id",
|
||||
label: t("fields.salesChannel"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: sales_channels.map((s) => ({
|
||||
label: s.name,
|
||||
value: s.id,
|
||||
})),
|
||||
}
|
||||
|
||||
filters = [...filters, salesChannelFilter]
|
||||
}
|
||||
|
||||
// if (product_categories) {
|
||||
// const categoryFilter: Filter = {
|
||||
// key: "category_id",
|
||||
// label: t("fields.category"),
|
||||
// type: "select",
|
||||
// multiple: true,
|
||||
// options: product_categories.map((c) => ({
|
||||
// label: c.name,
|
||||
// value: c.id,
|
||||
// })),
|
||||
// }
|
||||
|
||||
// filters = [...filters, categoryFilter]
|
||||
// }
|
||||
|
||||
// if (collections) {
|
||||
// const collectionFilter: Filter = {
|
||||
// key: "collection_id",
|
||||
// label: t("fields.collection"),
|
||||
// type: "select",
|
||||
// multiple: true,
|
||||
// options: collections.map((c) => ({
|
||||
// label: c.title,
|
||||
// value: c.id,
|
||||
// })),
|
||||
// }
|
||||
|
||||
// filters = [...filters, collectionFilter]
|
||||
// }
|
||||
|
||||
// const giftCardFilter: Filter = {
|
||||
// key: "is_giftcard",
|
||||
// label: t("fields.giftCard"),
|
||||
// type: "select",
|
||||
// options: [
|
||||
// {
|
||||
// label: t("fields.true"),
|
||||
// value: "true",
|
||||
// },
|
||||
// {
|
||||
// label: t("fields.false"),
|
||||
// value: "false",
|
||||
// },
|
||||
// ],
|
||||
// }
|
||||
|
||||
const statusFilter: Filter = {
|
||||
key: "status",
|
||||
label: t("fields.status"),
|
||||
type: "select",
|
||||
multiple: true,
|
||||
options: [
|
||||
{
|
||||
label: t("products.productStatus.draft"),
|
||||
value: "draft",
|
||||
},
|
||||
{
|
||||
label: t("products.productStatus.proposed"),
|
||||
value: "proposed",
|
||||
},
|
||||
{
|
||||
label: t("products.productStatus.published"),
|
||||
value: "published",
|
||||
},
|
||||
{
|
||||
label: t("products.productStatus.rejected"),
|
||||
value: "rejected",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
filters = [...filters, statusFilter, ...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useDateTableFilters } from "./use-date-table-filters"
|
||||
|
||||
export const useProductTagTableFilters = () => {
|
||||
const dateFilters = useDateTableFilters()
|
||||
|
||||
return dateFilters
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useDateTableFilters } from "./use-date-table-filters"
|
||||
|
||||
export const useProductTypeTableFilters = () => {
|
||||
const dateFilters = useDateTableFilters()
|
||||
|
||||
return dateFilters
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const usePromotionTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
let filters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at", type: "date" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at", type: "date" },
|
||||
]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useRegionTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
const filters = [...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useSalesChannelTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
return dateFilters
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useShippingOptionTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const isReturnFilter: Filter = {
|
||||
key: "is_return",
|
||||
label: t("fields.type"),
|
||||
type: "select",
|
||||
options: [
|
||||
{ label: t("regions.return"), value: "true" },
|
||||
{ label: t("regions.outbound"), value: "false" },
|
||||
],
|
||||
}
|
||||
|
||||
const isAdminFilter: Filter = {
|
||||
key: "admin_only",
|
||||
label: t("fields.availability"),
|
||||
type: "select",
|
||||
options: [
|
||||
{ label: t("general.admin"), value: "true" },
|
||||
{ label: t("general.store"), value: "false" },
|
||||
],
|
||||
}
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
const filters = [isReturnFilter, isAdminFilter, ...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Filter } from "../../../components/table/data-table"
|
||||
|
||||
export const useTaxRateTableFilters = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const dateFilters: Filter[] = [
|
||||
{ label: t("fields.createdAt"), key: "created_at" },
|
||||
{ label: t("fields.updatedAt"), key: "updated_at" },
|
||||
].map((f) => ({
|
||||
key: f.key,
|
||||
label: f.label,
|
||||
type: "date",
|
||||
}))
|
||||
|
||||
const filters = [...dateFilters]
|
||||
|
||||
return filters
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UsePromotionTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const usePromotionTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UsePromotionTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, created_at, updated_at } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminGetPromotionsParams = {
|
||||
limit: pageSize,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
export * from "./use-campaign-table-query"
|
||||
export * from "./use-collection-table-query"
|
||||
export * from "./use-customer-group-table-query"
|
||||
export * from "./use-customer-table-query"
|
||||
export * from "./use-order-table-query"
|
||||
export * from "./use-product-table-query"
|
||||
export * from "./use-product-tag-table-query"
|
||||
export * from "./use-product-type-table-query"
|
||||
export * from "./use-region-table-query"
|
||||
export * from "./use-return-reason-table-query"
|
||||
export * from "./use-sales-channel-table-query"
|
||||
export * from "./use-shipping-option-table-query"
|
||||
export * from "./use-tax-rate-table-query"
|
||||
export * from "./use-tax-region-table-query"
|
||||
export * from "./use-user-invite-table-query"
|
||||
@@ -0,0 +1,31 @@
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseCampaignTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useCampaignTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseCampaignTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
const searchParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseCollectionTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useCollectionTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseCollectionTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, created_at, updated_at, q, order } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminCollectionListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseCustomerGroupTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useCustomerGroupTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseCustomerGroupTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "has_account", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, created_at, updated_at, q, order } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminGetCustomerGroupsParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseCustomerTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useCustomerTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseCustomerTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
[
|
||||
"offset",
|
||||
"q",
|
||||
"has_account",
|
||||
"groups",
|
||||
"order",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, groups, created_at, updated_at, has_account, q, order } =
|
||||
queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminCustomerFilters = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
groups: groups?.split(","),
|
||||
has_account: has_account ? has_account === "true" : undefined,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseFulfillmentProviderTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useFulfillmentProvidersTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseFulfillmentProviderTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "stock_location_id"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, stock_location_id } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminFulfillmentProviderListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
stock_location_id,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseOrderTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useOrderTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseOrderTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
[
|
||||
"offset",
|
||||
"q",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"region_id",
|
||||
"sales_channel_id",
|
||||
"payment_status",
|
||||
"fulfillment_status",
|
||||
"order",
|
||||
],
|
||||
prefix
|
||||
)
|
||||
|
||||
const {
|
||||
offset,
|
||||
sales_channel_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
fulfillment_status,
|
||||
payment_status,
|
||||
region_id,
|
||||
q,
|
||||
order,
|
||||
} = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminOrderFilters = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
sales_channel_id: sales_channel_id?.split(","),
|
||||
fulfillment_status: fulfillment_status?.split(","),
|
||||
payment_status: payment_status?.split(","),
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
region_id: region_id?.split(","),
|
||||
order: order ? order : "-display_id",
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseProductTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useProductTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseProductTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
[
|
||||
"offset",
|
||||
"order",
|
||||
"q",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"sales_channel_id",
|
||||
"category_id",
|
||||
"collection_id",
|
||||
"is_giftcard",
|
||||
"tag_id",
|
||||
"type_id",
|
||||
"status",
|
||||
"id",
|
||||
],
|
||||
prefix
|
||||
)
|
||||
|
||||
const {
|
||||
offset,
|
||||
sales_channel_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
category_id,
|
||||
collection_id,
|
||||
tag_id,
|
||||
type_id,
|
||||
is_giftcard,
|
||||
status,
|
||||
order,
|
||||
q,
|
||||
} = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminProductListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
sales_channel_id: sales_channel_id?.split(","),
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
category_id: category_id?.split(","),
|
||||
collection_id: collection_id?.split(","),
|
||||
is_giftcard: is_giftcard ? is_giftcard === "true" : undefined,
|
||||
order: order,
|
||||
tag_id: tag_id ? tag_id.split(",") : undefined,
|
||||
type_id: type_id?.split(","),
|
||||
status: status?.split(",") as HttpTypes.AdminProductStatus[],
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseProductTagTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useProductTagTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseProductTagTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
const searchParams: HttpTypes.AdminProductTagListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseProductTypeTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useProductTypeTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseProductTypeTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
const searchParams: HttpTypes.AdminProductTypeListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { FindParams, HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseRegionTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useRegionTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseRegionTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
|
||||
const searchParams: FindParams & HttpTypes.AdminRegionFilters = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseReturnReasonTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useReturnReasonTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseReturnReasonTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
const searchParams: HttpTypes.AdminReturnReasonListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseSalesChannelTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useSalesChannelTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseSalesChannelTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at", "is_disabled"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, created_at, updated_at, is_disabled, ...rest } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminSalesChannelListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
is_disabled:
|
||||
is_disabled === "true"
|
||||
? true
|
||||
: is_disabled === "false"
|
||||
? false
|
||||
: undefined,
|
||||
...rest,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseShippingOptionTableQueryProps = {
|
||||
regionId: string
|
||||
isReturn?: boolean
|
||||
pageSize?: number
|
||||
prefix?: string
|
||||
}
|
||||
|
||||
export const useShippingOptionTableQuery = ({
|
||||
regionId,
|
||||
pageSize = 10,
|
||||
prefix,
|
||||
}: UseShippingOptionTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
[
|
||||
"offset",
|
||||
"q",
|
||||
"order",
|
||||
"admin_only",
|
||||
"is_return",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, order, q, admin_only, is_return, created_at, updated_at } =
|
||||
queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminShippingOptionListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
// TODO: We don't allow region_id in the API yet
|
||||
// region_id: regionId,
|
||||
is_return: is_return ? is_return === "true" : undefined,
|
||||
admin_only: admin_only ? admin_only === "true" : undefined,
|
||||
q,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseTaxRateTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useTaxRateTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseTaxRateTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
|
||||
const searchParams: HttpTypes.AdminTaxRateListParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseTaxRegionTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useTaxRegionTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseTaxRegionTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, q, order, created_at, updated_at } = queryObject
|
||||
|
||||
const searchParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { useQueryParams } from "../../use-query-params"
|
||||
|
||||
type UseUserInviteTableQueryProps = {
|
||||
prefix?: string
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export const useUserInviteTableQuery = ({
|
||||
prefix,
|
||||
pageSize = 20,
|
||||
}: UseUserInviteTableQueryProps) => {
|
||||
const queryObject = useQueryParams(
|
||||
["offset", "q", "order", "created_at", "updated_at"],
|
||||
prefix
|
||||
)
|
||||
|
||||
const { offset, created_at, updated_at, q, order } = queryObject
|
||||
|
||||
const searchParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
order,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
|
||||
q,
|
||||
}
|
||||
|
||||
return {
|
||||
searchParams,
|
||||
raw: queryObject,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { MutableRefObject, Ref, RefCallback } from "react"
|
||||
|
||||
// Utility function to set multiple refs
|
||||
function setRef<T>(ref: Ref<T> | undefined, value: T) {
|
||||
if (typeof ref === "function") {
|
||||
ref(value)
|
||||
} else if (ref && "current" in ref) {
|
||||
;(ref as MutableRefObject<T>).current = value
|
||||
}
|
||||
}
|
||||
|
||||
// Combining multiple refs into one
|
||||
export const useCombinedRefs = <T,>(
|
||||
...refs: (Ref<T> | undefined)[]
|
||||
): RefCallback<T> => {
|
||||
return (value: T) => {
|
||||
refs.forEach((ref) => setRef(ref, value))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import {
|
||||
QueryKey,
|
||||
keepPreviousData,
|
||||
useInfiniteQuery,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { useDebouncedSearch } from "./use-debounced-search"
|
||||
|
||||
type ComboboxExternalData = {
|
||||
offset: number
|
||||
limit: number
|
||||
count: number
|
||||
}
|
||||
|
||||
type ComboboxQueryParams = {
|
||||
q?: string
|
||||
offset?: number
|
||||
limit?: number
|
||||
}
|
||||
|
||||
export const useComboboxData = <
|
||||
TResponse extends ComboboxExternalData,
|
||||
TParams extends ComboboxQueryParams
|
||||
>({
|
||||
queryKey,
|
||||
queryFn,
|
||||
getOptions,
|
||||
defaultValue,
|
||||
defaultValueKey,
|
||||
pageSize = 10,
|
||||
}: {
|
||||
queryKey: QueryKey
|
||||
queryFn: (params: TParams) => Promise<TResponse>
|
||||
getOptions: (data: TResponse) => { label: string; value: string }[]
|
||||
defaultValueKey?: keyof TParams
|
||||
defaultValue?: string | string[]
|
||||
pageSize?: number
|
||||
}) => {
|
||||
const { searchValue, onSearchValueChange, query } = useDebouncedSearch()
|
||||
|
||||
const queryInitialDataBy = defaultValueKey || "id"
|
||||
const { data: initialData } = useQuery({
|
||||
queryKey: queryKey,
|
||||
queryFn: async () => {
|
||||
return queryFn({
|
||||
[queryInitialDataBy]: defaultValue,
|
||||
limit: Array.isArray(defaultValue) ? defaultValue.length : 1,
|
||||
} as TParams)
|
||||
},
|
||||
enabled: !!defaultValue,
|
||||
})
|
||||
|
||||
const { data, ...rest } = useInfiniteQuery({
|
||||
queryKey: [...queryKey, query],
|
||||
queryFn: async ({ pageParam = 0 }) => {
|
||||
return await queryFn({
|
||||
q: query,
|
||||
limit: pageSize,
|
||||
offset: pageParam,
|
||||
} as TParams)
|
||||
},
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (lastPage) => {
|
||||
const moreItemsExist = lastPage.count > lastPage.offset + lastPage.limit
|
||||
return moreItemsExist ? lastPage.offset + lastPage.limit : undefined
|
||||
},
|
||||
placeholderData: keepPreviousData,
|
||||
})
|
||||
|
||||
const options = data?.pages.flatMap((page) => getOptions(page)) ?? []
|
||||
const defaultOptions = initialData ? getOptions(initialData) : []
|
||||
|
||||
/**
|
||||
* If there are no options and the query is empty, then the combobox should be disabled,
|
||||
* as there is no data to search for.
|
||||
*/
|
||||
const disabled = !rest.isPending && !options.length && !searchValue
|
||||
|
||||
// // make sure that the default value is included in the option, if its not in options already
|
||||
if (
|
||||
defaultValue &&
|
||||
defaultOptions.length &&
|
||||
!options.find((o) => o.value === defaultValue)
|
||||
) {
|
||||
options.unshift(defaultOptions[0])
|
||||
}
|
||||
|
||||
return {
|
||||
options,
|
||||
searchValue,
|
||||
onSearchValueChange,
|
||||
disabled,
|
||||
...rest,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { useCallback, useState } from "react"
|
||||
|
||||
/**
|
||||
* Base interface for a command that can be managed
|
||||
* by the `useCommandHistory` hook.
|
||||
*/
|
||||
export interface Command {
|
||||
execute: () => void
|
||||
undo: () => void
|
||||
redo: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to manage a history of commands that can be undone, redone, and executed.
|
||||
*/
|
||||
export const useCommandHistory = (maxHistory = 20) => {
|
||||
const [past, setPast] = useState<Command[]>([])
|
||||
const [future, setFuture] = useState<Command[]>([])
|
||||
|
||||
const canUndo = past.length > 0
|
||||
const canRedo = future.length > 0
|
||||
|
||||
const undo = useCallback(() => {
|
||||
if (!canUndo) {
|
||||
return
|
||||
}
|
||||
|
||||
const previous = past[past.length - 1]
|
||||
const newPast = past.slice(0, past.length - 1)
|
||||
|
||||
previous.undo()
|
||||
|
||||
setPast(newPast)
|
||||
setFuture([previous, ...future.slice(0, maxHistory - 1)])
|
||||
}, [canUndo, future, past, maxHistory])
|
||||
|
||||
const redo = useCallback(() => {
|
||||
if (!canRedo) {
|
||||
return
|
||||
}
|
||||
|
||||
const next = future[0]
|
||||
const newFuture = future.slice(1)
|
||||
|
||||
next.redo()
|
||||
|
||||
setPast([...past, next].slice(0, maxHistory - 1))
|
||||
setFuture(newFuture)
|
||||
}, [canRedo, future, past, maxHistory])
|
||||
|
||||
const execute = useCallback(
|
||||
(command: Command) => {
|
||||
command.execute()
|
||||
setPast((past) => [...past, command].slice(0, maxHistory - 1))
|
||||
setFuture([])
|
||||
},
|
||||
[maxHistory]
|
||||
)
|
||||
|
||||
return {
|
||||
undo,
|
||||
redo,
|
||||
execute,
|
||||
canUndo,
|
||||
canRedo,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import {
|
||||
ColumnDef,
|
||||
OnChangeFn,
|
||||
PaginationState,
|
||||
Row,
|
||||
RowSelectionState,
|
||||
getCoreRowModel,
|
||||
getExpandedRowModel,
|
||||
getPaginationRowModel,
|
||||
useReactTable,
|
||||
} from "@tanstack/react-table"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { useSearchParams } from "react-router-dom"
|
||||
|
||||
type UseDataTableProps<TData> = {
|
||||
data?: TData[]
|
||||
columns: ColumnDef<TData, any>[]
|
||||
count?: number
|
||||
pageSize?: number
|
||||
enableRowSelection?: boolean | ((row: Row<TData>) => boolean)
|
||||
rowSelection?: {
|
||||
state: RowSelectionState
|
||||
updater: OnChangeFn<RowSelectionState>
|
||||
}
|
||||
enablePagination?: boolean
|
||||
enableExpandableRows?: boolean
|
||||
getRowId?: (original: TData, index: number) => string
|
||||
getSubRows?: (original: TData) => TData[]
|
||||
meta?: Record<string, unknown>
|
||||
prefix?: string
|
||||
}
|
||||
|
||||
export const useDataTable = <TData,>({
|
||||
data = [],
|
||||
columns,
|
||||
count = 0,
|
||||
pageSize: _pageSize = 20,
|
||||
enablePagination = true,
|
||||
enableRowSelection = false,
|
||||
enableExpandableRows = false,
|
||||
rowSelection: _rowSelection,
|
||||
getSubRows,
|
||||
getRowId,
|
||||
meta,
|
||||
prefix,
|
||||
}: UseDataTableProps<TData>) => {
|
||||
const [searchParams, setSearchParams] = useSearchParams()
|
||||
const offsetKey = `${prefix ? `${prefix}_` : ""}offset`
|
||||
const offset = searchParams.get(offsetKey)
|
||||
|
||||
const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
|
||||
pageIndex: offset ? Math.ceil(Number(offset) / _pageSize) : 0,
|
||||
pageSize: _pageSize,
|
||||
})
|
||||
const pagination = useMemo(
|
||||
() => ({
|
||||
pageIndex,
|
||||
pageSize,
|
||||
}),
|
||||
[pageIndex, pageSize]
|
||||
)
|
||||
const [localRowSelection, setLocalRowSelection] = useState({})
|
||||
const rowSelection = _rowSelection?.state ?? localRowSelection
|
||||
const setRowSelection = _rowSelection?.updater ?? setLocalRowSelection
|
||||
|
||||
useEffect(() => {
|
||||
if (!enablePagination) {
|
||||
return
|
||||
}
|
||||
|
||||
const index = offset ? Math.ceil(Number(offset) / _pageSize) : 0
|
||||
|
||||
if (index === pageIndex) {
|
||||
return
|
||||
}
|
||||
|
||||
setPagination((prev) => ({
|
||||
...prev,
|
||||
pageIndex: index,
|
||||
}))
|
||||
}, [offset, enablePagination, _pageSize, pageIndex])
|
||||
|
||||
const onPaginationChange = (
|
||||
updater: (old: PaginationState) => PaginationState
|
||||
) => {
|
||||
const state = updater(pagination)
|
||||
const { pageIndex, pageSize } = state
|
||||
|
||||
setSearchParams((prev) => {
|
||||
if (!pageIndex) {
|
||||
prev.delete(offsetKey)
|
||||
return prev
|
||||
}
|
||||
|
||||
const newSearch = new URLSearchParams(prev)
|
||||
newSearch.set(offsetKey, String(pageIndex * pageSize))
|
||||
|
||||
return newSearch
|
||||
})
|
||||
|
||||
setPagination(state)
|
||||
return state
|
||||
}
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
state: {
|
||||
rowSelection: rowSelection, // We always pass a selection state to the table even if it's not enabled
|
||||
pagination: enablePagination ? pagination : undefined,
|
||||
},
|
||||
pageCount: Math.ceil((count ?? 0) / pageSize),
|
||||
enableRowSelection,
|
||||
getRowId,
|
||||
getSubRows,
|
||||
onRowSelectionChange: enableRowSelection ? setRowSelection : undefined,
|
||||
onPaginationChange: enablePagination
|
||||
? (onPaginationChange as OnChangeFn<PaginationState>)
|
||||
: undefined,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getPaginationRowModel: enablePagination
|
||||
? getPaginationRowModel()
|
||||
: undefined,
|
||||
getExpandedRowModel: enableExpandableRows
|
||||
? getExpandedRowModel()
|
||||
: undefined,
|
||||
manualPagination: enablePagination ? true : undefined,
|
||||
meta,
|
||||
})
|
||||
|
||||
return { table }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { format, formatDistance, sub } from "date-fns"
|
||||
import { enUS } from "date-fns/locale"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { languages } from "../i18n/languages"
|
||||
|
||||
// TODO: We rely on the current language to determine the date locale. This is not ideal, as we use en-US for the english translation.
|
||||
// We either need to also have an en-GB translation or we need to separate the date locale from the translation language.
|
||||
export const useDate = () => {
|
||||
const { i18n } = useTranslation()
|
||||
|
||||
const locale =
|
||||
languages.find((l) => l.code === i18n.language)?.date_locale || enUS
|
||||
|
||||
const getFullDate = ({
|
||||
date,
|
||||
includeTime = false,
|
||||
}: {
|
||||
date: string | Date
|
||||
includeTime?: boolean
|
||||
}) => {
|
||||
const ensuredDate = new Date(date)
|
||||
|
||||
if (isNaN(ensuredDate.getTime())) {
|
||||
return ""
|
||||
}
|
||||
|
||||
const timeFormat = includeTime ? "p" : ""
|
||||
|
||||
return format(ensuredDate, `PP ${timeFormat}`, {
|
||||
locale,
|
||||
})
|
||||
}
|
||||
|
||||
function getRelativeDate(date: string | Date): string {
|
||||
const now = new Date()
|
||||
|
||||
return formatDistance(sub(new Date(date), { minutes: 0 }), now, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
getFullDate,
|
||||
getRelativeDate,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import debounce from "lodash/debounce"
|
||||
import { useCallback, useEffect, useState } from "react"
|
||||
|
||||
/**
|
||||
* Hook for debouncing search input
|
||||
* @returns searchValue, onSearchValueChange, query
|
||||
*/
|
||||
export const useDebouncedSearch = () => {
|
||||
const [searchValue, onSearchValueChange] = useState("")
|
||||
const [debouncedQuery, setDebouncedQuery] = useState("")
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
const debouncedUpdate = useCallback(
|
||||
debounce((query: string) => setDebouncedQuery(query), 300),
|
||||
[]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
debouncedUpdate(searchValue)
|
||||
|
||||
return () => debouncedUpdate.cancel()
|
||||
}, [searchValue, debouncedUpdate])
|
||||
|
||||
return {
|
||||
searchValue,
|
||||
onSearchValueChange,
|
||||
query: debouncedQuery || undefined,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { useRef, useState } from "react"
|
||||
|
||||
export const useHandleTableScroll = () => {
|
||||
const tableContainerRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
// Listen for if the table container that has overflow-y: auto is scrolled, and if true set some state
|
||||
const [isScrolled, setIsScrolled] = useState(false)
|
||||
|
||||
const handleScroll = () => {
|
||||
if (tableContainerRef.current) {
|
||||
setIsScrolled(
|
||||
tableContainerRef.current.scrollTop > 0 &&
|
||||
tableContainerRef.current.scrollTop <
|
||||
tableContainerRef.current.scrollHeight
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
tableContainerRef,
|
||||
isScrolled,
|
||||
handleScroll,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export const useMediaQuery = (query: string) => {
|
||||
const mediaQuery = window.matchMedia(query)
|
||||
const [matches, setMatches] = useState(mediaQuery.matches)
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (e: MediaQueryListEvent) => setMatches(e.matches)
|
||||
mediaQuery.addEventListener("change", handler)
|
||||
return () => mediaQuery.removeEventListener("change", handler)
|
||||
}, [mediaQuery])
|
||||
|
||||
return matches
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { useSearchParams } from "react-router-dom"
|
||||
|
||||
type QueryParams<T extends string> = {
|
||||
[key in T]: string | undefined
|
||||
}
|
||||
|
||||
export function useQueryParams<T extends string>(
|
||||
keys: T[],
|
||||
prefix?: string
|
||||
): QueryParams<T> {
|
||||
const [params] = useSearchParams()
|
||||
|
||||
// Use a type assertion to initialize the result
|
||||
const result = {} as QueryParams<T>
|
||||
|
||||
keys.forEach((key) => {
|
||||
const prefixedKey = prefix ? `${prefix}_${key}` : key
|
||||
const value = params.get(prefixedKey) || undefined
|
||||
|
||||
result[key] = value
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user