Skip to content

Commit 87a61ba

Browse files
authored
fix(dashboard): handle large resource count in tax rule override edit form (medusajs#13297)
* fix(dashboard): handle larege resource count in tax rule override edit form * fix: typo * fix: rm log
1 parent ff98055 commit 87a61ba

File tree

4 files changed

+99
-27
lines changed

4 files changed

+99
-27
lines changed

.changeset/quick-goats-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
fix(dashboard): handle large resource count in tax rule override edit form

packages/admin/dashboard/src/routes/tax-regions/common/components/target-item/target-item.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import { XMarkMini } from "@medusajs/icons"
22
import { IconButton, Text } from "@medusajs/ui"
3+
import { useProduct } from "../../../../../hooks/api"
34

45
type TargetItemProps = {
56
index: number
67
onRemove: (index: number) => void
78
label: string
9+
value: string
810
}
911

10-
export const TargetItem = ({ index, label, onRemove }: TargetItemProps) => {
12+
export const TargetItem = ({
13+
index,
14+
label,
15+
onRemove,
16+
value,
17+
}: TargetItemProps) => {
18+
const { product } = useProduct(
19+
value,
20+
{ fields: "id,title" },
21+
{ enabled: !label }
22+
)
23+
1124
return (
1225
<div className="bg-ui-bg-field-component shadow-borders-base flex items-center justify-between gap-2 rounded-md px-2 py-0.5">
1326
<Text size="small" leading="compact">
14-
{label}
27+
{label || product?.title}
1528
</Text>
1629
<IconButton
1730
size="small"

packages/admin/dashboard/src/routes/tax-regions/tax-region-tax-override-edit/components/tax-region-tax-override-edit-form/tax-region-tax-override-edit-form.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import {
3838
import { createTaxRulePayload } from "../../../common/utils"
3939
import { InitialRuleValues } from "../../types"
4040

41+
export const DISPLAY_OVERRIDE_ITEMS_LIMIT = 10
42+
4143
type TaxRegionTaxOverrideEditFormProps = {
4244
taxRate: HttpTypes.AdminTaxRate
4345
initialValues: InitialRuleValues
@@ -272,7 +274,7 @@ export const TaxRegionTaxOverrideEditForm = ({
272274
}
273275

274276
const getFieldHandler = (type: TaxRateRuleReferenceType) => {
275-
const { fields, remove, append } = getControls(type)
277+
const { fields, remove, prepend } = getControls(type)
276278
const modalId = getStackedModalId(type)
277279

278280
return (references: TaxRateRuleReference[]) => {
@@ -296,7 +298,7 @@ export const TaxRegionTaxOverrideEditForm = ({
296298
}
297299
}
298300

299-
append(fieldsToAdd)
301+
prepend(fieldsToAdd) // to display newer items first
300302
setIsOpen(modalId, false)
301303
}
302304
}
@@ -588,17 +590,33 @@ export const TaxRegionTaxOverrideEditForm = ({
588590
<div className="flex flex-col gap-y-1.5">
589591
<Divider variant="dashed" />
590592
<div className="flex flex-col gap-y-1.5 px-1.5">
591-
{fields.map((field, index) => {
592-
return (
593-
<TargetItem
594-
key={field.id}
595-
index={index}
596-
label={field.label}
597-
onRemove={remove}
598-
/>
599-
)
600-
})}
593+
{fields
594+
.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT)
595+
.map((field, index) => {
596+
return (
597+
<TargetItem
598+
key={field.id}
599+
index={index}
600+
label={field.label}
601+
value={field.value}
602+
onRemove={remove}
603+
/>
604+
)
605+
})}
601606
</div>
607+
{fields.length >
608+
DISPLAY_OVERRIDE_ITEMS_LIMIT && (
609+
<div className="flex flex-col gap-y-1.5 px-1.5">
610+
{/* <Divider variant="dashed" /> */}
611+
<div className="text-ui-fg-muted txt-small flex flex-col gap-y-1.5 px-1.5">
612+
{t("general.plusCountMore", {
613+
count:
614+
fields.length -
615+
DISPLAY_OVERRIDE_ITEMS_LIMIT,
616+
})}
617+
</div>
618+
</div>
619+
)}
602620
</div>
603621
) : null}
604622
</div>

packages/admin/dashboard/src/routes/tax-regions/tax-region-tax-override-edit/tax-region-tax-override-edit.tsx

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import { RouteDrawer } from "../../../components/modals"
77
import { useProductTypes } from "../../../hooks/api/product-types"
88
import { useProducts } from "../../../hooks/api/products"
99
import { TaxRateRuleReferenceType } from "../common/constants"
10-
import { TaxRegionTaxOverrideEditForm } from "./components/tax-region-tax-override-edit-form"
10+
import {
11+
DISPLAY_OVERRIDE_ITEMS_LIMIT,
12+
TaxRegionTaxOverrideEditForm,
13+
} from "./components/tax-region-tax-override-edit-form"
1114
import { InitialRuleValues } from "./types"
1215
import { useShippingOptions, useTaxRate } from "../../../hooks/api"
16+
import { TaxRateRuleReference } from "../common/schemas"
1317

1418
export const TaxRegionTaxOverrideEdit = () => {
1519
const { t } = useTranslation()
@@ -49,7 +53,7 @@ export const TaxRegionTaxOverrideEdit = () => {
4953

5054
const useDefaultRulesValues = (
5155
taxRate?: HttpTypes.AdminTaxRate
52-
): { initialValues?: InitialRuleValues; isPending: boolean } => {
56+
): { initialValues: InitialRuleValues; isPending: boolean } => {
5357
const rules = taxRate?.rules || []
5458

5559
const idsByReferenceType: {
@@ -63,10 +67,12 @@ const useDefaultRulesValues = (
6367
// [TaxRateRuleReferenceType.CUSTOMER_GROUP]: [],
6468
}
6569

66-
rules.forEach((rule) => {
67-
const reference = rule.reference as TaxRateRuleReferenceType
68-
idsByReferenceType[reference]?.push(rule.reference_id)
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+
})
7076

7177
const queries = [
7278
{
@@ -137,8 +143,21 @@ const useDefaultRulesValues = (
137143

138144
const queryResults = queries.map(({ ids, hook }) => {
139145
const enabled = ids.length > 0
146+
140147
return {
141-
result: hook({ id: ids, limit: ids.length }, { enabled }),
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+
),
142161
enabled,
143162
}
144163
})
@@ -162,12 +181,29 @@ const useDefaultRulesValues = (
162181
})
163182

164183
const initialRulesValues: InitialRuleValues = queries.reduce(
165-
(acc, { key, getResult }, index) => ({
166-
...acc,
167-
[key]: queryResults[index].enabled
168-
? getResult(queryResults[index].result)
169-
: [],
170-
}),
184+
(acc, { key, getResult }, index) => {
185+
let initialValues: TaxRateRuleReference[] = []
186+
187+
if (queryResults[index].enabled) {
188+
const fetchedEntityList = getResult(queryResults[index].result)
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+
},
171207
{} as InitialRuleValues
172208
)
173209

0 commit comments

Comments
 (0)