1 import { PencilSquare, Trash } from "@medusajs/icons"
2 import { AdminCampaignResponse } from "@medusajs/types"
3 import {
4 Badge,
5 Container,
6 Heading,
7 StatusBadge,
8 Text,
9 toast,
10 usePrompt,
11 } from "@medusajs/ui"
12 import { useTranslation } from "react-i18next"
13 import { useNavigate } from "react-router-dom"
14 import { ActionMenu } from "../../../../../components/common/action-menu"
15 import { useDeleteCampaign } from "../../../../../hooks/api/campaigns"
16 import { currencies } from "../../../../../lib/data/currencies"
17 import {
18 campaignStatus,
19 statusColor,
20 } from "../../../common/utils/campaign-status"
21
22 type CampaignGeneralSectionProps = {
23 campaign: AdminCampaignResponse["campaign"]
24 }
25
26 export const CampaignGeneralSection = ({
27 campaign,
28 }: CampaignGeneralSectionProps) => {
29 const { t } = useTranslation()
30 const prompt = usePrompt()
31 const navigate = useNavigate()
32 const { mutateAsync } = useDeleteCampaign(campaign.id)
33
34 const handleDelete = async () => {
35 const res = await prompt({
36 title: t("campaigns.delete.title"),
37 description: t("campaigns.delete.description", {
38 name: campaign.name,
39 }),
40 confirmText: t("actions.delete"),
41 cancelText: t("actions.cancel"),
42 })
43
44 if (!res) {
45 return
46 }
47
48 await mutateAsync(undefined, {
49 onSuccess: () => {
50 toast.success(
51 t("campaigns.delete.successToast", {
52 name: campaign.name,
53 })
54 )
55
56 navigate("/campaigns", { replace: true })
57 },
58 onError: (error) => {
59 toast.error(error.message)
60 },
61 })
62 }
63
64 const status = campaignStatus(campaign)
65
66 return (
67 <Container className="divide-y p-0">
68 <div className="flex items-center justify-between px-6 py-4">
69 <Heading>{campaign.name}</Heading>
70
71 <div className="flex items-center gap-x-4">
72 <StatusBadge color={statusColor(status)}>
73 {t(`campaigns.status.${status}`)}
74 </StatusBadge>
75
76 <ActionMenu
77 groups={[
78 {
79 actions: [
80 {
81 icon: <PencilSquare />,
82 label: t("actions.edit"),
83 to: `/campaigns/${campaign.id}/edit`,
84 },
85 ],
86 },
87 {
88 actions: [
89 {
90 icon: <Trash />,
91 label: t("actions.delete"),
92 onClick: handleDelete,
93 },
94 ],
95 },
96 ]}
97 />
98 </div>
99 </div>
100
101 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
102 <Text size="small" leading="compact" weight="plus">
103 {t("campaigns.fields.identifier")}
104 </Text>
105
106 <Text size="small" leading="compact">
107 {campaign.campaign_identifier}
108 </Text>
109 </div>
110
111 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
112 <Text size="small" leading="compact" weight="plus">
113 {t("fields.description")}
114 </Text>
115
116 <Text size="small" leading="compact">
117 {campaign.description || "-"}
118 </Text>
119 </div>
120
121 {campaign?.budget && campaign.budget.type === "spend" && (
122 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
123 <Text size="small" leading="compact" weight="plus">
124 {t("fields.currency")}
125 </Text>
126
127 <div>
128 <Badge size="xsmall">{campaign?.budget.currency_code}</Badge>
129 <Text className="inline pl-3" size="small" leading="compact">
130 {currencies[campaign?.budget.currency_code?.toUpperCase()]?.name}
131 </Text>
132 </div>
133 </div>
134 )}
135 </Container>
136 )
137 }