|
1
|
import { |
|
2
|
ArrowDownRightMini, |
|
3
|
PencilSquare, |
|
4
|
Trash, |
|
5
|
TriangleRightMini, |
|
6
|
} from "@medusajs/icons" |
|
7
|
import { HttpTypes } from "@medusajs/types" |
|
8
|
import { |
|
9
|
Badge, |
|
10
|
Divider, |
|
11
|
IconButton, |
|
12
|
StatusBadge, |
|
13
|
Text, |
|
14
|
Tooltip, |
|
15
|
} from "@medusajs/ui" |
|
16
|
import { Collapsible as RadixCollapsible } from "radix-ui" |
|
17
|
import { ComponentPropsWithoutRef } from "react" |
|
18
|
import { useTranslation } from "react-i18next" |
|
19
|
|
|
20
|
import { FetchError } from "@medusajs/js-sdk" |
|
21
|
import { ActionMenu } from "../../../../../components/common/action-menu" |
|
22
|
import { useProductTypes } from "../../../../../hooks/api/product-types" |
|
23
|
import { useProducts } from "../../../../../hooks/api/products" |
|
24
|
import { formatPercentage } from "../../../../../lib/percentage-helpers" |
|
25
|
import { TaxRateRuleReferenceType } from "../../constants" |
|
26
|
import { useDeleteTaxRateAction } from "../../hooks" |
|
27
|
import { useShippingOptions } from "../../../../../hooks/api" |
|
28
|
import { DISPLAY_OVERRIDE_ITEMS_LIMIT } from "../../../tax-region-tax-override-edit/components/tax-region-tax-override-edit-form" |
|
29
|
|
|
30
|
interface TaxOverrideCardProps extends ComponentPropsWithoutRef<"div"> { |
|
31
|
taxRate: HttpTypes.AdminTaxRate |
|
32
|
} |
|
33
|
|
|
34
|
export const TaxOverrideCard = ({ taxRate }: TaxOverrideCardProps) => { |
|
35
|
const { t } = useTranslation() |
|
36
|
const handleDelete = useDeleteTaxRateAction(taxRate) |
|
37
|
|
|
38
|
if (taxRate.is_default) { |
|
39
|
return null |
|
40
|
} |
|
41
|
|
|
42
|
const groupedRules = taxRate.rules.reduce((acc, rule) => { |
|
43
|
if (!acc[rule.reference]) { |
|
44
|
acc[rule.reference] = [] |
|
45
|
} |
|
46
|
|
|
47
|
acc[rule.reference].push(rule.reference_id) |
|
48
|
|
|
49
|
return acc |
|
50
|
}, {} as Record<string, string[]>) |
|
51
|
|
|
52
|
const validKeys = Object.values(TaxRateRuleReferenceType) |
|
53
|
const numberOfTargets = Object.keys(groupedRules).map((key) => |
|
54
|
validKeys.includes(key as TaxRateRuleReferenceType) |
|
55
|
).length |
|
56
|
|
|
57
|
return ( |
|
58
|
<RadixCollapsible.Root> |
|
59
|
<div className="flex items-center justify-between px-6 py-3"> |
|
60
|
<div className="flex items-center gap-x-2"> |
|
61
|
<RadixCollapsible.Trigger asChild> |
|
62
|
<IconButton size="2xsmall" variant="transparent" className="group"> |
|
63
|
<TriangleRightMini className="text-ui-fg-muted transition-transform group-data-[state='open']:rotate-90" /> |
|
64
|
</IconButton> |
|
65
|
</RadixCollapsible.Trigger> |
|
66
|
<div className="flex items-center gap-x-1.5"> |
|
67
|
<Text size="small" weight="plus" leading="compact"> |
|
68
|
{taxRate.name} |
|
69
|
</Text> |
|
70
|
{taxRate.code && ( |
|
71
|
<div className="text-ui-fg-subtle flex items-center gap-x-1.5"> |
|
72
|
<Text size="small" leading="compact"> |
|
73
|
· |
|
74
|
</Text> |
|
75
|
<Text size="small" leading="compact"> |
|
76
|
{taxRate.code} |
|
77
|
</Text> |
|
78
|
</div> |
|
79
|
)} |
|
80
|
</div> |
|
81
|
</div> |
|
82
|
<div className="flex items-center gap-x-3"> |
|
83
|
<Text size="small" leading="compact" className="text-ui-fg-subtle"> |
|
84
|
{t("taxRegions.fields.targets.numberOfTargets", { |
|
85
|
count: numberOfTargets, |
|
86
|
})} |
|
87
|
</Text> |
|
88
|
<div className="bg-ui-border-base h-3 w-px" /> |
|
89
|
<StatusBadge color={taxRate.is_combinable ? "green" : "grey"}> |
|
90
|
{taxRate.is_combinable |
|
91
|
? t("taxRegions.fields.isCombinable.true") |
|
92
|
: t("taxRegions.fields.isCombinable.false")} |
|
93
|
</StatusBadge> |
|
94
|
<ActionMenu |
|
95
|
groups={[ |
|
96
|
{ |
|
97
|
actions: [ |
|
98
|
{ |
|
99
|
label: t("actions.edit"), |
|
100
|
icon: <PencilSquare />, |
|
101
|
to: `overrides/${taxRate.id}/edit`, |
|
102
|
}, |
|
103
|
], |
|
104
|
}, |
|
105
|
{ |
|
106
|
actions: [ |
|
107
|
{ |
|
108
|
label: t("actions.delete"), |
|
109
|
icon: <Trash />, |
|
110
|
onClick: handleDelete, |
|
111
|
}, |
|
112
|
], |
|
113
|
}, |
|
114
|
]} |
|
115
|
/> |
|
116
|
</div> |
|
117
|
</div> |
|
118
|
<RadixCollapsible.Content> |
|
119
|
<div className="bg-ui-bg-subtle"> |
|
120
|
<Divider variant="dashed" /> |
|
121
|
<div className="px-6 py-3"> |
|
122
|
<div className="flex items-center gap-x-3"> |
|
123
|
<div className="text-ui-fg-muted flex size-5 items-center justify-center"> |
|
124
|
<ArrowDownRightMini /> |
|
125
|
</div> |
|
126
|
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-2"> |
|
127
|
<Badge size="2xsmall">{formatPercentage(taxRate.rate)}</Badge> |
|
128
|
<Text |
|
129
|
size="small" |
|
130
|
leading="compact" |
|
131
|
className="text-ui-fg-subtle" |
|
132
|
> |
|
133
|
{t("taxRegions.fields.targets.operators.on")} |
|
134
|
</Text> |
|
135
|
{Object.entries(groupedRules).map(([reference, ids], index) => { |
|
136
|
return ( |
|
137
|
<div |
|
138
|
key={reference} |
|
139
|
className="flex items-center gap-x-1.5" |
|
140
|
> |
|
141
|
<Reference |
|
142
|
key={reference} |
|
143
|
reference={reference as TaxRateRuleReferenceType} |
|
144
|
ids={ids} |
|
145
|
/> |
|
146
|
{index < Object.keys(groupedRules).length - 1 && ( |
|
147
|
<Text |
|
148
|
size="small" |
|
149
|
leading="compact" |
|
150
|
className="text-ui-fg-subtle" |
|
151
|
> |
|
152
|
{t("taxRegions.fields.targets.operators.and")} |
|
153
|
</Text> |
|
154
|
)} |
|
155
|
</div> |
|
156
|
) |
|
157
|
})} |
|
158
|
</div> |
|
159
|
</div> |
|
160
|
</div> |
|
161
|
</div> |
|
162
|
</RadixCollapsible.Content> |
|
163
|
</RadixCollapsible.Root> |
|
164
|
) |
|
165
|
} |
|
166
|
|
|
167
|
const Reference = ({ |
|
168
|
reference, |
|
169
|
ids, |
|
170
|
}: { |
|
171
|
reference: TaxRateRuleReferenceType |
|
172
|
ids: string[] |
|
173
|
}) => { |
|
174
|
return ( |
|
175
|
<div className="flex items-center gap-x-1.5"> |
|
176
|
<ReferenceBadge reference={reference} /> |
|
177
|
<ReferenceValues type={reference} ids={ids} /> |
|
178
|
</div> |
|
179
|
) |
|
180
|
} |
|
181
|
|
|
182
|
const ReferenceBadge = ({ |
|
183
|
reference, |
|
184
|
}: { |
|
185
|
reference: TaxRateRuleReferenceType |
|
186
|
}) => { |
|
187
|
const { t } = useTranslation() |
|
188
|
let label: string | null = null |
|
189
|
|
|
190
|
switch (reference) { |
|
191
|
case TaxRateRuleReferenceType.PRODUCT: |
|
192
|
label = t("taxRegions.fields.targets.tags.product") |
|
193
|
break |
|
194
|
// case TaxRateRuleReferenceType.PRODUCT_COLLECTION: |
|
195
|
// label = t("taxRegions.fields.targets.tags.productCollection") |
|
196
|
// break |
|
197
|
// case TaxRateRuleReferenceType.PRODUCT_TAG: |
|
198
|
// label = t("taxRegions.fields.targets.tags.productTag") |
|
199
|
// break |
|
200
|
case TaxRateRuleReferenceType.PRODUCT_TYPE: |
|
201
|
label = t("taxRegions.fields.targets.tags.productType") |
|
202
|
break |
|
203
|
case TaxRateRuleReferenceType.SHIPPING_OPTION: |
|
204
|
label = t("taxRegions.fields.targets.tags.shippingOption") |
|
205
|
break |
|
206
|
// case TaxRateRuleReferenceType.CUSTOMER_GROUP: |
|
207
|
// label = t("taxRegions.fields.targets.tags.customerGroup") |
|
208
|
// break |
|
209
|
} |
|
210
|
|
|
211
|
if (!label) { |
|
212
|
return null |
|
213
|
} |
|
214
|
|
|
215
|
return <Badge size="2xsmall">{label}</Badge> |
|
216
|
} |
|
217
|
|
|
218
|
const ReferenceValues = ({ |
|
219
|
type, |
|
220
|
ids, |
|
221
|
}: { |
|
222
|
type: TaxRateRuleReferenceType |
|
223
|
ids: string[] |
|
224
|
}) => { |
|
225
|
const { t } = useTranslation() |
|
226
|
|
|
227
|
const { isPending, additional, labels, isError, error } = useReferenceValues( |
|
228
|
type, |
|
229
|
ids |
|
230
|
) |
|
231
|
|
|
232
|
if (isError) { |
|
233
|
throw error |
|
234
|
} |
|
235
|
|
|
236
|
if (isPending) { |
|
237
|
return ( |
|
238
|
<div className="bg-ui-tag-neutral-bg border-ui-tag-neutral-border h-5 w-14 animate-pulse rounded-md" /> |
|
239
|
) |
|
240
|
} |
|
241
|
|
|
242
|
return ( |
|
243
|
<Tooltip |
|
244
|
content={ |
|
245
|
<ul> |
|
246
|
{labels?.map((label: string, index) => ( |
|
247
|
<li key={index}>{label}</li> |
|
248
|
))} |
|
249
|
{additional > 0 && ( |
|
250
|
<li> |
|
251
|
{t("taxRegions.fields.targets.additionalValues", { |
|
252
|
count: additional, |
|
253
|
})} |
|
254
|
</li> |
|
255
|
)} |
|
256
|
</ul> |
|
257
|
} |
|
258
|
> |
|
259
|
<Badge size="2xsmall"> |
|
260
|
{t("taxRegions.fields.targets.values", { |
|
261
|
count: ids.length, |
|
262
|
})} |
|
263
|
</Badge> |
|
264
|
</Tooltip> |
|
265
|
) |
|
266
|
} |
|
267
|
|
|
268
|
const useReferenceValues = ( |
|
269
|
type: TaxRateRuleReferenceType, |
|
270
|
ids: string[] |
|
271
|
): { |
|
272
|
labels: string[] | undefined |
|
273
|
isPending: boolean |
|
274
|
additional: number |
|
275
|
isError: boolean |
|
276
|
error: FetchError | null |
|
277
|
} => { |
|
278
|
const products = useProducts( |
|
279
|
{ |
|
280
|
id: ids.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT), |
|
281
|
limit: DISPLAY_OVERRIDE_ITEMS_LIMIT, |
|
282
|
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config |
|
283
|
fields: |
|
284
|
"-type,-collection,-options,-tags,-images,-variants,-sales_channels", |
|
285
|
}, |
|
286
|
{ |
|
287
|
enabled: !!ids.length && type === TaxRateRuleReferenceType.PRODUCT, |
|
288
|
} |
|
289
|
) |
|
290
|
|
|
291
|
// const tags = useProductTags( |
|
292
|
// { |
|
293
|
// id: ids, |
|
294
|
// limit: 10, |
|
295
|
// }, |
|
296
|
// { |
|
297
|
// enabled: !!ids.length && type === TaxRateRuleReferenceType.PRODUCT_TAG, |
|
298
|
// } |
|
299
|
// ) |
|
300
|
|
|
301
|
const productTypes = useProductTypes( |
|
302
|
{ |
|
303
|
id: ids.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT), |
|
304
|
limit: DISPLAY_OVERRIDE_ITEMS_LIMIT, |
|
305
|
}, |
|
306
|
{ |
|
307
|
enabled: !!ids.length && type === TaxRateRuleReferenceType.PRODUCT_TYPE, |
|
308
|
} |
|
309
|
) |
|
310
|
|
|
311
|
const shippingOptions = useShippingOptions( |
|
312
|
{ |
|
313
|
id: ids.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT), |
|
314
|
limit: DISPLAY_OVERRIDE_ITEMS_LIMIT, |
|
315
|
}, |
|
316
|
{ |
|
317
|
enabled: |
|
318
|
!!ids.length && type === TaxRateRuleReferenceType.SHIPPING_OPTION, |
|
319
|
} |
|
320
|
) |
|
321
|
// const collections = useCollections( |
|
322
|
// { |
|
323
|
// id: ids, |
|
324
|
// limit: 10, |
|
325
|
// }, |
|
326
|
// { |
|
327
|
// enabled: |
|
328
|
// !!ids.length && type === TaxRateRuleReferenceType.PRODUCT_COLLECTION, |
|
329
|
// } |
|
330
|
// ) |
|
331
|
|
|
332
|
// const customerGroups = useCustomerGroups( |
|
333
|
// { |
|
334
|
// id: ids, |
|
335
|
// limit: 10, |
|
336
|
// }, |
|
337
|
// { |
|
338
|
// enabled: !!ids.length && type === TaxRateRuleReferenceType.CUSTOMER_GROUP, |
|
339
|
// } |
|
340
|
// ) |
|
341
|
const additionalCount = |
|
342
|
ids.length > DISPLAY_OVERRIDE_ITEMS_LIMIT |
|
343
|
? ids.length - DISPLAY_OVERRIDE_ITEMS_LIMIT |
|
344
|
: 0 |
|
345
|
switch (type) { |
|
346
|
case TaxRateRuleReferenceType.PRODUCT: |
|
347
|
return { |
|
348
|
labels: products.products?.map((product) => product.title), |
|
349
|
isPending: products.isPending, |
|
350
|
additional: additionalCount, |
|
351
|
isError: products.isError, |
|
352
|
error: products.error, |
|
353
|
} |
|
354
|
// case TaxRateRuleReferenceType.PRODUCT_TAG: |
|
355
|
// return { |
|
356
|
// labels: tags.product_tags?.map((tag: any) => tag.value), |
|
357
|
// isPending: tags.isPending, |
|
358
|
// additional: |
|
359
|
// tags.product_tags && tags.count |
|
360
|
// ? tags.count - tags.product_tags.length |
|
361
|
// : 0, |
|
362
|
// isError: tags.isError, |
|
363
|
// error: tags.error, |
|
364
|
// } |
|
365
|
case TaxRateRuleReferenceType.PRODUCT_TYPE: |
|
366
|
return { |
|
367
|
labels: productTypes.product_types?.map((type) => type.value), |
|
368
|
isPending: productTypes.isPending, |
|
369
|
additional: additionalCount, |
|
370
|
isError: productTypes.isError, |
|
371
|
error: productTypes.error, |
|
372
|
} |
|
373
|
case TaxRateRuleReferenceType.SHIPPING_OPTION: |
|
374
|
return { |
|
375
|
labels: shippingOptions.shipping_options?.map((option) => option.name), |
|
376
|
isPending: shippingOptions.isPending, |
|
377
|
additional: additionalCount, |
|
378
|
isError: shippingOptions.isError, |
|
379
|
error: shippingOptions.error, |
|
380
|
} |
|
381
|
// case TaxRateRuleReferenceType.PRODUCT_COLLECTION: |
|
382
|
// return { |
|
383
|
// labels: collections.collections?.map((collection) => collection.title!), |
|
384
|
// isPending: collections.isPending, |
|
385
|
// additional: |
|
386
|
// collections.collections && collections.count |
|
387
|
// ? collections.count - collections.collections.length |
|
388
|
// : 0, |
|
389
|
// isError: collections.isError, |
|
390
|
// error: collections.error, |
|
391
|
// } |
|
392
|
// case TaxRateRuleReferenceType.CUSTOMER_GROUP: |
|
393
|
// return { |
|
394
|
// labels: customerGroups.customer_groups?.map((group) => group.name!), |
|
395
|
// isPending: customerGroups.isPending, |
|
396
|
// additional: |
|
397
|
// customerGroups.customer_groups && customerGroups.count |
|
398
|
// ? customerGroups.count - customerGroups.customer_groups.length |
|
399
|
// : 0, |
|
400
|
// isError: customerGroups.isError, |
|
401
|
// error: customerGroups.error, |
|
402
|
// } |
|
403
|
} |
|
404
|
} |