Skip to content

Commit

Permalink
Merge pull request #474 from Fekide/472-bug-cannot-read-properties-of…
Browse files Browse the repository at this point in the history
…-null-reading-__component

fix(direct-translation): errors with empty components
  • Loading branch information
sargreal authored Dec 4, 2024
2 parents 8e5f787 + 4053037 commit f1f86fd
Showing 1 changed file with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'

/**
* @param data The data
* @param {null | object} data The data
* @param allLayoutData The schema of the data
* @param {null | string} component The name of a component or null for the content type
* @returns {{[key: string]: {value: any, type: string}}}
Expand All @@ -19,7 +19,7 @@ export default function flattenEntity(
? allLayoutData.components[component]
: allLayoutData.contentType

if (data.__component) {
if (data?.__component) {
result[`${prefix}__component`] = {
value: data.__component,
type: 'component',
Expand All @@ -38,28 +38,34 @@ export default function flattenEntity(
if (_.has(data, attribute)) {
const value = _.get(data, attribute)

if (attributeData.type === 'component') {
_.assign(
result,
...(attributeData.repeatable
? value.map((c, index) =>
flattenEntity(
c,
allLayoutData,
attributeData.component,
`${prefix}${attribute}.${index}.`,
index
)
if (_.isEmpty(value)) {
// If the translated value is empty, it must be passed directly so it can be reset
result[prefix + attribute] = { value, type: attributeData.type }
} else if (attributeData.type === 'component') {
if (attributeData.repeatable) {
_.assign(
result,
...value.map((c, index) =>
flattenEntity(
c,
allLayoutData,
attributeData.component,
`${prefix}${attribute}.${index}.`,
index
)
: [
flattenEntity(
value,
allLayoutData,
attributeData.component,
prefix + attribute + '.'
),
])
)
)
)
} else {
_.assign(
result,
flattenEntity(
value,
allLayoutData,
attributeData.component,
prefix + attribute + '.'
)
)
}
} else if (attributeData.type === 'dynamiczone' && Array.isArray(value)) {
_.assign(
result,
Expand Down

0 comments on commit f1f86fd

Please sign in to comment.