From aab240711277c02463183fc72e288ed3b721da6d Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Fri, 24 Nov 2023 09:56:50 -0500 Subject: [PATCH] fix(live-preview): clear hasMany relationships --- packages/live-preview/src/traverseFields.ts | 2 +- test/live-preview/collections/Pages.ts | 141 +++++++++--------- test/live-preview/int.spec.ts | 38 ++--- .../app/_blocks/Relationships/index.tsx | 78 +++++++--- 4 files changed, 143 insertions(+), 116 deletions(-) diff --git a/packages/live-preview/src/traverseFields.ts b/packages/live-preview/src/traverseFields.ts index af6b0753a5..e6f0f139bb 100644 --- a/packages/live-preview/src/traverseFields.ts +++ b/packages/live-preview/src/traverseFields.ts @@ -125,7 +125,7 @@ export const traverseFields = (args: { case 'relationship': // Handle `hasMany` relationships if (fieldSchema.hasMany && Array.isArray(incomingData[fieldName])) { - if (!result[fieldName]) { + if (!result[fieldName] || !incomingData[fieldName].length) { result[fieldName] = [] } diff --git a/test/live-preview/collections/Pages.ts b/test/live-preview/collections/Pages.ts index 4424c68c48..b92aef1298 100644 --- a/test/live-preview/collections/Pages.ts +++ b/test/live-preview/collections/Pages.ts @@ -50,6 +50,79 @@ export const Pages: CollectionConfig = { }, ], }, + { + label: 'Test', + fields: [ + { + name: 'relationshipInRichText', + type: 'richText', + }, + { + name: 'relationshipAsUpload', + type: 'upload', + relationTo: 'media', + }, + { + name: 'relationshipMonoHasOne', + type: 'relationship', + relationTo: 'posts', + }, + { + name: 'relationshipMonoHasMany', + type: 'relationship', + relationTo: 'posts', + hasMany: true, + }, + { + name: 'relationshipPolyHasOne', + type: 'relationship', + relationTo: ['posts'], + }, + { + name: 'relationshipPolyHasMany', + type: 'relationship', + relationTo: ['posts'], + hasMany: true, + }, + { + name: 'arrayOfRelationships', + type: 'array', + fields: [ + { + name: 'uploadInArray', + type: 'upload', + relationTo: 'media', + }, + { + name: 'richTextInArray', + type: 'richText', + }, + { + name: 'relationshipInArrayMonoHasOne', + type: 'relationship', + relationTo: 'posts', + }, + { + name: 'relationshipInArrayMonoHasMany', + type: 'relationship', + relationTo: 'posts', + hasMany: true, + }, + { + name: 'relationshipInArrayPolyHasOne', + type: 'relationship', + relationTo: ['posts'], + }, + { + name: 'relationshipInArrayPolyHasMany', + type: 'relationship', + relationTo: ['posts'], + hasMany: true, + }, + ], + }, + ], + }, ], }, { @@ -71,73 +144,5 @@ export const Pages: CollectionConfig = { }, ], }, - { - name: 'relationshipInRichText', - type: 'richText', - }, - { - name: 'relationshipAsUpload', - type: 'upload', - relationTo: 'media', - }, - { - name: 'relationshipMonoHasOne', - type: 'relationship', - relationTo: 'posts', - }, - { - name: 'relationshipMonoHasMany', - type: 'relationship', - relationTo: 'posts', - hasMany: true, - }, - { - name: 'relationshipPolyHasOne', - type: 'relationship', - relationTo: ['posts'], - }, - { - name: 'relationshipPolyHasMany', - type: 'relationship', - relationTo: ['posts'], - hasMany: true, - }, - { - name: 'arrayOfRelationships', - type: 'array', - fields: [ - { - name: 'uploadInArray', - type: 'upload', - relationTo: 'media', - }, - { - name: 'richTextInArray', - type: 'richText', - }, - { - name: 'relationshipInArrayMonoHasOne', - type: 'relationship', - relationTo: 'posts', - }, - { - name: 'relationshipInArrayMonoHasMany', - type: 'relationship', - relationTo: 'posts', - hasMany: true, - }, - { - name: 'relationshipInArrayPolyHasOne', - type: 'relationship', - relationTo: ['posts'], - }, - { - name: 'relationshipInArrayPolyHasMany', - type: 'relationship', - relationTo: ['posts'], - hasMany: true, - }, - ], - }, ], } diff --git a/test/live-preview/int.spec.ts b/test/live-preview/int.spec.ts index 03f0458e2b..3a6f8a88f6 100644 --- a/test/live-preview/int.spec.ts +++ b/test/live-preview/int.spec.ts @@ -220,13 +220,20 @@ describe('Collections - Live Preview', () => { expect(merge1.relationshipPolyHasMany).toMatchObject([ { value: testPost, relationTo: postsSlug }, ]) + }) + it('— relationships - can clear relationships', async () => { + const initialData: Partial = { + title: 'Test Page', + relationshipMonoHasOne: testPost.id, + relationshipMonoHasMany: [testPost.id], + relationshipPolyHasOne: { value: testPost.id, relationTo: postsSlug }, + relationshipPolyHasMany: [{ value: testPost.id, relationTo: postsSlug }], + } - // Clear relationships const merge2 = await mergeData({ depth: 1, fieldSchema: schemaJSON, incomingData: { - ...merge1, relationshipMonoHasOne: null, relationshipMonoHasMany: [], relationshipPolyHasOne: null, @@ -242,33 +249,6 @@ describe('Collections - Live Preview', () => { expect(merge2.relationshipMonoHasMany).toEqual([]) expect(merge2.relationshipPolyHasOne).toBeFalsy() expect(merge2.relationshipPolyHasMany).toEqual([]) - - // Now populate the relationships again - // This will ensure that the first merge wasn't just initial state - const merge3 = await mergeData({ - depth: 1, - fieldSchema: schemaJSON, - incomingData: { - ...merge2, - relationshipMonoHasOne: testPost.id, - relationshipMonoHasMany: [testPost.id], - relationshipPolyHasOne: { value: testPost.id, relationTo: postsSlug }, - relationshipPolyHasMany: [{ value: testPost.id, relationTo: postsSlug }], - }, - initialData, - serverURL, - returnNumberOfRequests: true, - }) - - expect(merge3._numberOfRequests).toEqual(4) - expect(merge3.relationshipMonoHasOne).toMatchObject(testPost) - expect(merge3.relationshipMonoHasMany).toMatchObject([testPost]) - - expect(merge3.relationshipPolyHasOne).toMatchObject({ value: testPost, relationTo: postsSlug }) - - expect(merge3.relationshipPolyHasMany).toMatchObject([ - { value: testPost, relationTo: postsSlug }, - ]) }) it('— relationships - populates within arrays', async () => { diff --git a/test/live-preview/next-app/app/_blocks/Relationships/index.tsx b/test/live-preview/next-app/app/_blocks/Relationships/index.tsx index a68f3bbb17..c85d8a9d9a 100644 --- a/test/live-preview/next-app/app/_blocks/Relationships/index.tsx +++ b/test/live-preview/next-app/app/_blocks/Relationships/index.tsx @@ -52,8 +52,20 @@ export const RelationshipsBlock: React.FC = (props) =>

Monomorphic Has Many:

- {data?.relationshipMonoHasMany?.map((item, index) => - item ?
{typeof item === 'string' ? item : item.title}
: 'null', + {data?.relationshipMonoHasMany ? ( + + {data?.relationshipMonoHasMany.length + ? data?.relationshipMonoHasMany?.map((item, index) => + item ? ( +
{typeof item === 'string' ? item : item.title}
+ ) : ( + 'null' + ), + ) + : 'None'} +
+ ) : ( +
None
)}

Polymorphic Has One: @@ -70,12 +82,22 @@ export const RelationshipsBlock: React.FC = (props) =>

Polymorphic Has Many:

- {data?.relationshipPolyHasMany?.map((item, index) => - item.value ? ( -
{typeof item.value === 'string' ? item.value : item.value.title}
- ) : ( - 'null' - ), + {data?.relationshipPolyHasMany ? ( + + {data?.relationshipPolyHasMany.length + ? data?.relationshipPolyHasMany?.map((item, index) => + item.value ? ( +
+ {typeof item.value === 'string' ? item.value : item.value.title} +
+ ) : ( + 'null' + ), + ) + : 'None'} +
+ ) : ( +
None
)}

Array of Relationships: @@ -113,8 +135,20 @@ export const RelationshipsBlock: React.FC = (props) =>

Monomorphic Has Many:

- {item?.relationshipInArrayMonoHasMany?.map((rel, relIndex) => - rel ?
{typeof rel === 'string' ? rel : rel.title}
: 'null', + {item?.relationshipInArrayMonoHasMany ? ( + + {item?.relationshipInArrayMonoHasMany.length + ? item?.relationshipInArrayMonoHasMany?.map((rel, relIndex) => + rel ? ( +
{typeof rel === 'string' ? rel : rel.title}
+ ) : ( + 'null' + ), + ) + : 'None'} +
+ ) : ( +
None
)}

Polymorphic Has One: @@ -131,14 +165,22 @@ export const RelationshipsBlock: React.FC = (props) =>

Polymorphic Has Many:

- {item?.relationshipInArrayPolyHasMany?.map((rel, relIndex) => - rel.value ? ( -
- {typeof rel.value === 'string' ? rel.value : rel.value.title} -
- ) : ( - 'null' - ), + {item?.relationshipInArrayPolyHasMany ? ( + + {item?.relationshipInArrayPolyHasMany.length + ? item?.relationshipInArrayPolyHasMany?.map((rel, relIndex) => + rel.value ? ( +
+ {typeof rel.value === 'string' ? rel.value : rel.value.title} +
+ ) : ( + 'null' + ), + ) + : 'None'} +
+ ) : ( +
None
)} ))}