Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): updatedAt field in locked-docs collection able to be updated by non-owner #9026

Merged
merged 7 commits into from
Nov 5, 2024
Merged
8 changes: 7 additions & 1 deletion packages/ui/src/utilities/buildFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
32 changes: 32 additions & 0 deletions test/locked-documents/collections/Posts/fields/DocumentLoaded.tsx
Original file line number Diff line number Diff line change
@@ -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<TextFieldClientProps> = ({ field: label }) => {
const field = useField<Date>({
path: 'documentLoaded',
})
const hasRun = useRef(false)

useEffect(() => {
if (hasRun.current || field.formInitializing) {
return
}
hasRun.current = true

field.setValue(new Date().toISOString())
}, [field])

return (
<div
style={{
marginBottom: '20px',
}}
>
<FieldLabel field={label} />
<DatePicker displayFormat="yyyy-MM-dd hh:mm:ss" readOnly={true} value={field.value} />
</div>
)
}
15 changes: 15 additions & 0 deletions test/locked-documents/collections/Posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const PostsCollection: CollectionConfig = {
slug: postsSlug,
admin: {
useAsTitle: 'text',
defaultColumns: ['text', 'createdAt', 'updatedAt', '_status'],
},
lockDocuments: {
duration: 180,
Expand All @@ -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,
Expand Down