Skip to content

Commit be5c818

Browse files
authored
Merge pull request #531 from breezertwo/fix/batch-translate
2 parents 4abcadf + a7df734 commit be5c818

2 files changed

Lines changed: 31 additions & 34 deletions

File tree

plugin/admin/src/utils/mutateCTBContentTypeSchema.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,34 @@ const localizedPath = ['pluginOptions', 'i18n', 'localized']
66
const translatePath = ['pluginOptions', 'translate', 'translate']
77

88
const addTranslationToFields = (attributes: Schema.ContentType['attributes']) =>
9-
Object.keys(attributes).reduce<Schema.ContentType['attributes']>(
10-
(acc, current) => {
11-
const currentAttribute = attributes[current]
12-
13-
const attributeIsLocalized = get(currentAttribute, localizedPath, true)
14-
15-
const attributeDefaultTranslated =
16-
currentAttribute.type === 'relation' || attributeIsLocalized
17-
18-
if (
19-
attributeDefaultTranslated &&
20-
TRANSLATABLE_FIELDS.includes(currentAttribute.type)
21-
) {
22-
const translate = {
23-
translate: get(
24-
currentAttribute,
25-
translatePath,
26-
attributeDefaultTranslated ? 'translate' : 'copy'
27-
),
28-
}
29-
30-
const pluginOptions = {
31-
...(currentAttribute.pluginOptions ?? {}),
32-
translate,
33-
}
34-
35-
acc[current] = { ...currentAttribute, pluginOptions }
36-
37-
return acc
9+
Object.values(attributes).map((currentAttribute) => {
10+
const attributeIsLocalized = get(currentAttribute, localizedPath, true)
11+
12+
const attributeDefaultTranslated =
13+
currentAttribute.type === 'relation' || attributeIsLocalized
14+
15+
if (
16+
attributeDefaultTranslated &&
17+
TRANSLATABLE_FIELDS.includes(currentAttribute.type)
18+
) {
19+
const translate = {
20+
translate: get(
21+
currentAttribute,
22+
translatePath,
23+
attributeDefaultTranslated ? 'translate' : 'copy'
24+
),
3825
}
3926

40-
acc[current] = currentAttribute
27+
const pluginOptions = {
28+
...(currentAttribute.pluginOptions ?? {}),
29+
translate,
30+
}
31+
32+
return { ...currentAttribute, pluginOptions }
33+
}
4134

42-
return acc
43-
},
44-
{}
45-
)
35+
return currentAttribute
36+
})
4637

4738
type OmitByPath<T extends object, K extends string[]> = Pick<
4839
T,

plugin/server/src/utils/clean-data.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ export function cleanData<
8282
resultData[attr] = mediaFiles.id
8383
}
8484
}
85+
86+
// Remove attribute if it's an empty array
87+
if (Array.isArray(resultData[attr]) && resultData[attr].length === 0) {
88+
delete resultData[attr]
89+
}
8590
})
91+
8692
return resultData as unknown as Utils.If<
8793
ForFrontend,
8894
Modules.Documents.Document<TSchemaUID>,

0 commit comments

Comments
 (0)