1 import { AdminCampaign, AdminPromotion } from "@medusajs/types"
2 import { DataTableCommand, usePrompt } from "@medusajs/ui"
3 import { useMemo } from "react"
4 import { useTranslation } from "react-i18next"
5
6 import { ConfigurableDataTable } from "../../../../../components/table/configurable-data-table"
7 import { useAddOrRemoveCampaignPromotions } from "../../../../../hooks/api/campaigns"
8 import { usePromotions } from "../../../../../hooks/api/promotions"
9 import { createTableAdapter } from "../../../../../lib/table/table-adapters"
10 // Registers the `promotion_method` / `promotion_status` renderers globally.
11 import "../../../../promotions/promotion-list/components/promotion-list-table/promotion-list-table-renderers"
12 import { PromotionActions } from "./campaign-promotion-section"
13
14 const ALLOWED_FILTERS = [
15 "id",
16 "code",
17 "application_method.currency_code",
18 "created_at",
19 "updated_at",
20 "deleted_at",
21 ]
22
23 export const ConfigurableCampaignPromotionSection = ({
24 campaign,
25 }: {
26 campaign: AdminCampaign
27 }) => {
28 const { t } = useTranslation()
29 const prompt = usePrompt()
30 const { mutateAsync } = useAddOrRemoveCampaignPromotions(campaign.id)
31
32 const commands = useMemo<DataTableCommand[]>(
33 () => [
34 {
35 label: t("actions.remove"),
36 shortcut: "r",
37 action: async (selection) => {
38 const ids = Object.keys(selection)
39 const res = await prompt({
40 title: t("campaigns.promotions.remove.title", {
41 count: ids.length,
42 }),
43 description: t("campaigns.promotions.remove.description", {
44 count: ids.length,
45 }),
46 confirmText: t("actions.continue"),
47 cancelText: t("actions.cancel"),
48 })
49 if (!res) {
50 return
51 }
52 await mutateAsync({ remove: ids })
53 },
54 },
55 ],
56 [t, prompt, mutateAsync]
57 )
58
59 const adapter = useMemo(
60 () =>
61 createTableAdapter<AdminPromotion>({
62 entity: "promotions",
63 viewConfigurationKey: "promotions-campaign",
64 queryPrefix: "cmpprom",
65 pageSize: 10,
66 enableRowSelection: true,
67 commands,
68 emptyState: {
69 empty: {
70 heading: t("campaigns.promotions.list.noRecordsMessage"),
71 },
72 filtered: {
73 heading: t("general.noRecordsMessage"),
74 description: t("general.noRecordsMessageFiltered"),
75 },
76 },
77 useData: (fields, params) => {
78 const { promotions, count, isError, error, isLoading } =
79 usePromotions({
80 fields,
81 ...params,
82 campaign_id: campaign.id,
83 })
84 return { data: promotions, count, isLoading, isError, error }
85 },
86 getRowHref: (row) => `/promotions/${row.id}`,
87 renderRowActions: (row) => (
88 <PromotionActions promotion={row} campaignId={campaign.id} />
89 ),
90 transformColumns: (columns) =>
91 columns.map((column) => ({
92 ...column,
93 filter: !ALLOWED_FILTERS.includes(column.field)
94 ? { ...column.filter, enabled: false }
95 : column.filter,
96 })),
97 }),
98 [t, campaign.id, commands]
99 )
100
101 return (
102 <ConfigurableDataTable
103 adapter={adapter}
104 heading={t("promotions.domain")}
105 actions={[{ label: t("general.add"), to: `add-promotions` }]}
106 />
107 )
108 }