fix: incorrect form changed state after doc drawer edit (#9025)

Closes #9000

When you update a relationship document via the document drawer, the
initial document is registering `modified: true`. We should only set
modified to true on the initial document if the relationship id has
changed.
This commit is contained in:
Jessica Chowdhury
2024-11-08 14:04:37 -05:00
committed by GitHub
parent f67761fe22
commit dc111041cb
2 changed files with 37 additions and 17 deletions

View File

@@ -457,26 +457,25 @@ const RelationshipFieldComponent: RelationshipFieldClientComponent = (props) =>
i18n, i18n,
}) })
if (hasMany) { const currentValue = valueRef.current
setValue( const docId = args.doc.id
valueRef.current
? (valueRef.current as Option[]).map((option) => {
if (option.value === args.doc.id) {
return {
relationTo: args.collectionConfig.slug,
value: args.doc.id,
}
}
return option if (hasMany) {
}) const unchanged = (currentValue as Option[]).some((option) =>
: null, typeof option === 'string' ? option === docId : option.value === docId,
) )
const valuesToSet = (currentValue as Option[]).map((option) =>
option.value === docId
? { relationTo: args.collectionConfig.slug, value: docId }
: option,
)
setValue(valuesToSet, unchanged)
} else { } else {
setValue({ const unchanged = currentValue === docId
relationTo: args.collectionConfig.slug,
value: args.doc.id, setValue({ relationTo: args.collectionConfig.slug, value: docId }, unchanged)
})
} }
}, },
[i18n, config, hasMany, setValue], [i18n, config, hasMany, setValue],

View File

@@ -453,6 +453,27 @@ describe('fields - relationship', () => {
).toHaveCount(1) ).toHaveCount(1)
}) })
test('should update relationship from drawer without enabling save in main doc', async () => {
await page.goto(url.edit(docWithExistingRelations.id))
const saveButton = page.locator('#action-save')
await expect(saveButton).toBeDisabled()
await openDocDrawer(
page,
'#field-relationship button.relationship--single-value__drawer-toggler ',
)
const field = page.locator('#field-name')
await field.fill('Updated')
await saveButton.nth(1).click()
await expect(page.locator('.payload-toast-container')).toContainText('Updated successfully')
await page.locator('.doc-drawer__header-close').click()
await expect(saveButton).toBeDisabled()
})
test('should allow filtering by polymorphic relationships with version drafts enabled', async () => { test('should allow filtering by polymorphic relationships with version drafts enabled', async () => {
await createVersionedRelationshipFieldDoc('Without relationship') await createVersionedRelationshipFieldDoc('Without relationship')
await createVersionedRelationshipFieldDoc('with relationship', [ await createVersionedRelationshipFieldDoc('with relationship', [