feat(dashboard): Initial pricing domain (#6996)
**What** - Sets up the initial work for Pricing domain - Fixes Store domain **Todo in follow up PR** - Translations - Add status when creating the Price List and allow updating it - Improve DataGrid component - Add missing functionality once backend support is added (customer_groups, region prices and update prices) CLOSES CORE-1931
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import {
|
||||
AdminCustomerGroupListResponse,
|
||||
AdminCustomerGroupResponse,
|
||||
} from "@medusajs/types"
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import {
|
||||
AdminCustomerGroupResponse,
|
||||
AdminCustomerGroupListResponse,
|
||||
} from "@medusajs/types"
|
||||
|
||||
const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
|
||||
const customerGroupsQueryKeys = queryKeysFactory(CUSTOMER_GROUPS_QUERY_KEY)
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/medusa"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import {
|
||||
AddPriceListPricesReq,
|
||||
CreatePriceListReq,
|
||||
DeletePriceListPricesReq,
|
||||
UpdatePriceListReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
PriceListDeleteRes,
|
||||
PriceListListRes,
|
||||
PriceListRes,
|
||||
} from "../../types/api-responses"
|
||||
|
||||
const PRICE_LISTS_QUERY_KEY = "price-lists" as const
|
||||
export const priceListsQueryKeys = queryKeysFactory(PRICE_LISTS_QUERY_KEY)
|
||||
|
||||
export const usePriceList = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<PriceListRes, Error, PriceListRes, QueryKey>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.priceLists.retrieve(id, query),
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const usePriceLists = (
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<PriceListListRes, Error, PriceListListRes, QueryKey>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.priceLists.list(query),
|
||||
queryKey: priceListsQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreatePriceList = (
|
||||
options?: UseMutationOptions<PriceListRes, Error, CreatePriceListReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.priceLists.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.list() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdatePriceList = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<PriceListRes, Error, UpdatePriceListReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.priceLists.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.list() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeletePriceList = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<PriceListDeleteRes, Error, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.priceLists.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.list() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePriceListAddPrices = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<PriceListRes, Error, AddPriceListPricesReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.priceLists.addPrices(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const usePriceListRemovePrices = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<PriceListRes, Error, DeletePriceListPricesReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.priceLists.removePrices(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: priceListsQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: priceListsQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user