Skip to content

Commit

Permalink
fix(getlocalizedfields): relax tab field detection (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 21, 2023
1 parent 0af9a90 commit 29ecd1a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/api/payload-crowdin-sync/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class payloadCrowdInSyncFilesApi {
name: crowdInFile.data.name,
type: crowdInFile.data.type,
path: crowdInFile.data.path,
...(fileType === "json" && { fileData: { json: value } }),
//...(fileType === "json" && { fileData: { json: value } }),
...(fileType === "html" && { fileData: { html: value } }),
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ export const getLocalizedFields = ({
// flatten the presetational only tabs field
// TODO: check this is correct. Docs at https://payloadcms.com/docs/fields/tabs suggest that subsequent tabs can be accessed using a prefix, but documents saved with tabs seem to retain their structure as if they didn't have tabs.
let flattenedFields = fields;
if (fields.length === 1 && fields[0].type === "tabs") {
if (fields.length > 1 && fields[0].type === "tabs") {
flattenedFields = fields[0].tabs.map((tab) => tab.fields).flat();
}
const localizedFields = getLocalizedFieldsRecursive({
fields: flattenedFields,
type,
localizedParent,
});
const allLocalizedFields = type ? getLocalizedFields({ fields: fields }) : fields;
if (
localizedFields.length === 1 &&
allLocalizedFields.length === 1 &&
get(localizedFields[0], "name") === "meta"
) {
return [];
Expand Down
110 changes: 110 additions & 0 deletions src/utilities/tests/buildJsonCrowdinObject/group-field-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,114 @@ describe("fn: buildCrowdinJsonObject: group field type", () => {
expected,
);
});

it("includes localized fields nested in groups nested in a group nested in a collapsible field", () => {
const doc = {
id: "638641358b1a140462752076",
title: "Test Policy created with title",
groupField: {
nestedGroupField: fieldDocValue,
secondNestedGroupField: fieldDocValue,
},
status: "draft",
createdAt: "2022-11-29T17:28:21.644Z",
updatedAt: "2022-11-29T17:28:21.644Z",
};
const fields: Field[] = [
{
name: "title",
type: "text",
localized: true,
},
{
label: "collapsibleField",
type: "collapsible",
fields: [
{
name: "groupField",
type: "group",
fields: [
{
name: "nestedGroupField",
type: "group",
fields: basicLocalizedFields,
},
{
name: "secondNestedGroupField",
type: "group",
fields: basicLocalizedFields,
},
],
},
],
},
];
const localizedFields = getLocalizedFields({ fields });
const expected = {
title: "Test Policy created with title",
groupField: {
nestedGroupField: fieldJsonCrowdinObject(),
secondNestedGroupField: fieldJsonCrowdinObject(),
},
};
expect(buildCrowdinJsonObject({ doc, fields: localizedFields })).toEqual(
expected,
);
});

it("includes localized fields nested in groups nested in a group nested in a collapsible field with top-level localization settings", () => {
const doc = {
id: "638641358b1a140462752076",
title: "Test Policy created with title",
groupField: {
nestedGroupField: fieldDocValue,
secondNestedGroupField: fieldDocValue,
},
status: "draft",
createdAt: "2022-11-29T17:28:21.644Z",
updatedAt: "2022-11-29T17:28:21.644Z",
};
const fields: Field[] = [
{
name: "title",
type: "text",
localized: true,
},
{
label: "collapsibleField",
type: "collapsible",
fields: [
{
name: "groupField",
type: "group",
fields: [
{
name: "nestedGroupField",
type: "group",
localized: true,
fields: basicNonLocalizedFields,
},
{
name: "secondNestedGroupField",
type: "group",
localized: true,
fields: basicNonLocalizedFields,
},
],
},
],
},
];
const localizedFields = getLocalizedFields({ fields });
const expected = {
title: "Test Policy created with title",
groupField: {
nestedGroupField: fieldJsonCrowdinObject(),
secondNestedGroupField: fieldJsonCrowdinObject(),
},
};
expect(buildCrowdinJsonObject({ doc, fields: localizedFields })).toEqual(
expected,
);
});
});

0 comments on commit 29ecd1a

Please sign in to comment.