feat(dashboard): added details page for promotions + edit sliders (#6882)

* chore: added details page for promotions

* chore: add edit rules, edit details and edit campaign pages

* chore: change to type button

* chore: connection for rules

* chore: listing rule labels of multiple modules

* chore: add badge summary list

* chore: fix campaigns
This commit is contained in:
Riqwan Thamir
2024-04-06 13:20:31 +02:00
committed by GitHub
parent 07fb058d96
commit 5e30b8cce6
60 changed files with 2803 additions and 153 deletions
@@ -0,0 +1,96 @@
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 { CreateCampaignReq, UpdateCampaignReq } from "../../types/api-payloads"
import {
CampaignDeleteRes,
CampaignListRes,
CampaignRes,
} from "../../types/api-responses"
const REGIONS_QUERY_KEY = "campaigns" as const
const campaignsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
export const useCampaign = (
id: string,
options?: Omit<
UseQueryOptions<CampaignRes, Error, CampaignRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: campaignsQueryKeys.detail(id),
queryFn: async () => client.campaigns.retrieve(id),
...options,
})
return { ...data, ...rest }
}
export const useCampaigns = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<CampaignListRes, Error, CampaignListRes, QueryKey>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.campaigns.list(query),
queryKey: campaignsQueryKeys.list(query),
...options,
})
return { ...data, ...rest }
}
export const useCreateCampaign = (
options?: UseMutationOptions<CampaignRes, Error, CreateCampaignReq>
) => {
return useMutation({
mutationFn: (payload) => client.campaigns.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useUpdateCampaign = (
id: string,
options?: UseMutationOptions<CampaignRes, Error, UpdateCampaignReq>
) => {
return useMutation({
mutationFn: (payload) => client.campaigns.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeleteCampaign = (
id: string,
options?: UseMutationOptions<CampaignDeleteRes, Error, void>
) => {
return useMutation({
mutationFn: () => client.campaigns.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.detail(id) })
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
@@ -1,12 +1,47 @@
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
import { AdminGetPromotionsParams } from "@medusajs/medusa"
import {
QueryKey,
useMutation,
UseMutationOptions,
useQuery,
UseQueryOptions,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { PromotionListRes, PromotionRes } from "../../types/api-responses"
import {
BatchAddPromotionRulesReq,
BatchRemovePromotionRulesReq,
BatchUpdatePromotionRulesReq,
CreatePromotionReq,
UpdatePromotionReq,
} from "../../types/api-payloads"
import {
PromotionDeleteRes,
PromotionListRes,
PromotionRes,
PromotionRuleAttributesListRes,
PromotionRuleOperatorsListRes,
PromotionRulesListRes,
PromotionRuleValuesListRes,
} from "../../types/api-responses"
const PROMOTIONS_QUERY_KEY = "promotions" as const
const promotionsQueryKeys = queryKeysFactory(PROMOTIONS_QUERY_KEY)
export const promotionsQueryKeys = {
...queryKeysFactory(PROMOTIONS_QUERY_KEY),
listRules: (id: string, ruleType: string) => [
PROMOTIONS_QUERY_KEY,
id,
ruleType,
],
listRuleAttributes: (ruleType: string) => [PROMOTIONS_QUERY_KEY, ruleType],
listRuleValues: (ruleType: string, ruleValue: string) => [
PROMOTIONS_QUERY_KEY,
ruleType,
ruleValue,
],
listRuleOperators: () => [PROMOTIONS_QUERY_KEY],
}
export const usePromotion = (
id: string,
@@ -24,6 +59,28 @@ export const usePromotion = (
return { ...data, ...rest }
}
export const usePromotionRules = (
id: string,
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<
@@ -39,3 +96,185 @@ export const usePromotions = (
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,
options?: Omit<
UseQueryOptions<
PromotionListRes,
Error,
PromotionRuleValuesListRes,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: promotionsQueryKeys.listRuleValues(ruleType, ruleValue),
queryFn: async () => client.promotions.listRuleValues(ruleType, ruleValue),
...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() })
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,
})
}