Skip to content

Commit

Permalink
fix(translations): faulty logic to lookup lexical block files (#215)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
thompsonsj authored Oct 28, 2024
1 parent b56076a commit d1c9206
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -938,8 +952,6 @@ describe('Lexical editor with multiple blocks', () => {
targetLanguageId: 'fr',
}
)
// TODO: figure out why twice?
.twice()
.reply(
200,
mockClient.buildProjectFileTranslation({
Expand All @@ -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',
})
Expand All @@ -978,8 +988,6 @@ describe('Lexical editor with multiple blocks', () => {
targetLanguageId: 'fr',
}
)
// TODO: figure out why twice?
.twice()
.reply(
200,
mockClient.buildProjectFileTranslation({
Expand All @@ -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',
})
Expand Down Expand Up @@ -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
Expand Down
73 changes: 18 additions & 55 deletions plugin/src/lib/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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<any> {
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: {
Expand All @@ -108,41 +99,13 @@ export async function getFileByParent(
},
},
});

return result.docs[0];
}

export async function getFiles(
crowdinArticleDirectoryId: string,
payload: Payload
): Promise<any> {
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<CrowdinFile[]> {
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,
Expand Down
40 changes: 25 additions & 15 deletions plugin/src/lib/api/payload-crowdin-sync/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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[]
}

Expand Down Expand Up @@ -358,7 +358,7 @@ export class payloadCrowdinSyncTranslationsApi {
}

async getHtmlFieldSlugsByArticleDirectory(parentCrowdinArticleDirectoryId?: string): Promise<string[]> {
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}`)
Expand All @@ -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;
Expand All @@ -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) || {
Expand Down Expand Up @@ -452,7 +461,7 @@ export class payloadCrowdinSyncTranslationsApi {
fieldName,
locale,
collection: collection?.slug,
parentCrowdinArticleDirectoryId,
crowdinArticleDirectoryId,
fields
},
'error',
Expand Down Expand Up @@ -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`
Expand All @@ -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?
Expand All @@ -535,7 +545,7 @@ export class payloadCrowdinSyncTranslationsApi {
fieldName: field,
locale: locale,
fields,
parentCrowdinArticleDirectoryId: getRelationshipId(file.crowdinArticleDirectory),
crowdinArticleDirectoryId,
});
}

Expand Down

0 comments on commit d1c9206

Please sign in to comment.