* chore(medusa): strict zod versions in workspace * feat(dashboard): add campaign create to promotion UI * wip * fix(medusa): Missing middlewares export (#7289) * fix(docblock-generator): fix how type names created from Zod objects are inferred (#7292) * feat(api-ref): show schema of a tag (#7297) * feat: Add support for sendgrid and logger notification providers (#7290) * feat: Add support for sendgrid and logger notification providers * fix: changes based on PR review * chore: add action to automatically label docs (#7284) * chore: add action to automatically label docs * removes the paths param * docs: preparations for preview (#7267) * configured base paths + added development banner * fix typelist site url * added navbar and sidebar badges * configure algolia filters * remove AI assistant * remove unused imports * change navbar text and badge * lint fixes * fix build error * add to api reference rewrites * fix build error * fix build errors in user-guide * fix feedback component * add parent title to pagination * added breadcrumbs component * remove user-guide links * resolve todos * fix details about authentication * change documentation title * lint content * chore: fix bug with form reset * chore: address reviews * chore: fix specs * chore: loads of FE fixes + BE adds * chore: add more polishes + reorg files * chore: fixes to promotions modal * chore: cleanup * chore: cleanup * chore: fix build * chore: fkix cart spec * chore: fix module tests * chore: fix moar tests * wip * chore: templates + fixes + migrate currency * chore: fix build, add validation for max_quantity * chore: allow removing campaigns * chore: fix specs * chore: scope campaigns based on currency * remove console logs * chore: add translations + update keys * chore: move over filesfrom v2 to routes * chore(dashboard): Delete old translation files (#7423) * feat(dashboard,admin-sdk,admin-shared,admin-vite-plugin): Add support for UI extensions (#7383) * intial work * update lock * add routes and fix HMR of configs * cleanup * rm imports * rm debug from plugin * address feedback * address feedback * temp skip specs --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com> Co-authored-by: Shahed Nasser <shahednasser@gmail.com> Co-authored-by: Stevche Radevski <sradevski@live.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>
290 lines
7.3 KiB
TypeScript
290 lines
7.3 KiB
TypeScript
import { AdminGetPromotionsParams } from "@medusajs/medusa"
|
|
import { AdminRuleValueOptionsListResponse } from "@medusajs/types"
|
|
import {
|
|
QueryKey,
|
|
useMutation,
|
|
UseMutationOptions,
|
|
useQuery,
|
|
UseQueryOptions,
|
|
} from "@tanstack/react-query"
|
|
import { client } from "../../lib/client"
|
|
import { queryClient } from "../../lib/query-client"
|
|
import { queryKeysFactory } from "../../lib/query-key-factory"
|
|
import {
|
|
BatchAddPromotionRulesReq,
|
|
BatchRemovePromotionRulesReq,
|
|
BatchUpdatePromotionRulesReq,
|
|
CreatePromotionReq,
|
|
UpdatePromotionReq,
|
|
} from "../../types/api-payloads"
|
|
import {
|
|
PromotionDeleteRes,
|
|
PromotionListRes,
|
|
PromotionRes,
|
|
PromotionRuleAttributesListRes,
|
|
PromotionRuleOperatorsListRes,
|
|
PromotionRulesListRes,
|
|
} from "../../types/api-responses"
|
|
import { campaignsQueryKeys } from "./campaigns"
|
|
|
|
const PROMOTIONS_QUERY_KEY = "promotions" as const
|
|
export const promotionsQueryKeys = {
|
|
...queryKeysFactory(PROMOTIONS_QUERY_KEY),
|
|
listRules: (id: string | null, ruleType: string) => [
|
|
PROMOTIONS_QUERY_KEY,
|
|
id,
|
|
ruleType,
|
|
],
|
|
listRuleAttributes: (ruleType: string) => [PROMOTIONS_QUERY_KEY, ruleType],
|
|
listRuleValues: (ruleType: string, ruleValue: string, query: object) => [
|
|
PROMOTIONS_QUERY_KEY,
|
|
ruleType,
|
|
ruleValue,
|
|
query,
|
|
],
|
|
listRuleOperators: () => [PROMOTIONS_QUERY_KEY],
|
|
}
|
|
|
|
export const usePromotion = (
|
|
id: string,
|
|
options?: Omit<
|
|
UseQueryOptions<PromotionRes, Error, PromotionRes, QueryKey>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.detail(id),
|
|
queryFn: async () => client.promotions.retrieve(id),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const usePromotionRules = (
|
|
id: string | null,
|
|
ruleType: string,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
PromotionRulesListRes,
|
|
Error,
|
|
PromotionRulesListRes,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.listRules(id, ruleType),
|
|
queryFn: async () => client.promotions.listRules(id, ruleType),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const usePromotions = (
|
|
query?: AdminGetPromotionsParams,
|
|
options?: Omit<
|
|
UseQueryOptions<PromotionListRes, Error, PromotionListRes, QueryKey>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.list(query),
|
|
queryFn: async () => client.promotions.list(query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const usePromotionRuleOperators = (
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
PromotionListRes,
|
|
Error,
|
|
PromotionRuleOperatorsListRes,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.listRuleOperators(),
|
|
queryFn: async () => client.promotions.listRuleOperators(),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const usePromotionRuleAttributes = (
|
|
ruleType: string,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
PromotionListRes,
|
|
Error,
|
|
PromotionRuleAttributesListRes,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.listRuleAttributes(ruleType),
|
|
queryFn: async () => client.promotions.listRuleAttributes(ruleType),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const usePromotionRuleValues = (
|
|
ruleType: string,
|
|
ruleValue: string,
|
|
query?: Record<string, any>,
|
|
options?: Omit<
|
|
UseQueryOptions<
|
|
AdminRuleValueOptionsListResponse,
|
|
Error,
|
|
AdminRuleValueOptionsListResponse,
|
|
QueryKey
|
|
>,
|
|
"queryFn" | "queryKey"
|
|
>
|
|
) => {
|
|
const { data, ...rest } = useQuery({
|
|
queryKey: promotionsQueryKeys.listRuleValues(
|
|
ruleType,
|
|
ruleValue,
|
|
query || {}
|
|
),
|
|
queryFn: async () =>
|
|
client.promotions.listRuleValues(ruleType, ruleValue, query),
|
|
...options,
|
|
})
|
|
|
|
return { ...data, ...rest }
|
|
}
|
|
|
|
export const useDeletePromotion = (
|
|
id: string,
|
|
options?: UseMutationOptions<PromotionDeleteRes, Error, void>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: () => client.promotions.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<PromotionRes, Error, CreatePromotionReq>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => client.promotions.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<PromotionRes, Error, UpdatePromotionReq>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => client.promotions.update(id, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
|
queryClient.invalidateQueries({
|
|
queryKey: promotionsQueryKeys.detail(id),
|
|
})
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const usePromotionAddRules = (
|
|
id: string,
|
|
ruleType: string,
|
|
options?: UseMutationOptions<PromotionRes, Error, BatchAddPromotionRulesReq>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) => client.promotions.addRules(id, ruleType, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
|
queryClient.invalidateQueries({
|
|
queryKey: promotionsQueryKeys.detail(id),
|
|
})
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const usePromotionRemoveRules = (
|
|
id: string,
|
|
ruleType: string,
|
|
options?: UseMutationOptions<
|
|
PromotionRes,
|
|
Error,
|
|
BatchRemovePromotionRulesReq
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) =>
|
|
client.promotions.removeRules(id, ruleType, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
|
queryClient.invalidateQueries({
|
|
queryKey: promotionsQueryKeys.detail(id),
|
|
})
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export const usePromotionUpdateRules = (
|
|
id: string,
|
|
ruleType: string,
|
|
options?: UseMutationOptions<
|
|
PromotionRes,
|
|
Error,
|
|
BatchUpdatePromotionRulesReq
|
|
>
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: (payload) =>
|
|
client.promotions.updateRules(id, ruleType, payload),
|
|
onSuccess: (data, variables, context) => {
|
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() })
|
|
queryClient.invalidateQueries({
|
|
queryKey: promotionsQueryKeys.listRules(id, ruleType),
|
|
})
|
|
queryClient.invalidateQueries({
|
|
queryKey: promotionsQueryKeys.detail(id),
|
|
})
|
|
|
|
options?.onSuccess?.(data, variables, context)
|
|
},
|
|
...options,
|
|
})
|
|
}
|