1 import { PencilSquare, Trash } from "@medusajs/icons"
2 import { HttpTypes } from "@medusajs/types"
3 import {
4 Badge,
5 Container,
6 Copy,
7 Heading,
8 StatusBadge,
9 Text,
10 usePrompt,
11 } from "@medusajs/ui"
12 import { useTranslation } from "react-i18next"
13 import { useNavigate } from "react-router-dom"
14
15 import { ActionMenu } from "../../../../../components/common/action-menu"
16 import { useDeletePromotion } from "../../../../../hooks/api/promotions"
17 import { formatCurrency } from "../../../../../lib/format-currency"
18 import { formatPercentage } from "../../../../../lib/percentage-helpers"
19 import { getPromotionStatus } from "../../../../../lib/promotions"
20
21 type PromotionGeneralSectionProps = {
22 promotion: HttpTypes.AdminPromotion
23 }
24
25 function getDisplayValue(promotion: HttpTypes.AdminPromotion) {
26 const value = promotion.application_method?.value
27
28 if (!value) {
29 return null
30 }
31
32 if (promotion.application_method?.type === "fixed") {
33 const currency = promotion.application_method?.currency_code
34
35 if (!currency) {
36 return null
37 }
38
39 return formatCurrency(value, currency)
40 } else if (promotion.application_method?.type === "percentage") {
41 return formatPercentage(value)
42 }
43
44 return null
45 }
46
47 export const PromotionGeneralSection = ({
48 promotion,
49 }: PromotionGeneralSectionProps) => {
50 const { t } = useTranslation()
51 const prompt = usePrompt()
52 const navigate = useNavigate()
53 const { mutateAsync } = useDeletePromotion(promotion.id)
54
55 const handleDelete = async () => {
56 const confirm = await prompt({
57 title: t("general.areYouSure"),
58 description: t("promotions.deleteWarning", {
59 code: promotion.code,
60 }),
61 verificationInstruction: t("general.typeToConfirm"),
62 verificationText: promotion.code,
63 confirmText: t("actions.delete"),
64 cancelText: t("actions.cancel"),
65 })
66
67 if (!confirm) {
68 return
69 }
70
71 await mutateAsync(undefined, {
72 onSuccess: () => {
73 navigate("/promotions", { replace: true })
74 },
75 })
76 }
77
78 const [color, text] = getPromotionStatus(promotion)
79 const displayValue = getDisplayValue(promotion)
80
81 return (
82 <Container className="divide-y p-0">
83 <div className="flex items-center justify-between px-6 py-4">
84 <div className="flex flex-col">
85 <Heading>{promotion.code}</Heading>
86 </div>
87
88 <div className="flex items-center gap-x-2">
89 <StatusBadge color={color}>{text}</StatusBadge>
90 <ActionMenu
91 groups={[
92 {
93 actions: [
94 {
95 icon: <PencilSquare />,
96 label: t("actions.edit"),
97 to: `/promotions/${promotion.id}/edit`,
98 },
99 ],
100 },
101 {
102 actions: [
103 {
104 icon: <Trash />,
105 label: t("actions.delete"),
106 onClick: handleDelete,
107 },
108 ],
109 },
110 ]}
111 />
112 </div>
113 </div>
114
115 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
116 <Text size="small" weight="plus" leading="compact">
117 {t("promotions.fields.campaign")}
118 </Text>
119
120 <Text size="small" leading="compact" className="text-pretty">
121 {promotion.is_automatic
122 ? t("promotions.form.method.automatic.title")
123 : t("promotions.form.method.code.title")}
124 </Text>
125 </div>
126
127 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
128 <Text size="small" weight="plus" leading="compact">
129 {t("fields.code")}
130 </Text>
131
132 <Copy
133 content={promotion.code!}
134 className="text-ui-tag-neutral-text"
135 asChild
136 >
137 <Badge
138 size="2xsmall"
139 rounded="full"
140 className="cursor-pointer text-pretty"
141 >
142 {promotion.code}
143 </Badge>
144 </Copy>
145 </div>
146
147 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
148 <Text size="small" weight="plus" leading="compact">
149 {t("promotions.fields.type")}
150 </Text>
151
152 <Text size="small" leading="compact" className="text-pretty capitalize">
153 {promotion.type}
154 </Text>
155 </div>
156
157 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
158 <Text size="small" weight="plus" leading="compact">
159 {t("promotions.fields.value")}
160 </Text>
161
162 <div className="flex items-center gap-x-2">
163 <Text className="inline" size="small" leading="compact">
164 {displayValue || "-"}
165 </Text>
166 {promotion?.application_method?.type === "fixed" && (
167 <Badge size="2xsmall" rounded="full">
168 {promotion?.application_method?.currency_code?.toUpperCase()}
169 </Badge>
170 )}
171 </div>
172 </div>
173
174 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
175 <Text size="small" weight="plus" leading="compact">
176 {t("promotions.fields.allocation")}
177 </Text>
178
179 <Text size="small" leading="compact" className="text-pretty capitalize">
180 {promotion.application_method?.allocation!}
181 </Text>
182 </div>
183
184 {promotion.application_method?.type === "fixed" && (
185 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
186 <Text size="small" weight="plus" leading="compact">
187 {t("promotions.fields.taxInclusive")}
188 </Text>
189
190 <div className="flex items-center gap-x-2">
191 <Text className="inline" size="small" leading="compact">
192 {promotion.is_tax_inclusive
193 ? t("fields.true")
194 : t("fields.false")}
195 </Text>
196 </div>
197 </div>
198 )}
199
200 {typeof promotion.limit === "number" && (
201 <div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4">
202 <Text size="small" weight="plus" leading="compact">
203 Usage Limit
204 </Text>
205
206 <div className="flex items-center gap-x-2">
207 <Text className="inline" size="small" leading="compact">
208 {promotion.used || 0} / {promotion.limit}
209 </Text>
210 </div>
211 </div>
212 )}
213 </Container>
214 )
215 }