Commit daa9214
authored
fix(dashboard): sorting countries by name or code no longer throws error (#16020)
## Summary
**What** — What changes are introduced in this PR?
Fixes an error thrown when sorting the countries table in the admin dashboard ("The key display_name is not a valid order key"). Affects the region detail page's country section, the add-countries modal, the region-create modal, and the geo-zone country picker under shipping/locations.
Also fixing string interpolation on console log for the error - ${key} was not being evaluated.
**Why** — Why are these changes relevant or necessary?
All four `_DataTable` instances that render a sortable countries list defined their own `orderBy` prop independently of the table's column definitions, and had copied the column ids (`display_name`, `iso_2`) instead of the values useCountries' `acceptedOrderKeys` allowlist actually accepted (name, code). `display_name` was rejected outright by the hook, throwing on sort. Additionally, "code" never corresponded to a real field anywhere in the data model, it existed only as a magic string inside the hook's internal `key === "code" ? "iso_2" : "name"` translation.
**How** — How have these changes been implemented?
Aligned both sides of the boundary on the real field names already present on `RegionCountryDTO`/`StaticCountry`:`name` and `iso_2`.
- `use-countries.tsx`: changed `acceptedOrderKeys` to ["name", "iso_2"] and removed the "code" → "iso_2" translation, since the order key now maps directly to a real field. Also using back-tick instead of `""` for the console log.
- Updated the `orderBy` arrays in `region-country-section.tsx`, `add-countries-form.tsx`, `create-region-form.tsx`, and `geo-zone-form.tsx`, `add-countries-form.tsx`, `create-region-form.tsx`, and `region-country-section.tsx` to pass { key: "name" } / { key: "iso_2" }.
- This is enforced by the type system going forward, `DataTableOrderByKey<TData>.key` is typed as keyof TData, so any key that isn't a real `StaticCountry` field (like the old "code") fails to compile.
**Testing** — How have these changes been tested, or how can the reviewer test the feature?
Added `packages/admin/dashboard/src/routes/regions/common/hooks/__tests__/use-countries.spec.tsx`, covering:
- default sort by name
- descending sort via -name/-iso_2
- ascending sort by iso_2
- an invalid order key throwing
- q filtering
- limit/offset pagination.
Manually verified in the dashboard: sorting by Name and Code (both directions) no longer errors, on all four affected screens (region detail country section, add-countries modal, region-create modal, geo-zone form).
---
## Checklist
Please ensure the following before requesting a review:
- [x] I have added a **changeset** for this PR
- Every non-breaking change should be marked as a **patch**
- To add a changeset, run `yarn changeset` and follow the prompts
- [x] The changes are covered by relevant **tests**
- [x] I have verified the code works as intended locally
- [x] I have linked the related issue(s) if applicable
---
## Additional Context
This is my first PR here so please lmk if something needs to be done differently.
Fixes: #160171 parent 008d415 commit daa9214
7 files changed
Lines changed: 106 additions & 7 deletions
File tree
- .changeset
- packages/admin/dashboard/src/routes
- locations/common/components/geo-zone-form
- regions
- common/hooks
- __tests__
- region-add-countries/components/add-countries-form
- region-create/components/create-region-form
- region-detail/components/region-country-section
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | | - | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| |||
Lines changed: 94 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
| 147 | + | |
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
376 | | - | |
| 376 | + | |
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| |||
0 commit comments