|
1
|
import { PencilSquare, Trash } from "@medusajs/icons" |
|
2
|
import { AdminApiKeyResponse, HttpTypes } from "@medusajs/types" |
|
3
|
import { DataTableCommand, toast, usePrompt } from "@medusajs/ui" |
|
4
|
import { useMemo } from "react" |
|
5
|
import { useTranslation } from "react-i18next" |
|
6
|
|
|
7
|
import { ActionMenu } from "../../../../../components/common/action-menu" |
|
8
|
import { ConfigurableDataTable } from "../../../../../components/table/configurable-data-table" |
|
9
|
import { useBatchRemoveSalesChannelsFromApiKey } from "../../../../../hooks/api/api-keys" |
|
10
|
import { useSalesChannels } from "../../../../../hooks/api/sales-channels" |
|
11
|
import { createTableAdapter } from "../../../../../lib/table/table-adapters" |
|
12
|
// Registers the `sales_channel_status` renderer for the is_disabled pill. |
|
13
|
import "../../../../sales-channels/sales-channel-list/components/sales-channel-table-renderers" |
|
14
|
|
|
15
|
const ALLOWED_FILTERS = [ |
|
16
|
"id", |
|
17
|
"name", |
|
18
|
"description", |
|
19
|
"is_disabled", |
|
20
|
"created_at", |
|
21
|
"updated_at", |
|
22
|
"deleted_at", |
|
23
|
] |
|
24
|
|
|
25
|
const ApiKeySalesChannelRowActions = ({ |
|
26
|
salesChannel, |
|
27
|
apiKeyId, |
|
28
|
}: { |
|
29
|
salesChannel: HttpTypes.AdminSalesChannel |
|
30
|
apiKeyId: string |
|
31
|
}) => { |
|
32
|
const { t } = useTranslation() |
|
33
|
const prompt = usePrompt() |
|
34
|
const { mutateAsync } = useBatchRemoveSalesChannelsFromApiKey(apiKeyId) |
|
35
|
|
|
36
|
const handleDelete = async () => { |
|
37
|
const res = await prompt({ |
|
38
|
title: t("general.areYouSure"), |
|
39
|
description: t("apiKeyManagement.removeSalesChannel.warning", { |
|
40
|
name: salesChannel.name, |
|
41
|
}), |
|
42
|
confirmText: t("actions.delete"), |
|
43
|
cancelText: t("actions.cancel"), |
|
44
|
}) |
|
45
|
if (!res) { |
|
46
|
return |
|
47
|
} |
|
48
|
await mutateAsync([salesChannel.id], { |
|
49
|
onSuccess: () => { |
|
50
|
toast.success( |
|
51
|
t("apiKeyManagement.removeSalesChannel.successToast", { count: 1 }) |
|
52
|
) |
|
53
|
}, |
|
54
|
onError: (err) => toast.error(err.message), |
|
55
|
}) |
|
56
|
} |
|
57
|
|
|
58
|
return ( |
|
59
|
<ActionMenu |
|
60
|
groups={[ |
|
61
|
{ |
|
62
|
actions: [ |
|
63
|
{ |
|
64
|
label: t("actions.edit"), |
|
65
|
icon: <PencilSquare />, |
|
66
|
to: `/settings/sales-channels/${salesChannel.id}/edit`, |
|
67
|
}, |
|
68
|
], |
|
69
|
}, |
|
70
|
{ |
|
71
|
actions: [ |
|
72
|
{ |
|
73
|
label: t("actions.delete"), |
|
74
|
icon: <Trash />, |
|
75
|
onClick: handleDelete, |
|
76
|
}, |
|
77
|
], |
|
78
|
}, |
|
79
|
]} |
|
80
|
/> |
|
81
|
) |
|
82
|
} |
|
83
|
|
|
84
|
export const ConfigurableApiKeySalesChannelSection = ({ |
|
85
|
apiKey, |
|
86
|
}: { |
|
87
|
apiKey: AdminApiKeyResponse["api_key"] |
|
88
|
}) => { |
|
89
|
const { t } = useTranslation() |
|
90
|
const prompt = usePrompt() |
|
91
|
const { mutateAsync } = useBatchRemoveSalesChannelsFromApiKey(apiKey.id) |
|
92
|
|
|
93
|
const commands = useMemo<DataTableCommand[]>( |
|
94
|
() => [ |
|
95
|
{ |
|
96
|
label: t("actions.remove"), |
|
97
|
shortcut: "r", |
|
98
|
action: async (selection) => { |
|
99
|
const ids = Object.keys(selection) |
|
100
|
const res = await prompt({ |
|
101
|
title: t("general.areYouSure"), |
|
102
|
description: t("apiKeyManagement.removeSalesChannel.warningBatch", { |
|
103
|
count: ids.length, |
|
104
|
}), |
|
105
|
confirmText: t("actions.continue"), |
|
106
|
cancelText: t("actions.cancel"), |
|
107
|
}) |
|
108
|
if (!res) { |
|
109
|
return |
|
110
|
} |
|
111
|
await mutateAsync(ids, { |
|
112
|
onSuccess: () => { |
|
113
|
toast.success( |
|
114
|
t("apiKeyManagement.removeSalesChannel.successToastBatch", { |
|
115
|
count: ids.length, |
|
116
|
}) |
|
117
|
) |
|
118
|
}, |
|
119
|
onError: (err) => toast.error(err.message), |
|
120
|
}) |
|
121
|
}, |
|
122
|
}, |
|
123
|
], |
|
124
|
[t, prompt, mutateAsync] |
|
125
|
) |
|
126
|
|
|
127
|
const adapter = useMemo( |
|
128
|
() => |
|
129
|
createTableAdapter<HttpTypes.AdminSalesChannel>({ |
|
130
|
entity: "sales-channels", |
|
131
|
viewConfigurationKey: "sales-channels-api-key", |
|
132
|
queryPrefix: "apisc", |
|
133
|
pageSize: 10, |
|
134
|
enableRowSelection: true, |
|
135
|
commands, |
|
136
|
emptyState: { |
|
137
|
empty: { heading: t("general.noRecordsMessage") }, |
|
138
|
filtered: { |
|
139
|
heading: t("general.noRecordsMessage"), |
|
140
|
description: t("general.noRecordsMessageFiltered"), |
|
141
|
}, |
|
142
|
}, |
|
143
|
useData: (fields, params) => { |
|
144
|
const { sales_channels, count, isError, error, isLoading } = |
|
145
|
useSalesChannels( |
|
146
|
{ |
|
147
|
fields, |
|
148
|
...params, |
|
149
|
publishable_key_id: apiKey.id, |
|
150
|
}, |
|
151
|
{ |
|
152
|
placeholderData: (previousData: any, previousQuery: any) => { |
|
153
|
const prevFields = |
|
154
|
previousQuery?.[previousQuery?.length - 1]?.query?.fields |
|
155
|
if (prevFields && prevFields !== fields) { |
|
156
|
return undefined |
|
157
|
} |
|
158
|
return previousData |
|
159
|
}, |
|
160
|
} |
|
161
|
) |
|
162
|
return { data: sales_channels, count, isLoading, isError, error } |
|
163
|
}, |
|
164
|
getRowHref: (row) => `/settings/sales-channels/${row.id}`, |
|
165
|
renderRowActions: (row) => ( |
|
166
|
<ApiKeySalesChannelRowActions |
|
167
|
salesChannel={row} |
|
168
|
apiKeyId={apiKey.id} |
|
169
|
/> |
|
170
|
), |
|
171
|
transformColumns: (columns) => |
|
172
|
columns.map((column) => ({ |
|
173
|
...column, |
|
174
|
filter: !ALLOWED_FILTERS.includes(column.field) |
|
175
|
? { ...column.filter, enabled: false } |
|
176
|
: column.filter, |
|
177
|
})), |
|
178
|
}), |
|
179
|
[t, apiKey.id, commands] |
|
180
|
) |
|
181
|
|
|
182
|
return ( |
|
183
|
<ConfigurableDataTable |
|
184
|
adapter={adapter} |
|
185
|
heading={t("salesChannels.domain")} |
|
186
|
actions={[{ label: t("actions.add"), to: "sales-channels" }]} |
|
187
|
/> |
|
188
|
) |
|
189
|
} |