diff --git a/.vscode/launch.json b/.vscode/launch.json index 75239e61a4..7074612e85 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,7 @@ "--runInBand" ], "env": { - "PAYLOAD_CONFIG_PATH": "demo/payload.config.js" + "PAYLOAD_CONFIG_PATH": "demo/payload.config.ts" }, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", diff --git a/demo/collections/Localized.ts b/demo/collections/Localized.ts index 50a8b27952..b9b28b90dc 100644 --- a/demo/collections/Localized.ts +++ b/demo/collections/Localized.ts @@ -2,9 +2,9 @@ import { PayloadCollectionConfig } from '../../src/collections/config/types'; import { Block } from '../../src/fields/config/types'; const RichTextBlock: Block = { - slug: 'test', + slug: 'richTextBlock', labels: { - singular: 'Rich Text Test', + singular: 'Rich Text Block', plural: 'Rich Text Blocks', }, fields: [ diff --git a/src/collections/operations/update.ts b/src/collections/operations/update.ts index 71dc5f5032..87066f54cc 100644 --- a/src/collections/operations/update.ts +++ b/src/collections/operations/update.ts @@ -104,7 +104,7 @@ async function update(incomingArgs: Arguments): Promise { if (!doc && hasWherePolicy) throw new Forbidden(); if (locale && doc.setLocale) { - doc.setLocale(locale, fallbackLocale); + doc.setLocale(locale, null); } let originalDoc: Document = doc.toJSON({ virtuals: true }); @@ -250,6 +250,10 @@ async function update(incomingArgs: Arguments): Promise { await doc.save(); + if (locale && doc.setLocale) { + doc.setLocale(locale, fallbackLocale); + } + let result: Document = doc.toJSON({ virtuals: true }); result = removeInternalFields(result); diff --git a/src/collections/tests/collections.spec.js b/src/collections/tests/collections.spec.js index aaf3b8343b..0e7da2f253 100644 --- a/src/collections/tests/collections.spec.js +++ b/src/collections/tests/collections.spec.js @@ -44,6 +44,26 @@ describe('Collections - REST', () => { title: 'title', description: englishPostDesc, priority: 1, + nonLocalizedGroup: { + text: 'english', + }, + localizedGroup: { + text: 'english', + }, + nonLocalizedArray: [ + { + localizedEmbeddedText: 'english', + }, + ], + richTextBlocks: [ + { + blockType: 'richTextBlock', + blockName: 'Test Block Name', + content: [{ + children: [{ text: 'english' }], + }], + }, + ], }), headers, method: 'post', @@ -54,6 +74,11 @@ describe('Collections - REST', () => { expect(response.status).toBe(201); expect(data.doc.title).not.toBeNull(); expect(data.doc.id).not.toBeNull(); + expect(data.doc.nonLocalizedGroup.text).toStrictEqual('english'); + expect(data.doc.localizedGroup.text).toStrictEqual('english'); + expect(data.doc.nonLocalizedArray[0].localizedEmbeddedText).toStrictEqual('english'); + expect(data.doc.richTextBlocks[0].content[0].children[0].text).toStrictEqual('english'); + const timestampRegex = /^(\d{4})(?:-?W(\d+)(?:-?(\d+)D?)?|(?:-(\d+))?-(\d+))(?:[T ](\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?)?(?:Z(-?\d*))?$/; expect(data.doc.createdAt).toStrictEqual(expect.stringMatching(timestampRegex)); expect(data.doc.updatedAt).toStrictEqual(expect.stringMatching(timestampRegex)); @@ -101,6 +126,26 @@ describe('Collections - REST', () => { title: 'title', description: spanishPostDesc, priority: 1, + nonLocalizedGroup: { + text: 'spanish', + }, + localizedGroup: { + text: 'spanish', + }, + nonLocalizedArray: [ + { + localizedEmbeddedText: 'spanish', + }, + ], + richTextBlocks: [ + { + blockType: 'richTextBlock', + blockName: 'Test Block Name', + content: [{ + children: [{ text: 'spanish' }], + }], + }, + ], }), headers, method: 'put', @@ -110,6 +155,10 @@ describe('Collections - REST', () => { expect(response.status).toBe(200); expect(data.doc.description).toBe(spanishPostDesc); + expect(data.doc.nonLocalizedGroup.text).toStrictEqual('spanish'); + expect(data.doc.localizedGroup.text).toStrictEqual('spanish'); + expect(data.doc.nonLocalizedArray[0].localizedEmbeddedText).toStrictEqual('spanish'); + expect(data.doc.richTextBlocks[0].content[0].children[0].text).toStrictEqual('spanish'); }); }); @@ -121,6 +170,10 @@ describe('Collections - REST', () => { expect(response.status).toBe(200); expect(data.description).toBe(englishPostDesc); + expect(data.nonLocalizedGroup.text).toStrictEqual('english'); + expect(data.localizedGroup.text).toStrictEqual('english'); + expect(data.nonLocalizedArray[0].localizedEmbeddedText).toStrictEqual('english'); + expect(data.richTextBlocks[0].content[0].children[0].text).toStrictEqual('english'); }); it('should allow a localized post to be retrieved in specified English locale', async () => { @@ -129,6 +182,10 @@ describe('Collections - REST', () => { expect(response.status).toBe(200); expect(data.description).toBe(englishPostDesc); + expect(data.nonLocalizedGroup.text).toStrictEqual('english'); + expect(data.localizedGroup.text).toStrictEqual('english'); + expect(data.nonLocalizedArray[0].localizedEmbeddedText).toStrictEqual('english'); + expect(data.richTextBlocks[0].content[0].children[0].text).toStrictEqual('english'); }); it('should allow a localized post to be retrieved in Spanish', async () => { @@ -137,6 +194,10 @@ describe('Collections - REST', () => { expect(response.status).toBe(200); expect(data.description).toBe(spanishPostDesc); + expect(data.nonLocalizedGroup.text).toStrictEqual('spanish'); + expect(data.localizedGroup.text).toStrictEqual('spanish'); + expect(data.nonLocalizedArray[0].localizedEmbeddedText).toStrictEqual('spanish'); + expect(data.richTextBlocks[0].content[0].children[0].text).toStrictEqual('spanish'); }); it('should allow a localized post to be retrieved in all locales', async () => { diff --git a/src/globals/operations/update.ts b/src/globals/operations/update.ts index 246c31d379..bbeeb43644 100644 --- a/src/globals/operations/update.ts +++ b/src/globals/operations/update.ts @@ -1,8 +1,8 @@ import deepmerge from 'deepmerge'; +import merge from 'lodash.merge'; import overwriteMerge from '../../utilities/overwriteMerge'; import executeAccess from '../../auth/executeAccess'; import removeInternalFields from '../../utilities/removeInternalFields'; -import { AfterChangeHook, BeforeValidateHook } from '../../collections/config/types'; async function update(args) { const { globals: { Model } } = this; @@ -39,7 +39,7 @@ async function update(args) { } if (locale && global.setLocale) { - global.setLocale(locale, fallbackLocale); + global.setLocale(locale, null); } const globalJSON = global.toJSON({ virtuals: true }); @@ -100,10 +100,14 @@ async function update(args) { // 7. Perform database operation // ///////////////////////////////////// - Object.assign(global, data); + merge(global, data); await global.save(); + if (locale && global.setLocale) { + global.setLocale(locale, fallbackLocale); + } + global = global.toJSON({ virtuals: true }); // ///////////////////////////////////// diff --git a/src/localization/sanitizeFallbackLocale.ts b/src/localization/sanitizeFallbackLocale.ts index 8bea28cffb..dc71cf7ba7 100644 --- a/src/localization/sanitizeFallbackLocale.ts +++ b/src/localization/sanitizeFallbackLocale.ts @@ -1,5 +1,5 @@ const sanitizeFallbackLocale = (fallbackLocale) => { - if (fallbackLocale === 'null' || fallbackLocale === 'none' || fallbackLocale === 'false' || fallbackLocale === false) { + if (fallbackLocale === 'null' || fallbackLocale === 'none' || fallbackLocale === 'false' || fallbackLocale === false || fallbackLocale === null) { return null; } diff --git a/yarn.lock b/yarn.lock index f353cb3464..c9b025ca36 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8224,7 +8224,7 @@ lodash.memoize@^4.1.2: lodash.merge@^4.6.2: version "4.6.2" - resolved "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" + resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== lodash.once@^4.0.0: @@ -8232,11 +8232,6 @@ lodash.once@^4.0.0: resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac" integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w= -lodash.set@^4.3.2: - version "4.3.2" - resolved "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" - integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM= - lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"