Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate documents with required localized fields #4236

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,41 @@ const Duplicate: React.FC<Props> = ({ id, collection, slug }) => {
if (result.status === 201 || result.status === 200) {
return json.doc.id
}
json.errors.forEach((error) => toast.error(error.message))

// only show the error if this is the initial request failing
if (!duplicateID) {
json.errors.forEach((error) => toast.error(error.message))
}
return null
}

let duplicateID
let duplicateID: string
let abort = false
const localeErrors = []

if (localization) {
await localization.localeCodes.reduce(async (priorLocalePatch, locale) => {
await priorLocalePatch
if (abort) return
duplicateID = await saveDocument({ id, duplicateID, locale })
const localeResult = await saveDocument({
id,
duplicateID,
locale,
})
duplicateID = localeResult || duplicateID
if (duplicateID && !localeResult) {
localeErrors.push(locale)
}
if (!duplicateID) {
abort = true
}
}, Promise.resolve())

if (abort && duplicateID) {
// delete the duplicate doc to prevent incomplete
await requests.delete(`${serverURL}${api}/${slug}/${duplicateID}`, {
headers: {
'Accept-Language': i18n.language,
},
})
}
} else {
duplicateID = await saveDocument({ id })
}

if (!duplicateID) {
// document was not saved, error toast was displayed
return
}

Expand All @@ -128,6 +133,16 @@ const Duplicate: React.FC<Props> = ({ id, collection, slug }) => {
{ autoClose: 3000 },
)

if (localeErrors.length > 0) {
toast.error(
`
${t('error:localesNotSaved', { count: localeErrors.length })}
${localeErrors.join(', ')}
`,
{ autoClose: 5000 },
)
}

setModified(false)

setTimeout(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/payload/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"invalidFileType": "Invalid file type",
"invalidFileTypeValue": "Invalid file type: {{value}}",
"loadingDocument": "There was a problem loading the document with ID of {{id}}.",
"localesNotSaved_one": "The following locale could not be saved:",
"localesNotSaved_other": "The following locales could not be saved:",
"missingEmail": "Missing email.",
"missingIDOfDocument": "Missing ID of document to update.",
"missingIDOfVersion": "Missing ID of version.",
Expand Down
8 changes: 8 additions & 0 deletions packages/payload/src/translations/translation-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@
"loadingDocument": {
"type": "string"
},
"localesNotSaved_one": {
"type": "string"
},
"localesNotSaved_other": {
"type": "string"
},
"missingEmail": {
"type": "string"
},
Expand Down Expand Up @@ -348,6 +354,8 @@
"invalidFileType",
"invalidFileTypeValue",
"loadingDocument",
"localesNotSaved_one",
"localesNotSaved_other",
"missingEmail",
"missingIDOfDocument",
"missingIDOfVersion",
Expand Down
53 changes: 41 additions & 12 deletions test/localization/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
} from '../helpers'
import { AdminUrlUtil } from '../helpers/adminUrlUtil'
import { initPayloadTest } from '../helpers/configHelpers'
import { englishTitle, localizedPostsSlug, spanishLocale } from './shared'
import {
englishTitle,
localizedPostsSlug,
spanishLocale,
withRequiredLocalizedFields,
} from './shared'

