Skip to content

Commit 077fb3a

Browse files
authored
fix(ui): respect locale in buildTableState (#11147)
### What? This PR fixes an issue where the `join` field table was not respecting the locale selected in the admin ui localizer. This also introduces an e2e test to the existing suite to catch this issue. ### Why? To properly render `join` field table data according to chosen and configured locales. ### How? Threading `req.locale` through to the `payload.find` call in `buildTableState`. Fixes #11134 Before: [Editing---Category--join-locales-before-Payload.webm](https://github.com/user-attachments/assets/d77b71bb-f849-4be2-aa96-26dbfedb52d4) After: [Editing---Category--join-locales-after-Payload.webm](https://github.com/user-attachments/assets/0d1f7351-adf4-4bad-ac82-0fee67f8b66a)
1 parent b65ae07 commit 077fb3a

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

packages/ui/src/utilities/buildTableState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const buildTableState = async (
156156
collection: collectionSlug,
157157
depth: 0,
158158
limit: query?.limit ? parseInt(query.limit, 10) : undefined,
159+
locale: req.locale,
159160
overrideAccess: false,
160161
page: query?.page ? parseInt(query.page, 10) : undefined,
161162
sort: query?.sort,

test/joins/collections/Posts.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ export const Posts: CollectionConfig = {
66
slug: postsSlug,
77
admin: {
88
useAsTitle: 'title',
9-
defaultColumns: ['title', 'category', 'updatedAt', 'createdAt'],
9+
defaultColumns: ['title', 'localizedText', 'category', 'updatedAt', 'createdAt'],
1010
},
1111
fields: [
1212
{
1313
name: 'title',
1414
type: 'text',
1515
},
16+
{
17+
name: 'localizedText',
18+
type: 'text',
19+
localized: true,
20+
},
1621
{
1722
name: 'author',
1823
type: 'relationship',

test/joins/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ export default buildConfigWithDefaults({
222222
},
223223
],
224224
localization: {
225-
locales: ['en', 'es'],
225+
locales: [
226+
{ label: '(en)', code: 'en' },
227+
{ label: '(es)', code: 'es' },
228+
],
226229
defaultLocale: 'en',
227230
},
228231
onInit: async (payload) => {

test/joins/e2e.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { PayloadTestSDK } from '../helpers/sdk/index.js'
99
import type { Config } from './payload-types.js'
1010

1111
import {
12+
changeLocale,
1213
ensureCompilationIsDone,
1314
exactText,
1415
initPageConsoleErrorCatch,
@@ -483,4 +484,20 @@ describe('Join Field', () => {
483484

484485
await expect(page.locator('#field-joinWithError')).toContainText('enableErrorOnJoin is true')
485486
})
487+
488+
test('should render localized data in table when locale changes', async () => {
489+
await page.goto(categoriesURL.edit(categoryID))
490+
const joinField = page.locator('#field-relatedPosts.field-type.join')
491+
await expect(joinField).toBeVisible()
492+
await expect(joinField.locator('.relationship-table table')).toBeVisible()
493+
494+
const row = joinField.locator('.relationship-table tbody tr.row-1')
495+
await expect(row).toBeVisible()
496+
const localizedTextCell = row.locator('.cell-localizedText span')
497+
await expect(localizedTextCell).toBeVisible()
498+
await expect(localizedTextCell).toHaveText('Text in en')
499+
500+
await changeLocale(page, 'es')
501+
await expect(localizedTextCell).toHaveText('Text in es')
502+
})
486503
})

test/joins/payload-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export interface User {
164164
export interface Post {
165165
id: string;
166166
title?: string | null;
167+
localizedText?: string | null;
167168
author?: (string | null) | User;
168169
/**
169170
* Hides posts for the `filtered` join field in categories
@@ -668,6 +669,7 @@ export interface UsersSelect<T extends boolean = true> {
668669
*/
669670
export interface PostsSelect<T extends boolean = true> {
670671
title?: T;
672+
localizedText?: T;
671673
author?: T;
672674
isFiltered?: T;
673675
restrictedField?: T;

test/joins/seed.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,67 @@ export const seed = async (_payload: Payload) => {
4444
},
4545
})
4646

47-
await _payload.create({
47+
const post1 = await _payload.create({
4848
collection: postsSlug,
4949
data: {
5050
category: category.id,
5151
group: {
5252
category: category.id,
5353
},
5454
title: 'Test Post 1',
55+
localizedText: 'Text in en',
5556
},
5657
})
5758

58-
await _payload.create({
59+
const post2 = await _payload.create({
5960
collection: postsSlug,
6061
data: {
6162
category: category.id,
6263
group: {
6364
category: category.id,
6465
},
6566
title: 'Test Post 2',
67+
localizedText: 'Text in en',
6668
},
6769
})
6870

69-
await _payload.create({
71+
const post3 = await _payload.create({
7072
collection: postsSlug,
7173
data: {
7274
category: category.id,
7375
group: {
7476
category: category.id,
7577
},
7678
title: 'Test Post 3',
79+
localizedText: 'Text in en',
80+
},
81+
})
82+
83+
await _payload.update({
84+
collection: postsSlug,
85+
id: post1.id,
86+
data: {
87+
localizedText: 'Text in es',
88+
},
89+
locale: 'es',
90+
})
91+
92+
await _payload.update({
93+
collection: postsSlug,
94+
id: post2.id,
95+
data: {
96+
localizedText: 'Text in es',
97+
},
98+
locale: 'es',
99+
})
100+
101+
await _payload.update({
102+
collection: postsSlug,
103+
id: post3.id,
104+
data: {
105+
localizedText: 'Text in es',
77106
},
107+
locale: 'es',
78108
})
79109

80110
// create an upload with image.png

0 commit comments

Comments
 (0)