Skip to content

Commit

Permalink
fix(ui): relationship edit drawers now respect current locale (#10262)
Browse files Browse the repository at this point in the history
Fixes #9979
  • Loading branch information
jmikrut authored Dec 30, 2024
1 parent f5a955d commit 6dac4c5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/next/src/views/Document/handleServerFunction.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { Data, DocumentPreferences, FormState, PayloadRequest, VisibleEntities } from 'payload'
import type {
Data,
DocumentPreferences,
FormState,
Locale,
PayloadRequest,
VisibleEntities,
} from 'payload'

import { getClientConfig } from '@payloadcms/ui/utilities/getClientConfig'
import { headers as getHeaders } from 'next/headers.js'
Expand All @@ -19,6 +26,7 @@ export const renderDocumentHandler = async (args: {
drawerSlug?: string
initialData?: Data
initialState?: FormState
locale?: Locale
overrideEntityVisibility?: boolean
redirectAfterDelete: boolean
redirectAfterDuplicate: boolean
Expand All @@ -30,6 +38,7 @@ export const renderDocumentHandler = async (args: {
docID,
drawerSlug,
initialData,
locale,
overrideEntityVisibility,
redirectAfterDelete,
redirectAfterDuplicate,
Expand Down Expand Up @@ -145,6 +154,7 @@ export const renderDocumentHandler = async (args: {
docID,
globalConfig: payload.config.globals.find((global) => global.slug === collectionSlug),
languageOptions: undefined, // TODO
locale,
permissions,
req,
translations: undefined, // TODO
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/elements/DocumentDrawer/DrawerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { DocumentDrawerProps } from './types.js'

import { LoadingOverlay } from '../../elements/Loading/index.js'
import { useConfig } from '../../providers/Config/index.js'
import { useLocale } from '../../providers/Locale/index.js'
import { useServerFunctions } from '../../providers/ServerFunctions/index.js'
import { useTranslation } from '../../providers/Translation/index.js'
import { abortAndIgnore, handleAbortRef } from '../../utilities/abortAndIgnore.js'
Expand All @@ -32,6 +33,7 @@ export const DocumentDrawerContent: React.FC<DocumentDrawerProps> = ({
const {
config: { collections },
} = useConfig()
const locale = useLocale()

const [collectionConfig] = useState(() =>
collections.find((collection) => collection.slug === collectionSlug),
Expand Down Expand Up @@ -62,6 +64,7 @@ export const DocumentDrawerContent: React.FC<DocumentDrawerProps> = ({
docID,
drawerSlug,
initialData,
locale,
overrideEntityVisibility,
redirectAfterDelete: redirectAfterDelete !== undefined ? redirectAfterDelete : false,
redirectAfterDuplicate:
Expand Down Expand Up @@ -95,6 +98,7 @@ export const DocumentDrawerContent: React.FC<DocumentDrawerProps> = ({
closeModal,
overrideEntityVisibility,
t,
locale,
],
)

Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/providers/ServerFunctions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
BuildTableStateArgs,
Data,
DocumentSlots,
Locale,
ServerFunctionClient,
} from 'payload'

Expand Down Expand Up @@ -40,6 +41,7 @@ type RenderDocument = (args: {
docID?: number | string
drawerSlug?: string
initialData?: Data
locale?: Locale
overrideEntityVisibility?: boolean
redirectAfterDelete?: boolean
redirectAfterDuplicate?: boolean
Expand Down
11 changes: 10 additions & 1 deletion test/localization/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export default buildConfigWithDefaults({
relationship: localizedRelation.id,
},
})
await payload.create({
const relationshipLocalized = await payload.create({
collection: relationshipLocalizedSlug,
data: {
arrayField: [
Expand All @@ -543,6 +543,15 @@ export default buildConfigWithDefaults({
locale: 'en',
})

await payload.update({
collection: relationshipLocalizedSlug,
id: relationshipLocalized.id,
data: {
relationMultiRelationTo: { relationTo: collection, value: localizedPost.id },
},
locale: 'es',
})

console.log('SEED 5')

const globalArray = await payload.updateGlobal({
Expand Down
21 changes: 21 additions & 0 deletions test/localization/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
changeLocale,
ensureCompilationIsDone,
initPageConsoleErrorCatch,
openDocDrawer,
saveDocAndAssert,
} from '../helpers.js'
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
Expand All @@ -23,6 +24,7 @@ import {
defaultLocale,
englishTitle,
localizedPostsSlug,
relationshipLocalizedSlug,
spanishLocale,
withRequiredLocalizedFields,
} from './shared.js'
Expand All @@ -41,6 +43,7 @@ const dirname = path.dirname(filename)
const { beforeAll, describe } = test
let url: AdminUrlUtil
let urlWithRequiredLocalizedFields: AdminUrlUtil
let urlRelationshipLocalized: AdminUrlUtil

const title = 'english title'
const spanishTitle = 'spanish title'
Expand All @@ -58,6 +61,7 @@ describe('Localization', () => {
;({ payload, serverURL } = await initPayloadE2ENoConfig<Config>({ dirname }))

url = new AdminUrlUtil(serverURL, localizedPostsSlug)
urlRelationshipLocalized = new AdminUrlUtil(serverURL, relationshipLocalizedSlug)
richTextURL = new AdminUrlUtil(serverURL, richTextSlug)
urlWithRequiredLocalizedFields = new AdminUrlUtil(serverURL, withRequiredLocalizedFields)

Expand Down Expand Up @@ -253,6 +257,23 @@ describe('Localization', () => {

await expect(page.locator('#field-children .rs__menu')).toContainText('spanish-relation2')
})

test('ensure relationship edit drawers are opened in currently selected locale', async () => {
await page.goto(urlRelationshipLocalized.list)
await changeLocale(page, spanishLocale)

const post = page.locator('.cell-id a').first()
const postUrl = await post.getAttribute('href')
await page.goto(serverURL + postUrl)
await page.waitForURL(serverURL + postUrl)

await openDocDrawer(
page,
'#field-relationMultiRelationTo .relationship--single-value__drawer-toggler',
)

await expect(page.locator('.doc-drawer__header-text')).toContainText('spanish-relation2')
})
})

describe('copy localized data', () => {
Expand Down

0 comments on commit 6dac4c5

Please sign in to comment.