diff --git a/packages/ui/src/utilities/buildFormState.ts b/packages/ui/src/utilities/buildFormState.ts index 34575ba151..5ceb148427 100644 --- a/packages/ui/src/utilities/buildFormState.ts +++ b/packages/ui/src/utilities/buildFormState.ts @@ -278,7 +278,13 @@ export const buildFormState = async ({ user: lockedDocument.docs[0]?.user?.value, } - if (updateLastEdited) { + const lockOwnerId = + typeof lockedDocument.docs[0]?.user?.value === 'object' + ? lockedDocument.docs[0]?.user?.value?.id + : lockedDocument.docs[0]?.user?.value + + // Should only update doc if the incoming / current user is also the owner of the locked doc + if (updateLastEdited && req.user && lockOwnerId === req.user.id) { await req.payload.db.updateOne({ id: lockedDocument.docs[0].id, collection: 'payload-locked-documents', diff --git a/test/locked-documents/collections/Posts/fields/DocumentLoaded.tsx b/test/locked-documents/collections/Posts/fields/DocumentLoaded.tsx new file mode 100644 index 0000000000..c652bdbce9 --- /dev/null +++ b/test/locked-documents/collections/Posts/fields/DocumentLoaded.tsx @@ -0,0 +1,32 @@ +'use client' +import type { TextFieldClientProps } from 'payload' + +import { DatePicker, FieldLabel, useField } from '@payloadcms/ui' +import { type FunctionComponent, useEffect, useRef } from 'react' + +export const DocumentLoaded: FunctionComponent = ({ field: label }) => { + const field = useField({ + path: 'documentLoaded', + }) + const hasRun = useRef(false) + + useEffect(() => { + if (hasRun.current || field.formInitializing) { + return + } + hasRun.current = true + + field.setValue(new Date().toISOString()) + }, [field]) + + return ( +
+ + +
+ ) +} diff --git a/test/locked-documents/collections/Posts/index.ts b/test/locked-documents/collections/Posts/index.ts index 53022349a7..02eb7a89f1 100644 --- a/test/locked-documents/collections/Posts/index.ts +++ b/test/locked-documents/collections/Posts/index.ts @@ -6,6 +6,7 @@ export const PostsCollection: CollectionConfig = { slug: postsSlug, admin: { useAsTitle: 'text', + defaultColumns: ['text', 'createdAt', 'updatedAt', '_status'], }, lockDocuments: { duration: 180, @@ -15,6 +16,20 @@ export const PostsCollection: CollectionConfig = { name: 'text', type: 'text', }, + { + name: 'documentLoaded', + label: 'Document loaded', + type: 'date', + admin: { + date: { + displayFormat: 'yyyy-MM-dd HH:mm:ss', + }, + readOnly: true, + components: { + Field: '/collections/Posts/fields/DocumentLoaded.tsx#DocumentLoaded', + }, + }, + }, ], versions: { drafts: true,