diff --git a/packages/core/admin/admin/src/content-manager/components/RelationInputWrapper/utils/normalizeRelations.js b/packages/core/admin/admin/src/content-manager/components/RelationInputWrapper/utils/normalizeRelations.js index 9c13420f5f..a478d181c3 100644 --- a/packages/core/admin/admin/src/content-manager/components/RelationInputWrapper/utils/normalizeRelations.js +++ b/packages/core/admin/admin/src/content-manager/components/RelationInputWrapper/utils/normalizeRelations.js @@ -6,44 +6,40 @@ export const normalizeRelations = ( relations, { modifiedData = {}, shouldAddLink = false, mainFieldName, targetModel } ) => { - // TODO - if (!relations?.data?.pages) { - return relations; - } - return { ...relations, data: { - pages: relations.data.pages - .map((page) => [ - ...[...page.results, ...(modifiedData?.connect ?? [])] - .map((relation) => { - const nextRelation = { ...relation }; + pages: + relations?.data?.pages + ?.map((page) => [ + ...[...page.results, ...(modifiedData?.connect ?? [])] + .map((relation) => { + const nextRelation = { ...relation }; - if (modifiedData?.disconnect?.find((relation) => relation.id === nextRelation.id)) { - return null; - } + if (modifiedData?.disconnect?.find((relation) => relation.id === nextRelation.id)) { + return null; + } - if (shouldAddLink) { - nextRelation.href = getRelationLink(targetModel, nextRelation.id); - } + if (shouldAddLink) { + nextRelation.href = getRelationLink(targetModel, nextRelation.id); + } - nextRelation.publicationState = false; + nextRelation.publicationState = false; - if (nextRelation?.publishedAt !== undefined) { - nextRelation.publicationState = nextRelation.publishedAt - ? PUBLICATION_STATES.PUBLISHED - : PUBLICATION_STATES.DRAFT; - } + if (nextRelation?.publishedAt !== undefined) { + nextRelation.publicationState = nextRelation.publishedAt + ? PUBLICATION_STATES.PUBLISHED + : PUBLICATION_STATES.DRAFT; + } - nextRelation.mainField = nextRelation[mainFieldName]; + nextRelation.mainField = nextRelation[mainFieldName]; - return nextRelation; - }) - .filter(Boolean), - ]) - .filter((page) => page.length > 0) - .reverse(), + return nextRelation; + }) + .filter(Boolean), + ]) + ?.filter((page) => page.length > 0) + ?.reverse() ?? [], }, }; };