1 import { PencilSquare } from "@medusajs/icons"
2 import { ApplicationMethodTargetTypeValues } from "@medusajs/types"
3 import { Badge, Container, Heading } from "@medusajs/ui"
4 import { useTranslation } from "react-i18next"
5
6 import { ActionMenu } from "../../../../../components/common/action-menu"
7 import { BadgeListSummary } from "../../../../../components/common/badge-list-summary"
8 import { NoRecords } from "../../../../../components/common/empty-table-content"
9 import { ExtendedPromotionRule } from "../../promotion-detail"
10
11 type RuleProps = {
12 rule: ExtendedPromotionRule
13 }
14
15 function RuleBlock({ rule }: RuleProps) {
16 return (
17 <div className="bg-ui-bg-subtle shadow-borders-base align-center flex justify-around rounded-md p-2">
18 <div className="text-ui-fg-subtle txt-compact-xsmall flex items-center whitespace-nowrap">
19 <Badge
20 size="2xsmall"
21 key="rule-attribute"
22 className="txt-compact-xsmall-plus tag-neutral-text mx-1 inline-block truncate"
23 >
24 {rule.attribute_label}
25 </Badge>
26
27 <span className="txt-compact-2xsmall mx-1 inline-block">
28 {rule.operator_label}
29 </span>
30
31 <BadgeListSummary
32 inline
33 className="!txt-compact-small-plus"
34 list={
35 (rule.field_type === "number"
36 ? [rule.values]
37 : rule.values?.map((v) => v.label) || []
38 ).filter(Boolean) as string[]
39 }
40 />
41 </div>
42 </div>
43 )
44 }
45
46 export type PromotionConditionsSectionProps = {
47 rules: ExtendedPromotionRule[]
48 ruleType: string
49 applicationMethodTargetType?: ApplicationMethodTargetTypeValues
50 }
51
52 export const PromotionConditionsSection = ({
53 rules,
54 ruleType,
55 applicationMethodTargetType,
56 }: PromotionConditionsSectionProps) => {
57 const { t } = useTranslation()
58
59 return (
60 <Container className="p-0">
61 <div className="flex items-center justify-between px-6 py-4">
62 <div className="flex flex-col">
63 <Heading level="h2">
64 {(t as any)(
65 ruleType === "target-rules"
66 ? `promotions.fields.conditions.${ruleType}.${applicationMethodTargetType}.title`
67 : `promotions.fields.conditions.${ruleType}.title`
68 )}
69 </Heading>
70 </div>
71
72 <ActionMenu
73 groups={[
74 {
75 actions: [
76 {
77 icon: <PencilSquare />,
78 label: t("actions.edit"),
79 to: `${ruleType}/edit`,
80 },
81 ],
82 },
83 ]}
84 />
85 </div>
86
87 <div className="text-ui-fg-subtle flex flex-col gap-2 px-6 pb-4 pt-2">
88 {!rules.length && (
89 <NoRecords
90 className="h-[180px]"
91 title={t("general.noRecordsTitle")}
92 message={t("promotions.conditions.list.noRecordsMessage")}
93 action={{
94 to: `${ruleType}/edit`,
95 label: t("promotions.conditions.add"),
96 }}
97 buttonVariant="transparentIconLeft"
98 />
99 )}
100
101 {rules.map((rule) => (
102 <RuleBlock key={`${rule.id}-${rule.attribute}`} rule={rule} />
103 ))}
104 </div>
105 </Container>
106 )
107 }