Skip to content

Commit 8c5c1fb

Browse files
rilromPatrikKozak
andauthored
fix(ui): component AddNewRelation saves relationTo as first collection instead of selected collection (#14836)
### What? Currently when you use the create button in a relationship field that has more than one collection in its `relationTo` and a collection that isn't first in the list is selected, the selected value in the field will always be pointing to the first collection in the list, causing incorrect results or validation errors. ### Why? The selected value in the field should be applied to the collection the user has selected, not just the first collection in the `relationTo` array. ### How? Use the selected collection in the `onChange` handler instead of `relatedCollections[0]`. Fixes: #14728 --------- Co-authored-by: Patrik Kozak <35232443+PatrikKozak@users.noreply.github.com>
1 parent 0da0da8 commit 8c5c1fb

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

docs/access-control/collections.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ export const CollectionWithVersionsAccess: CollectionConfig = {
339339
```
340340

341341
<Banner type="warning">
342-
**Note:** Returning a [Query](../queries/overview) will apply the constraint to the
343-
[`versions` collection](../versions/overview#database-impact), not the original Collection.
342+
**Note:** Returning a [Query](../queries/overview) will apply the constraint
343+
to the [`versions` collection](../versions/overview#database-impact), not the
344+
original Collection.
344345
</Banner>
345346

346347
The following arguments are provided to the `readVersions` function:

docs/access-control/globals.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ export const GlobalWithVersionsAccess: GlobalConfig = {
137137
```
138138

139139
<Banner type="warning">
140-
**Note:** Returning a [Query](../queries/overview) will apply the constraint to the
141-
[`versions` collection](../versions/overview#database-impact), not the original Global.
140+
**Note:** Returning a [Query](../queries/overview) will apply the constraint
141+
to the [`versions` collection](../versions/overview#database-impact), not the
142+
original Global.
142143
</Banner>
143144

144145
The following arguments are provided to the `readVersions` function:

packages/ui/src/elements/AddNewRelation/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const AddNewRelation: React.FC<Props> = ({
7979
])
8080
} else {
8181
onChange({
82-
relationTo: relatedCollections[0].slug,
82+
relationTo: collectionConfig?.slug,
8383
value: doc.id,
8484
})
8585
}
@@ -88,7 +88,7 @@ export const AddNewRelation: React.FC<Props> = ({
8888
setSelectedCollection(undefined)
8989
}
9090
},
91-
[collectionConfig, hasMany, onChange, value, relatedCollections],
91+
[collectionConfig, hasMany, onChange, value],
9292
)
9393

9494
const onPopupToggle = useCallback((state) => {

test/fields/collections/Relationship/e2e.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ describe('relationship', () => {
9898
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
9999
})
100100

101+
test('should save correct relationTo when creating doc in second collection (bug #14728)', async () => {
102+
await loadCreatePage()
103+
104+
await openCreateDocDrawer({ page, fieldSelector: '#field-relationship' })
105+
106+
// Select the SECOND collection (array-fields) instead of the first (text-fields)
107+
await page
108+
.locator('#field-relationship .relationship-add-new__relation-button--array-fields')
109+
.click()
110+
111+
await page.locator('[id^=doc-drawer_array-fields_1_] #action-save').click()
112+
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
113+
await page.locator('[id^=close-drawer__doc-drawer_array-fields_1_]').click()
114+
115+
const relationshipValue = page.locator('#field-relationship .relationship--single-value__text')
116+
await expect(relationshipValue).toBeVisible()
117+
await expect(async () => {
118+
const valueText = await relationshipValue.textContent()
119+
expect(valueText).not.toContain('Another text document')
120+
expect(valueText).not.toContain('Untitled')
121+
}).toPass()
122+
123+
await page.locator('#action-save').click()
124+
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
125+
})
126+
101127
test('should create nested inline relationships', async () => {
102128
await loadCreatePage()
103129

0 commit comments

Comments
 (0)