|
1
|
import { HttpTypes } from "@medusajs/types"; |
|
2
|
import { ColumnDef } from "@tanstack/react-table"; |
|
3
|
import { FieldPath, FieldValues } from "react-hook-form"; |
|
4
|
import { IncludesTaxTooltip } from "../../tax-badge"; |
|
5
|
import { DataGridCurrencyCell } from "../components/data-grid-currency-cell"; |
|
6
|
import { DataGridReadonlyCell } from "../components/data-grid-readonly-cell"; |
|
7
|
import { FieldContext } from "../types"; |
|
8
|
import { createDataGridHelper } from "./create-data-grid-column-helper"; |
|
9
|
|
|
10
|
type CreateDataGridPriceColumnsProps< |
|
11
|
TData, |
|
12
|
TFieldValues extends FieldValues |
|
13
|
> = { |
|
14
|
currencies?: string[]; |
|
15
|
regions?: HttpTypes.AdminRegion[]; |
|
16
|
pricePreferences?: HttpTypes.AdminPricePreference[]; |
|
17
|
isReadyOnly?: (context: FieldContext<TData>) => boolean; |
|
18
|
getFieldName: ( |
|
19
|
context: FieldContext<TData>, |
|
20
|
value: string |
|
21
|
) => FieldPath<TFieldValues> | null; |
|
22
|
}; |
|
23
|
|
|
24
|
export const createDataGridPriceColumns = < |
|
25
|
TData, |
|
26
|
TFieldValues extends FieldValues |
|
27
|
>({ |
|
28
|
currencies, |
|
29
|
regions, |
|
30
|
pricePreferences, |
|
31
|
isReadyOnly, |
|
32
|
getFieldName, |
|
33
|
}: CreateDataGridPriceColumnsProps<TData, TFieldValues>): ColumnDef< |
|
34
|
TData, |
|
35
|
unknown |
|
36
|
>[] => { |
|
37
|
const columnHelper = createDataGridHelper<TData, TFieldValues>(); |
|
38
|
|
|
39
|
return [ |
|
40
|
...(currencies?.map((currency) => { |
|
41
|
const preference = pricePreferences?.find( |
|
42
|
(p) => p.attribute === "currency_code" && p.value === currency |
|
43
|
); |
|
44
|
|
|
45
|
const translatedCurrencyName = `Price ${currency.toUpperCase()}`; |
|
46
|
|
|
47
|
return columnHelper.column({ |
|
48
|
id: `currency_prices.${currency}`, |
|
49
|
name: translatedCurrencyName, |
|
50
|
field: (context) => { |
|
51
|
const isReadyOnlyValue = isReadyOnly?.(context); |
|
52
|
|
|
53
|
if (isReadyOnlyValue) { |
|
54
|
return null; |
|
55
|
} |
|
56
|
|
|
57
|
return getFieldName(context, currency); |
|
58
|
}, |
|
59
|
type: "number", |
|
60
|
header: () => ( |
|
61
|
<div className="flex w-full items-center justify-between gap-3"> |
|
62
|
<span className="truncate" title={translatedCurrencyName}> |
|
63
|
{translatedCurrencyName} |
|
64
|
</span> |
|
65
|
<IncludesTaxTooltip includesTax={preference?.is_tax_inclusive} /> |
|
66
|
</div> |
|
67
|
), |
|
68
|
cell: (context) => { |
|
69
|
if (isReadyOnly?.(context)) { |
|
70
|
return <DataGridReadonlyCell context={context} />; |
|
71
|
} |
|
72
|
|
|
73
|
return <DataGridCurrencyCell code={currency} context={context} />; |
|
74
|
}, |
|
75
|
}); |
|
76
|
}) ?? []), |
|
77
|
...(regions?.map((region) => { |
|
78
|
const preference = pricePreferences?.find( |
|
79
|
(p) => p.attribute === "region_id" && p.value === region.id |
|
80
|
); |
|
81
|
|
|
82
|
const translatedRegionName = `Price ${region.name}`; |
|
83
|
|
|
84
|
return columnHelper.column({ |
|
85
|
id: `region_prices.${region.id}`, |
|
86
|
name: translatedRegionName, |
|
87
|
field: (context) => { |
|
88
|
const isReadyOnlyValue = isReadyOnly?.(context); |
|
89
|
|
|
90
|
if (isReadyOnlyValue) { |
|
91
|
return null; |
|
92
|
} |
|
93
|
|
|
94
|
return getFieldName(context, region.id); |
|
95
|
}, |
|
96
|
type: "number", |
|
97
|
header: () => ( |
|
98
|
<div className="flex w-full items-center justify-between gap-3"> |
|
99
|
<span className="truncate" title={translatedRegionName}> |
|
100
|
{translatedRegionName} |
|
101
|
</span> |
|
102
|
<IncludesTaxTooltip includesTax={preference?.is_tax_inclusive} /> |
|
103
|
</div> |
|
104
|
), |
|
105
|
cell: (context) => { |
|
106
|
if (isReadyOnly?.(context)) { |
|
107
|
return <DataGridReadonlyCell context={context} />; |
|
108
|
} |
|
109
|
|
|
110
|
const currency = currencies?.find((c) => c === region.currency_code); |
|
111
|
if (!currency) { |
|
112
|
return null; |
|
113
|
} |
|
114
|
|
|
115
|
return ( |
|
116
|
<DataGridCurrencyCell |
|
117
|
code={region.currency_code} |
|
118
|
context={context} |
|
119
|
/> |
|
120
|
); |
|
121
|
}, |
|
122
|
}); |
|
123
|
}) ?? []), |
|
124
|
]; |
|
125
|
}; |