normalizeRelations: Return empty array as fallback

This commit is contained in:
Gustav Hansen 2022-09-07 19:14:14 +02:00
parent 6d1642d713
commit 3e90c71ead

View File

@ -6,44 +6,40 @@ export const normalizeRelations = (
relations, relations,
{ modifiedData = {}, shouldAddLink = false, mainFieldName, targetModel } { modifiedData = {}, shouldAddLink = false, mainFieldName, targetModel }
) => { ) => {
// TODO
if (!relations?.data?.pages) {
return relations;
}
return { return {
...relations, ...relations,
data: { data: {
pages: relations.data.pages pages:
.map((page) => [ relations?.data?.pages
...[...page.results, ...(modifiedData?.connect ?? [])] ?.map((page) => [
.map((relation) => { ...[...page.results, ...(modifiedData?.connect ?? [])]
const nextRelation = { ...relation }; .map((relation) => {
const nextRelation = { ...relation };
if (modifiedData?.disconnect?.find((relation) => relation.id === nextRelation.id)) { if (modifiedData?.disconnect?.find((relation) => relation.id === nextRelation.id)) {
return null; return null;
} }
if (shouldAddLink) { if (shouldAddLink) {
nextRelation.href = getRelationLink(targetModel, nextRelation.id); nextRelation.href = getRelationLink(targetModel, nextRelation.id);
} }
nextRelation.publicationState = false; nextRelation.publicationState = false;
if (nextRelation?.publishedAt !== undefined) { if (nextRelation?.publishedAt !== undefined) {
nextRelation.publicationState = nextRelation.publishedAt nextRelation.publicationState = nextRelation.publishedAt
? PUBLICATION_STATES.PUBLISHED ? PUBLICATION_STATES.PUBLISHED
: PUBLICATION_STATES.DRAFT; : PUBLICATION_STATES.DRAFT;
} }
nextRelation.mainField = nextRelation[mainFieldName]; nextRelation.mainField = nextRelation[mainFieldName];
return nextRelation; return nextRelation;
}) })
.filter(Boolean), .filter(Boolean),
]) ])
.filter((page) => page.length > 0) ?.filter((page) => page.length > 0)
.reverse(), ?.reverse() ?? [],
}, },
}; };
}; };