From d1c92069b5ec838585278857cffb4152d1bbb1c9 Mon Sep 17 00:00:00 2001 From: Steven Thompson <44806974+thompsonsj@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:50:04 +0000 Subject: [PATCH] fix(translations): faulty logic to lookup lexical block files (#215) * fix(translations): faulty logic for retrieving lex block files * test(lexicaleditor): refelect less api calls due to fixed logic * refactor(helpers): remove by parent functions --- ...al-editor-with-blocks-inside-array.test.ts | 39 ++++++---- plugin/src/lib/api/helpers.ts | 73 +++++-------------- .../api/payload-crowdin-sync/translations.ts | 40 ++++++---- 3 files changed, 68 insertions(+), 84 deletions(-) diff --git a/dev/src/lib/tests/collections/lexical-editor-with-blocks-inside-array.test.ts b/dev/src/lib/tests/collections/lexical-editor-with-blocks-inside-array.test.ts index d803444..e060b40 100644 --- a/dev/src/lib/tests/collections/lexical-editor-with-blocks-inside-array.test.ts +++ b/dev/src/lib/tests/collections/lexical-editor-with-blocks-inside-array.test.ts @@ -13,7 +13,8 @@ import { } from '../../payload-types'; import { getFilesByDocumentID, - getFilesByParent, + getFiles, + getLexicalFieldArticleDirectory, } from 'plugin/src/lib/api/helpers'; import { getRelationshipId } from 'plugin/src/lib/utilities/payload'; @@ -752,8 +753,21 @@ describe('Lexical editor with multiple blocks', () => { }, })) as any; - const files = await getFilesByParent( - `${getRelationshipId(doc.crowdinArticleDirectory)}`, + const arrayIds = + (doc.items || []).map((item) => item.id) || ([] as string[]); + const blockIds = + (doc.items || []).map( + (item) => (item.block || []).find((x) => x !== undefined)?.id + ) || ([] as string[]); + + const lexicalFieldCrowdinArticleDirectory = await getLexicalFieldArticleDirectory({ + payload, + parent: doc.crowdinArticleDirectory, + name: `lex.items.${arrayIds[1]}.block.${blockIds[1]}.basicBlockLexical.content` + }) + + const files = await getFiles( + `${getRelationshipId(lexicalFieldCrowdinArticleDirectory)}`, payload ); @@ -938,8 +952,6 @@ describe('Lexical editor with multiple blocks', () => { targetLanguageId: 'fr', } ) - // TODO: figure out why twice? - .twice() .reply( 200, mockClient.buildProjectFileTranslation({ @@ -953,8 +965,6 @@ describe('Lexical editor with multiple blocks', () => { pluginOptions.projectId }/translations/builds/${48313}/download` ) - // TODO: figure out why twice? - .twice() .query({ targetLanguageId: 'fr', }) @@ -978,8 +988,6 @@ describe('Lexical editor with multiple blocks', () => { targetLanguageId: 'fr', } ) - // TODO: figure out why twice? - .twice() .reply( 200, mockClient.buildProjectFileTranslation({ @@ -993,8 +1001,6 @@ describe('Lexical editor with multiple blocks', () => { pluginOptions.projectId }/translations/builds/${48314}/download` ) - // TODO: figure out why twice? - .twice() .query({ targetLanguageId: 'fr', }) @@ -1033,9 +1039,14 @@ describe('Lexical editor with multiple blocks', () => { ); const crowdinFiles = await getFilesByDocumentID(`${doc.id}`, payload); - const contentCrowdinFiles = await getFilesByParent( - `${getRelationshipId(updatedDoc.crowdinArticleDirectory)}`, - payload + const lexicalFieldCrowdinArticleDirectory = await getLexicalFieldArticleDirectory({ + payload, + parent: updatedDoc.crowdinArticleDirectory, + name: `lex.items.${arrayIds[1]}.block.${blockIds[1]}.basicBlockLexical.content` + }) + const contentCrowdinFiles = await getFiles( + `${getRelationshipId(lexicalFieldCrowdinArticleDirectory)}`, + payload ); // check file ids are always mapped in the same way diff --git a/plugin/src/lib/api/helpers.ts b/plugin/src/lib/api/helpers.ts index 7c9de58..4045020 100644 --- a/plugin/src/lib/api/helpers.ts +++ b/plugin/src/lib/api/helpers.ts @@ -66,39 +66,30 @@ export async function getArticleDirectory( : undefined; } -export async function getFile( +export async function getLexicalFieldArticleDirectory({ + payload, + parent, + name, +}: { + payload: Payload, + parent?: CrowdinArticleDirectory | null | string, name: string, - crowdinArticleDirectoryId: string, - payload: Payload -): Promise { - const result = await payload.find({ - collection: "crowdin-files", - where: { - field: { equals: name }, - crowdinArticleDirectory: { - equals: crowdinArticleDirectoryId, - }, - }, - }); - return result.docs[0]; +}) { + const dir = await getArticleDirectory( + /** 'document id' is the field name in dot notation for lexical blocks */ + name, + payload, + false, + parent, + ) as any + return dir as CrowdinArticleDirectory } -export async function getFileByParent( +export async function getFile( name: string, - parentCrowdinArticleDirectoryId: string, + crowdinArticleDirectoryId: string, payload: Payload ): Promise { - const dirResult = await payload.find({ - collection: "crowdin-article-directories", - where: { - parent: { - equals: parentCrowdinArticleDirectoryId, - }, - }, - }); - const crowdinArticleDirectory = dirResult.docs[0] - const crowdinArticleDirectoryId = getRelationshipId(crowdinArticleDirectory as any) - const result = await payload.find({ collection: "crowdin-files", where: { @@ -108,41 +99,13 @@ export async function getFileByParent( }, }, }); - return result.docs[0]; } export async function getFiles( crowdinArticleDirectoryId: string, payload: Payload -): Promise { - const result = await payload.find({ - collection: "crowdin-files", - limit: 10000, - where: { - crowdinArticleDirectory: { - equals: crowdinArticleDirectoryId, - }, - }, - }); - return result.docs; -} - -export async function getFilesByParent( - parentCrowdinArticleDirectoryId: string, - payload: Payload ): Promise { - const dirResult = await payload.find({ - collection: "crowdin-article-directories", - where: { - parent: { - equals: parentCrowdinArticleDirectoryId, - }, - }, - }); - const crowdinArticleDirectory = dirResult.docs[0] - const crowdinArticleDirectoryId = getRelationshipId(crowdinArticleDirectory as any) - const result = await payload.find({ collection: "crowdin-files", limit: 10000, diff --git a/plugin/src/lib/api/payload-crowdin-sync/translations.ts b/plugin/src/lib/api/payload-crowdin-sync/translations.ts index 410be11..4b98c6b 100644 --- a/plugin/src/lib/api/payload-crowdin-sync/translations.ts +++ b/plugin/src/lib/api/payload-crowdin-sync/translations.ts @@ -28,7 +28,7 @@ import { } from '../../utilities/richTextConversion' import { Config, CrowdinFile } from "../../payload-types"; -import { getCollectionConfig, getFileByDocumentID, getFileByParent, getFilesByDocumentID, getFilesByParent } from "../helpers"; +import { getCollectionConfig, getFile, getFileByDocumentID, getFiles, getFilesByDocumentID, getLexicalFieldArticleDirectory } from "../helpers"; import { getLexicalBlockFields, getLexicalEditorConfig } from "../../utilities/lexical"; import { getRelationshipId } from "../../utilities/payload"; @@ -53,7 +53,7 @@ interface IgetTranslation { global?: boolean; collection?: CollectionConfig | GlobalConfig; /** Pass crowdinArticleDirectoryId to retrieve `crowdin-files` documents with a parent (i.e. Lexical field blocks) */ - parentCrowdinArticleDirectoryId?: string; + crowdinArticleDirectoryId?: string; fields?: Field[] } @@ -358,7 +358,7 @@ export class payloadCrowdinSyncTranslationsApi { } async getHtmlFieldSlugsByArticleDirectory(parentCrowdinArticleDirectoryId?: string): Promise { - const files = await getFilesByParent(`${parentCrowdinArticleDirectoryId}`, this.payload) as CrowdinFile[]; + const files = await getFiles(`${parentCrowdinArticleDirectoryId}`, this.payload) as CrowdinFile[]; const slugs = files .filter((file) => file.type === "html") .map((file) => `${file.field}`) @@ -372,8 +372,8 @@ export class payloadCrowdinSyncTranslationsApi { * * returns all json fields if fieldName is 'fields' */ // TODO refactor out fields override - replace `collection` with `fields` - async getTranslation({ documentId, fieldName, locale, collection, parentCrowdinArticleDirectoryId, fields }: IgetTranslation) { - const file = (typeof parentCrowdinArticleDirectoryId === 'string' ? await getFileByParent(fieldName, parentCrowdinArticleDirectoryId, this.payload) : await getFileByDocumentID(fieldName, `${documentId}`, this.payload)) as CrowdinFile; + async getTranslation({ documentId, fieldName, locale, collection, crowdinArticleDirectoryId, fields }: IgetTranslation) { + const file = (typeof crowdinArticleDirectoryId === 'string' ? await getFile(fieldName, crowdinArticleDirectoryId, this.payload) : await getFileByDocumentID(fieldName, `${documentId}`, this.payload)) as CrowdinFile; // it is possible a file doesn't exist yet - e.g. an article with localized text fields that contains an empty html field. if (!file) { return; @@ -394,19 +394,28 @@ export class payloadCrowdinSyncTranslationsApi { dotNotation: fieldName, fields: allFields, // do not filter out localized fields if lexical block fields - filterLocalizedFields:!(parentCrowdinArticleDirectoryId), + filterLocalizedFields:!(crowdinArticleDirectoryId), }) as RichTextField const editorConfig = getLexicalEditorConfig(field) // isLexical? if (editorConfig) { + // retrieve the article directory created for this lexical block + const lexicalFieldCrowdinArticleDirectory = await getLexicalFieldArticleDirectory({ + payload: this.payload, + parent: file.crowdinArticleDirectory, + name: `lex.${fieldName}`, + }) + const lexicalFieldCrowdinArticleDirectoryId = getRelationshipId(lexicalFieldCrowdinArticleDirectory as any) + // get translations here and pass it to convertHtmlToLexical // easier than passing `payload` and `pluginOptions` to create a new instance of the class we are in right now - keep convertHtmlToLexical focussed. const blockConfig = getLexicalBlockFields(editorConfig) - const blockTranslations = blockConfig ? await this.getBlockTranslations({ + const blockTranslations = blockConfig && lexicalFieldCrowdinArticleDirectoryId ? await this.getBlockTranslations({ blockConfig, file, - locale + locale, + crowdinArticleDirectoryId: lexicalFieldCrowdinArticleDirectoryId, }) : null return convertHtmlToLexical(data, editorConfig, blockTranslations) || { @@ -452,7 +461,7 @@ export class payloadCrowdinSyncTranslationsApi { fieldName, locale, collection: collection?.slug, - parentCrowdinArticleDirectoryId, + crowdinArticleDirectoryId, fields }, 'error', @@ -494,14 +503,15 @@ export class payloadCrowdinSyncTranslationsApi { private async getBlockTranslations({ blockConfig, - file, - locale + locale, + crowdinArticleDirectoryId, }: { blockConfig: { blocks: Block[] }, file: CrowdinFile, - locale: string + locale: string, + crowdinArticleDirectoryId: string, }) { // link with plugin/src/lib/api/payload-crowdin-sync/files/document.ts - store as variable? const fieldName = `blocks` @@ -522,11 +532,11 @@ export class payloadCrowdinSyncTranslationsApi { documentId: fieldName, fieldName: "blocks", locale: locale, - parentCrowdinArticleDirectoryId: getRelationshipId(file.crowdinArticleDirectory), + crowdinArticleDirectoryId, })) || {}; // add html fields - const localizedHtmlFields = await this.getHtmlFieldSlugsByArticleDirectory(getRelationshipId(file.crowdinArticleDirectory)); + const localizedHtmlFields = await this.getHtmlFieldSlugsByArticleDirectory(crowdinArticleDirectoryId); const crowdinHtmlObject: CrowdinHtmlObject = {}; for (const field of localizedHtmlFields) { // need to get the field definition here somehow? @@ -535,7 +545,7 @@ export class payloadCrowdinSyncTranslationsApi { fieldName: field, locale: locale, fields, - parentCrowdinArticleDirectoryId: getRelationshipId(file.crowdinArticleDirectory), + crowdinArticleDirectoryId, }); }