Skip to content

Commit 1d46b6d

Browse files
authored
fix(ui): join field "add new" calculate initial drawer data with relationship inside blocks (#10057)
We merged #9773 that adds support for join field with relationships inside arrays, it just happens that now it's also true for relationships inside blocks, added a test case with blocks. This just corrects initial data drawer calculation with the "add new" button if we encounter a blocks field in join's `on`.
1 parent 03ff775 commit 1d46b6d

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

packages/ui/src/fields/Join/index.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const getInitialDrawerData = ({
4343

4444
const field = flattenedFields.find((field) => field.name === path)
4545

46+
if (!field) {
47+
return null
48+
}
49+
4650
if (field.type === 'relationship' || field.type === 'upload') {
4751
return {
4852
// TODO: Handle polymorphic https://github.com/payloadcms/payload/pull/9990
@@ -71,6 +75,25 @@ const getInitialDrawerData = ({
7175
[field.name]: [initialData],
7276
}
7377
}
78+
79+
if (field.type === 'blocks') {
80+
for (const block of field.blocks) {
81+
const blockInitialData = getInitialDrawerData({
82+
docID,
83+
fields: block.fields,
84+
segments: nextSegments,
85+
})
86+
87+
if (blockInitialData) {
88+
blockInitialData.id = ObjectId().toHexString()
89+
blockInitialData.blockType = block.slug
90+
91+
return {
92+
[field.name]: [blockInitialData],
93+
}
94+
}
95+
}
96+
}
7497
}
7598

7699
const JoinFieldComponent: JoinFieldClientComponent = (props) => {

test/joins/collections/Categories.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export const Categories: CollectionConfig = {
109109
collection: 'posts',
110110
on: 'array.category',
111111
},
112+
{
113+
name: 'blocksPosts',
114+
type: 'join',
115+
collection: 'posts',
116+
on: 'blocks.category',
117+
},
112118
{
113119
name: 'singulars',
114120
type: 'join',

test/joins/collections/Posts.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,21 @@ export const Posts: CollectionConfig = {
8080
},
8181
],
8282
},
83+
{
84+
name: 'blocks',
85+
type: 'blocks',
86+
blocks: [
87+
{
88+
slug: 'block',
89+
fields: [
90+
{
91+
name: 'category',
92+
type: 'relationship',
93+
relationTo: categoriesSlug,
94+
},
95+
],
96+
},
97+
],
98+
},
8399
],
84100
}

test/joins/int.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('Joins Field', () => {
9595
camelCaseCategory: category.id,
9696
},
9797
array: [{ category: category.id }],
98+
blocks: [{ blockType: 'block', category: category.id }],
9899
})
99100
}
100101
})
@@ -193,6 +194,15 @@ describe('Joins Field', () => {
193194
expect(categoryWithPosts.arrayPosts.docs).toBeDefined()
194195
})
195196

197+
it('should populate joins with blocks relationships', async () => {
198+
const categoryWithPosts = await payload.findByID({
199+
id: category.id,
200+
collection: categoriesSlug,
201+
})
202+
203+
expect(categoryWithPosts.blocksPosts.docs).toBeDefined()
204+
})
205+
196206
it('should populate uploads in joins', async () => {
197207
const { docs } = await payload.find({
198208
limit: 1,

test/joins/payload-types.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export interface Config {
3636
hasManyPostsLocalized: 'posts';
3737
'group.relatedPosts': 'posts';
3838
'group.camelCasePosts': 'posts';
39+
arrayPosts: 'posts';
40+
blocksPosts: 'posts';
3941
filtered: 'posts';
4042
hiddenPosts: 'hidden-posts';
4143
singulars: 'singular';
@@ -125,6 +127,20 @@ export interface Post {
125127
category?: (string | null) | Category;
126128
camelCaseCategory?: (string | null) | Category;
127129
};
130+
array?:
131+
| {
132+
category?: (string | null) | Category;
133+
id?: string | null;
134+
}[]
135+
| null;
136+
blocks?:
137+
| {
138+
category?: (string | null) | Category;
139+
id?: string | null;
140+
blockName?: string | null;
141+
blockType: 'block';
142+
}[]
143+
| null;
128144
updatedAt: string;
129145
createdAt: string;
130146
}
@@ -183,6 +199,14 @@ export interface Category {
183199
hasNextPage?: boolean | null;
184200
} | null;
185201
};
202+
arrayPosts?: {
203+
docs?: (string | Post)[] | null;
204+
hasNextPage?: boolean | null;
205+
} | null;
206+
blocksPosts?: {
207+
docs?: (string | Post)[] | null;
208+
hasNextPage?: boolean | null;
209+
} | null;
186210
singulars?: {
187211
docs?: (string | Singular)[] | null;
188212
hasNextPage?: boolean | null;
@@ -463,6 +487,23 @@ export interface PostsSelect<T extends boolean = true> {
463487
category?: T;
464488
camelCaseCategory?: T;
465489
};
490+
array?:
491+
| T
492+
| {
493+
category?: T;
494+
id?: T;
495+
};
496+
blocks?:
497+
| T
498+
| {
499+
block?:
500+
| T
501+
| {
502+
category?: T;
503+
id?: T;
504+
blockName?: T;
505+
};
506+
};
466507
updatedAt?: T;
467508
createdAt?: T;
468509
}
@@ -482,6 +523,8 @@ export interface CategoriesSelect<T extends boolean = true> {
482523
relatedPosts?: T;
483524
camelCasePosts?: T;
484525
};
526+
arrayPosts?: T;
527+
blocksPosts?: T;
485528
singulars?: T;
486529
filtered?: T;
487530
updatedAt?: T;

0 commit comments

Comments
 (0)