1 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
2 import { useLoaderData, useParams } from "react-router-dom"
3
4 import { TwoColumnPageSkeleton } from "../../../components/common/skeleton"
5 import { LayoutComposer, detailPageDefaultEntries } from "../../../components/layout-composer"
6 import { usePromotion, usePromotionRules } from "../../../hooks/api/promotions"
7 import { CampaignSection } from "./components/campaign-section"
8 import { PromotionConditionsSection } from "./components/promotion-conditions-section"
9 import { PromotionGeneralSection } from "./components/promotion-general-section"
10 import { promotionLoader } from "./loader"
11 import { AdminPromotionRule } from "@medusajs/types"
12 import { BasePromotionRuleValue } from "@medusajs/types/dist/http/promotion/common"
13
14 export type ExtendedPromotionRule = Omit<AdminPromotionRule, "values"> & {
15 attribute_label?: string
16 operator_label?: string
17 field_type?: string
18 values?: (BasePromotionRuleValue & {
19 label?: string
20 })[]
21 }
22
23 export const PromotionDetail = () => {
24 const initialData = useLoaderData() as Awaited<
25 ReturnType<typeof promotionLoader>
26 >
27
28 const { id } = useParams()
29 const { promotion, isLoading } = usePromotion(id!, { initialData })
30 const query: Record<string, string> = {}
31
32 if (promotion?.type === "buyget") {
33 query.promotion_type = promotion.type
34 }
35
36 const { rules } = usePromotionRules(id!, "rules", query) as {
37 rules: ExtendedPromotionRule[]
38 }
39 const { rules: targetRules } = usePromotionRules(
40 id!,
41 "target-rules",
42 query
43 ) as {
44 rules: ExtendedPromotionRule[]
45 }
46 const { rules: buyRules } = usePromotionRules(id!, "buy-rules", query) as {
47 rules: ExtendedPromotionRule[]
48 }
49
50 if (isLoading || !promotion) {
51 return (
52 <TwoColumnPageSkeleton mainSections={3} sidebarSections={1} showJSON />
53 )
54 }
55
56 return (
57 <LayoutComposer
58 widgetsZonePrefix="promotion.details"
59 preferredLayoutId={CORE_LAYOUT_IDS.TWO_COLUMN}
60 data={promotion}
61 sections={{
62 main: (
63 <>
64 <LayoutComposer.Entry id="PromotionGeneralSection">
65 <PromotionGeneralSection promotion={promotion} />
66 </LayoutComposer.Entry>
67 <LayoutComposer.Entry id="PromotionConditionsSection:rules">
68 <PromotionConditionsSection
69 rules={rules || []}
70 ruleType={"rules"}
71 />
72 </LayoutComposer.Entry>
73 <LayoutComposer.Entry id="PromotionConditionsSection:target-rules">
74 <PromotionConditionsSection
75 rules={targetRules || []}
76 ruleType={"target-rules"}
77 applicationMethodTargetType={
78 promotion.application_method?.target_type || "items"
79 }
80 />
81 </LayoutComposer.Entry>
82 {promotion.type === "buyget" && (
83 <LayoutComposer.Entry id="PromotionConditionsSection:buy-rules">
84 <PromotionConditionsSection
85 rules={buyRules || []}
86 ruleType={"buy-rules"}
87 applicationMethodTargetType={"items"}
88 />
89 </LayoutComposer.Entry>
90 )}
91 {detailPageDefaultEntries(promotion)}
92 </>
93 ),
94 side: (
95 <>
96 <LayoutComposer.Entry id="CampaignSection">
97 <CampaignSection campaign={promotion.campaign!} />
98 </LayoutComposer.Entry>
99 </>
100 ),
101 }}
102 />
103 )
104 }