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