Skip to content

Commit 25e3902

Browse files
authored
fix(ui): should select document after creation from relationship field (#12842)
### What? After creating a new document from a relationship field, this doc should automatically become the selected document for that relationship field. This is the expected and current behavior. However, when the relationship ties to a collection with autosave enabled, this does not happen. ### Why? This is expected behavior and should still happen when the relationship is using an autosave enabled collection. ### How? 1. The logic in `addNewRelation` contained an `if` statement that checked for `operation === 'create'` - however when autosave is enabled, the `create` operation runs on the first data update and subsequently it is a `update` operation. 2. The `onSave` from the document drawer provider was not being run as part of the autosave workflow. #### Reported by client.
1 parent 59f536c commit 25e3902

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export const AddNewRelation: React.FC<Props> = ({
5252

5353
const onSave: DocumentDrawerContextType['onSave'] = useCallback(
5454
({ doc, operation }) => {
55-
if (operation === 'create') {
55+
// if autosave is enabled, the operation will be 'update'
56+
const isAutosaveEnabled =
57+
typeof collectionConfig?.versions?.drafts === 'object'
58+
? collectionConfig.versions.drafts.autosave
59+
: false
60+
61+
if (operation === 'create' || (operation === 'update' && isAutosaveEnabled)) {
5662
// ensure the value is not already in the array
5763
let isNewValue = false
5864
if (!value) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useLocale } from '../../providers/Locale/index.js'
2323
import { useTranslation } from '../../providers/Translation/index.js'
2424
import { formatTimeToNow } from '../../utilities/formatDocTitle/formatDateTitle.js'
2525
import { reduceFieldsToValuesWithValidation } from '../../utilities/reduceFieldsToValuesWithValidation.js'
26+
import { useDocumentDrawerContext } from '../DocumentDrawer/Provider.js'
2627
import { LeaveWithoutSaving } from '../LeaveWithoutSaving/index.js'
2728
import './index.scss'
2829

@@ -56,6 +57,8 @@ export const Autosave: React.FC<Props> = ({ id, collection, global: globalDoc })
5657
updateSavedDocumentData,
5758
} = useDocumentInfo()
5859

60+
const { onSave: onSaveFromDocumentDrawer } = useDocumentDrawerContext()
61+
5962
const { reportUpdate } = useDocumentEvents()
6063
const { dispatchFields, isValid, setBackgroundProcessing, setIsValid, setSubmitted } = useForm()
6164

@@ -183,6 +186,8 @@ export const Autosave: React.FC<Props> = ({ id, collection, global: globalDoc })
183186
// We need to log the time in order to figure out if we need to trigger the state off later
184187
endTimestamp = newDate.getTime()
185188

189+
const json = await res.json()
190+
186191
if (res.status === 200) {
187192
setLastUpdateTime(newDate.getTime())
188193

@@ -192,13 +197,20 @@ export const Autosave: React.FC<Props> = ({ id, collection, global: globalDoc })
192197
updatedAt: newDate.toISOString(),
193198
})
194199

200+
// if onSaveFromDocumentDrawer is defined, call it
201+
if (typeof onSaveFromDocumentDrawer === 'function') {
202+
void onSaveFromDocumentDrawer({
203+
...json,
204+
operation: 'update',
205+
})
206+
}
207+
195208
if (!mostRecentVersionIsAutosaved) {
196209
incrementVersionCount()
197210
setMostRecentVersionIsAutosaved(true)
198211
setUnpublishedVersionCount((prev) => prev + 1)
199212
}
200213
}
201-
const json = await res.json()
202214

203215
if (versionsConfig?.drafts && versionsConfig?.drafts?.validate && json?.errors) {
204216
if (Array.isArray(json.errors)) {

test/versions/e2e.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,36 @@ describe('Versions', () => {
175175
}).toPass({ timeout: 10000, intervals: [100] })
176176
})
177177

178+
test('autosave relationships - should select doc after creating from relationship field', async () => {
179+
await page.goto(postURL.create)
180+
const autosaveRelationField = page.locator('#field-relationToAutosaves')
181+
await expect(autosaveRelationField).toBeVisible()
182+
const addNewButton = autosaveRelationField.locator(
183+
'.relationship-add-new__add-button.doc-drawer__toggler',
184+
)
185+
await addNewButton.click()
186+
const titleField = page.locator('#field-title')
187+
const descriptionField = page.locator('#field-description')
188+
await titleField.fill('test')
189+
await descriptionField.fill('test')
190+
191+
const createdDate = await page.textContent(
192+
'li:has(p:has-text("Created:")) .doc-controls__value',
193+
)
194+
195+
// wait for modified date and created date to be different
196+
await expect(async () => {
197+
const modifiedDateLocator = page.locator(
198+
'li:has(p:has-text("Last Modified:")) .doc-controls__value',
199+
)
200+
await expect(modifiedDateLocator).not.toHaveText(createdDate ?? '')
201+
}).toPass({ timeout: POLL_TOPASS_TIMEOUT, intervals: [100] })
202+
203+
const closeDrawer = page.locator('.doc-drawer__header-close')
204+
await closeDrawer.click()
205+
const fieldValue = autosaveRelationField.locator('.value-container')
206+
await expect(fieldValue).toContainText('test')
207+
})
178208
test('should show collection versions view level action in collection versions view', async () => {
179209
await page.goto(url.list)
180210
await page.locator('tbody tr .cell-title a').first().click()

0 commit comments

Comments
 (0)