|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { Heading } from "@medusajs/ui" |
|
3
|
import { useTranslation } from "react-i18next" |
|
4
|
import { useParams } from "react-router-dom" |
|
5
|
|
|
6
|
import { RouteDrawer } from "../../../components/modals" |
|
7
|
import { useProductTypes } from "../../../hooks/api/product-types" |
|
8
|
import { useProducts } from "../../../hooks/api/products" |
|
9
|
import { TaxRateRuleReferenceType } from "../common/constants" |
|
10
|
import { |
|
11
|
DISPLAY_OVERRIDE_ITEMS_LIMIT, |
|
12
|
TaxRegionTaxOverrideEditForm, |
|
13
|
} from "./components/tax-region-tax-override-edit-form" |
|
14
|
import { InitialRuleValues } from "./types" |
|
15
|
import { useShippingOptions, useTaxRate } from "../../../hooks/api" |
|
16
|
import { TaxRateRuleReference } from "../common/schemas" |
|
17
|
|
|
18
|
export const TaxRegionTaxOverrideEdit = () => { |
|
19
|
const { t } = useTranslation() |
|
20
|
const { tax_rate_id } = useParams() |
|
21
|
|
|
22
|
const { tax_rate, isPending, isError, error } = useTaxRate(tax_rate_id!) |
|
23
|
|
|
24
|
const { initialValues, isPending: isInitializing } = |
|
25
|
useDefaultRulesValues(tax_rate) |
|
26
|
|
|
27
|
const ready = !isPending && !!tax_rate && !isInitializing && !!initialValues |
|
28
|
|
|
29
|
if (isError) { |
|
30
|
throw error |
|
31
|
} |
|
32
|
|
|
33
|
return ( |
|
34
|
<RouteDrawer> |
|
35
|
<RouteDrawer.Header> |
|
36
|
<RouteDrawer.Title asChild> |
|
37
|
<Heading>{t("taxRegions.taxOverrides.edit.header")}</Heading> |
|
38
|
</RouteDrawer.Title> |
|
39
|
<RouteDrawer.Description className="sr-only"> |
|
40
|
{t("taxRegions.taxOverrides.edit.hint")} |
|
41
|
</RouteDrawer.Description> |
|
42
|
</RouteDrawer.Header> |
|
43
|
{ready && ( |
|
44
|
<TaxRegionTaxOverrideEditForm |
|
45
|
taxRate={tax_rate} |
|
46
|
isCombinable={true} |
|
47
|
initialValues={initialValues} |
|
48
|
/> |
|
49
|
)} |
|
50
|
</RouteDrawer> |
|
51
|
) |
|
52
|
} |
|
53
|
|
|
54
|
const useDefaultRulesValues = ( |
|
55
|
taxRate?: HttpTypes.AdminTaxRate |
|
56
|
): { initialValues?: InitialRuleValues; isPending: boolean } => { |
|
57
|
const rules = taxRate?.rules || [] |
|
58
|
|
|
59
|
const idsByReferenceType: { |
|
60
|
[key in TaxRateRuleReferenceType]: string[] |
|
61
|
} = { |
|
62
|
[TaxRateRuleReferenceType.PRODUCT]: [], |
|
63
|
// [TaxRateRuleReferenceType.PRODUCT_COLLECTION]: [], |
|
64
|
// [TaxRateRuleReferenceType.PRODUCT_TAG]: [], |
|
65
|
[TaxRateRuleReferenceType.PRODUCT_TYPE]: [], |
|
66
|
[TaxRateRuleReferenceType.SHIPPING_OPTION]: [], |
|
67
|
// [TaxRateRuleReferenceType.CUSTOMER_GROUP]: [], |
|
68
|
} |
|
69
|
|
|
70
|
rules |
|
71
|
.sort((a, b) => a.created_at.localeCompare(b.created_at)) // preffer newer rules for display |
|
72
|
.forEach((rule) => { |
|
73
|
const reference = rule.reference as TaxRateRuleReferenceType |
|
74
|
idsByReferenceType[reference]?.push(rule.reference_id) |
|
75
|
}) |
|
76
|
|
|
77
|
const queries = [ |
|
78
|
{ |
|
79
|
ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT], |
|
80
|
hook: useProducts, |
|
81
|
key: TaxRateRuleReferenceType.PRODUCT, |
|
82
|
getResult: (result: HttpTypes.AdminProductListResponse) => |
|
83
|
result.products.map((product) => ({ |
|
84
|
label: product.title, |
|
85
|
value: product.id, |
|
86
|
})), |
|
87
|
}, |
|
88
|
// { |
|
89
|
// ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT_COLLECTION], |
|
90
|
// hook: useCollections, |
|
91
|
// key: TaxRateRuleReferenceType.PRODUCT_COLLECTION, |
|
92
|
// getResult: (result: HttpTypes.AdminCollectionListResponse) => |
|
93
|
// result.collections.map((collection) => ({ |
|
94
|
// label: collection.title!, |
|
95
|
// value: collection.id!, |
|
96
|
// })), |
|
97
|
// }, |
|
98
|
// { |
|
99
|
// ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT_TAG], |
|
100
|
// hook: useProductTags, |
|
101
|
// key: TaxRateRuleReferenceType.PRODUCT_TAG, |
|
102
|
// getResult: (result: any) => |
|
103
|
// result.tags.map((tag: any) => ({ |
|
104
|
// label: tag.value, |
|
105
|
// value: tag.id, |
|
106
|
// })), |
|
107
|
// }, |
|
108
|
{ |
|
109
|
ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT_TYPE], |
|
110
|
hook: useProductTypes, |
|
111
|
key: TaxRateRuleReferenceType.PRODUCT_TYPE, |
|
112
|
getResult: (result: HttpTypes.AdminProductTypeListResponse) => |
|
113
|
result.product_types.map((productType) => ({ |
|
114
|
label: productType.value, |
|
115
|
value: productType.id, |
|
116
|
})), |
|
117
|
}, |
|
118
|
{ |
|
119
|
ids: idsByReferenceType[TaxRateRuleReferenceType.SHIPPING_OPTION], |
|
120
|
hook: useShippingOptions, |
|
121
|
key: TaxRateRuleReferenceType.SHIPPING_OPTION, |
|
122
|
getResult: (result: HttpTypes.AdminShippingOptionListResponse) => |
|
123
|
result.shipping_options.map((shippingOption) => ({ |
|
124
|
label: shippingOption.name, |
|
125
|
value: shippingOption.id, |
|
126
|
})), |
|
127
|
}, |
|
128
|
// { |
|
129
|
// ids: idsByReferenceType[TaxRateRuleReferenceType.CUSTOMER_GROUP], |
|
130
|
// hook: useCustomerGroups, |
|
131
|
// key: TaxRateRuleReferenceType.CUSTOMER_GROUP, |
|
132
|
// getResult: ( |
|
133
|
// result: HttpTypes.PaginatedResponse<{ |
|
134
|
// customer_groups: HttpTypes.AdminCustomerGroup[] |
|
135
|
// }> |
|
136
|
// ) => |
|
137
|
// result.customer_groups.map((customerGroup) => ({ |
|
138
|
// label: customerGroup.name!, |
|
139
|
// value: customerGroup.id, |
|
140
|
// })), |
|
141
|
// }, |
|
142
|
] |
|
143
|
|
|
144
|
const queryResults = queries.map(({ ids, hook }) => { |
|
145
|
const enabled = ids.length > 0 |
|
146
|
|
|
147
|
return { |
|
148
|
result: hook( |
|
149
|
{ |
|
150
|
/** |
|
151
|
* Limit fetch to 10 resources for display |
|
152
|
*/ |
|
153
|
id: |
|
154
|
ids.length > DISPLAY_OVERRIDE_ITEMS_LIMIT |
|
155
|
? ids.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT) |
|
156
|
: ids, |
|
157
|
limit: DISPLAY_OVERRIDE_ITEMS_LIMIT, |
|
158
|
}, |
|
159
|
{ enabled } |
|
160
|
), |
|
161
|
enabled, |
|
162
|
} |
|
163
|
}) |
|
164
|
|
|
165
|
if (!taxRate) { |
|
166
|
return { isPending: true } |
|
167
|
} |
|
168
|
|
|
169
|
const isPending = queryResults.some( |
|
170
|
({ result, enabled }) => enabled && result.isPending |
|
171
|
) |
|
172
|
|
|
173
|
if (isPending) { |
|
174
|
return { isPending } |
|
175
|
} |
|
176
|
|
|
177
|
queryResults.forEach(({ result, enabled }) => { |
|
178
|
if (enabled && result.isError) { |
|
179
|
throw result.error |
|
180
|
} |
|
181
|
}) |
|
182
|
|
|
183
|
const initialRulesValues: InitialRuleValues = queries.reduce( |
|
184
|
(acc, { key, getResult }, index) => { |
|
185
|
let initialValues: TaxRateRuleReference[] = [] |
|
186
|
|
|
187
|
if (queryResults[index].enabled) { |
|
188
|
const fetchedEntityList = getResult(queryResults[index].result as any) |
|
189
|
|
|
190
|
const entityIdMap = new Map( |
|
191
|
fetchedEntityList.map((entity) => [entity.value, entity]) |
|
192
|
) |
|
193
|
|
|
194
|
const initialIds = idsByReferenceType[key] |
|
195
|
|
|
196
|
initialValues = initialIds.map((id) => ({ |
|
197
|
value: id, |
|
198
|
label: entityIdMap.get(id)?.label || "", |
|
199
|
})) |
|
200
|
} |
|
201
|
|
|
202
|
return { |
|
203
|
...acc, |
|
204
|
[key]: initialValues, |
|
205
|
} |
|
206
|
}, |
|
207
|
{} as InitialRuleValues |
|
208
|
) |
|
209
|
|
|
210
|
return { initialValues: initialRulesValues, isPending: false } |
|
211
|
} |