/**
* TODO: Localization
Expand All @@ -27,6 +32,7 @@ import { englishTitle, localizedPostsSlug, spanishLocale } from './shared'

const { beforeAll, describe } = test
let url: AdminUrlUtil
let urlWithRequiredLocalizedFields: AdminUrlUtil

const defaultLocale = 'en'
const title = 'english title'
Expand All @@ -46,6 +52,7 @@ describe('Localization', () => {
})

url = new AdminUrlUtil(serverURL, localizedPostsSlug)
urlWithRequiredLocalizedFields = new AdminUrlUtil(serverURL, withRequiredLocalizedFields)

const context = await browser.newContext()
page = await context.newPage()
Expand All @@ -57,7 +64,7 @@ describe('Localization', () => {
test('create english post, switch to spanish', async () => {
await page.goto(url.create)

await fillValues({ title, description })
await fillValues({ description, title })
await saveDocAndAssert(page)

// Change back to English
Expand All @@ -67,7 +74,7 @@ describe('Localization', () => {
await expect(page.locator('#field-title')).toBeEmpty()
await expect(page.locator('#field-description')).toHaveValue(description)

await fillValues({ title: spanishTitle, description })
await fillValues({ description, title: spanishTitle })
await saveDocAndAssert(page)
await changeLocale(page, defaultLocale)

Expand All @@ -84,7 +91,7 @@ describe('Localization', () => {
// Change to Spanish
await changeLocale(page, newLocale)

await fillValues({ title: spanishTitle, description })
await fillValues({ description, title: spanishTitle })
await saveDocAndAssert(page)

// Change back to English
Expand All @@ -96,7 +103,7 @@ describe('Localization', () => {

// Add English

await fillValues({ title, description })
await fillValues({ description, title })
await saveDocAndAssert(page)
await saveDocAndAssert(page)

Expand All @@ -112,7 +119,7 @@ describe('Localization', () => {
// Change to Arabic
await changeLocale(page, newLocale)

await fillValues({ title: arabicTitle, description })
await fillValues({ description, title: arabicTitle })
await saveDocAndAssert(page)

// Change back to English
Expand All @@ -124,7 +131,7 @@ describe('Localization', () => {

// Add English

await fillValues({ title, description })
await fillValues({ description, title })
await saveDocAndAssert(page)
await saveDocAndAssert(page)

Expand All @@ -138,21 +145,21 @@ describe('Localization', () => {
const localizedPost = await payload.create({
collection: localizedPostsSlug,
data: {
title: englishTitle,
localizedCheckbox: true,
title: englishTitle,
},
locale: defaultLocale,
})

const id = localizedPost.id.toString()

await payload.update({
collection: localizedPostsSlug,
id,
locale: spanishLocale,
collection: localizedPostsSlug,
data: {
title: spanishTitle,
},
locale: spanishLocale,
})

await page.goto(url.edit(id))
Expand All @@ -175,7 +182,7 @@ describe('Localization', () => {
test('should duplicate localized checkbox correctly', async () => {
await page.goto(url.create)
await changeLocale(page, defaultLocale)
await fillValues({ title: englishTitle, description })
await fillValues({ description, title: englishTitle })
await page.locator('#field-localizedCheckbox').click()

await page.locator('#action-save').click()
Expand All @@ -198,11 +205,33 @@ describe('Localization', () => {
await changeLocale(page, spanishLocale)
await expect(page.locator('#field-localizedCheckbox')).not.toBeChecked()
})

test('should duplicate even if missing some localized data', async () => {
// create a localized required doc
await page.goto(urlWithRequiredLocalizedFields.create)
await changeLocale(page, defaultLocale)
await page.locator('#field-title').fill(englishTitle)
await page.locator('#field-layout .blocks-field__drawer-toggler').click()
await page.locator('button[title="Text"]').click()
await page.fill('#field-layout__0__text', 'test')
await saveDocAndAssert(page)

const originalDocURL = page.url()
// duplicate
await openDocControls(page)
await page.locator('#action-duplicate').click()
await page.locator('#action-save').click()

// verify that the locale did copy
await expect(page.locator('#field-title')).toHaveValue(englishTitle)
// expect that the document has a new id
expect(page.url()).not.toStrictEqual(originalDocURL)
})
})
})

async function fillValues(data: Partial<LocalizedPost>) {
const { title: titleVal, description: descVal } = data
const { description: descVal, title: titleVal } = data

if (titleVal) await page.locator('#field-title').fill(titleVal)
if (descVal) await page.locator('#field-description').fill(descVal)
Expand Down
Loading