Skip to content

Commit 4278e72

Browse files
fix(next): richtext field is read-only for expired lock (#13789)
### What? Opening a document with an expired lock and richtext caused the richtext to be read-only. ### Why? The changes in #13579 made the richtext read only if isLocked is set. But the server side implementation of isLocked did not consider expired locks. ### How? Update the server-side getIsLocked to also consider expired locks by not loading them.
1 parent 3c5aa1b commit 4278e72

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

packages/next/src/views/Document/getIsLocked.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,28 @@ export const getIsLocked = async ({
4242

4343
const where: Where = {}
4444

45+
const lockDurationDefault = 300 // Default 5 minutes in seconds
46+
const lockDuration =
47+
typeof entityConfig.lockDocuments === 'object'
48+
? entityConfig.lockDocuments.duration
49+
: lockDurationDefault
50+
const lockDurationInMilliseconds = lockDuration * 1000
51+
52+
const now = new Date().getTime()
53+
4554
if (globalConfig) {
46-
where.globalSlug = {
47-
equals: globalConfig.slug,
48-
}
55+
where.and = [
56+
{
57+
globalSlug: {
58+
equals: globalConfig.slug,
59+
},
60+
},
61+
{
62+
updatedAt: {
63+
greater_than: new Date(now - lockDurationInMilliseconds),
64+
},
65+
},
66+
]
4967
} else {
5068
where.and = [
5169
{
@@ -58,6 +76,11 @@ export const getIsLocked = async ({
5876
equals: collectionConfig.slug,
5977
},
6078
},
79+
{
80+
updatedAt: {
81+
greater_than: new Date(now - lockDurationInMilliseconds),
82+
},
83+
},
6184
]
6285
}
6386

test/locked-documents/e2e.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ describe('Locked Documents', () => {
630630
let lockedDoc: PayloadLockedDocument
631631
let expiredTestDoc: Test
632632
let expiredTestLockedDoc: PayloadLockedDocument
633+
let expiredPostDoc: Post
634+
let expiredPostLockedDoc: PayloadLockedDocument
633635

634636
beforeAll(async () => {
635637
postDoc = await createPostDoc({
@@ -678,6 +680,27 @@ describe('Locked Documents', () => {
678680
},
679681
},
680682
})
683+
684+
expiredPostDoc = await createPostDoc({
685+
text: 'expired post doc',
686+
})
687+
688+
expiredPostLockedDoc = await payload.create({
689+
collection: lockedDocumentCollection,
690+
data: {
691+
document: {
692+
relationTo: 'posts',
693+
value: expiredPostDoc.id,
694+
},
695+
globalSlug: undefined,
696+
user: {
697+
relationTo: 'users',
698+
value: user2.id,
699+
},
700+
createdAt: new Date(Date.now() - 1000 * 60 * 60).toISOString(),
701+
updatedAt: new Date(Date.now() - 1000 * 60 * 60).toISOString(),
702+
},
703+
})
681704
})
682705

683706
afterAll(async () => {
@@ -705,6 +728,16 @@ describe('Locked Documents', () => {
705728
collection: 'tests',
706729
id: expiredTestDoc.id,
707730
})
731+
732+
await payload.delete({
733+
collection: lockedDocumentCollection,
734+
id: expiredPostLockedDoc.id,
735+
})
736+
737+
await payload.delete({
738+
collection: 'posts',
739+
id: expiredPostDoc.id,
740+
})
708741
})
709742

710743
test('should show Document Locked modal for incoming user when entering locked document', async () => {
@@ -739,6 +772,20 @@ describe('Locked Documents', () => {
739772
await expect(modalContainer).toBeHidden()
740773
})
741774

775+
test('expired lock should render editable fields (no read-only)', async () => {
776+
await page.goto(postsUrl.edit(expiredPostDoc.id))
777+
778+
await expect(page.locator('#field-text')).toBeEnabled()
779+
780+
const richTextRoot = page
781+
.locator('.rich-text-lexical .ContentEditable__root[data-lexical-editor="true"]')
782+
.first()
783+
await expect(richTextRoot).toBeVisible()
784+
785+
// ensure richtext is editable
786+
await expect(richTextRoot).toHaveAttribute('contenteditable', 'true')
787+
})
788+
742789
test('should show fields in read-only if incoming user views locked doc in read-only mode', async () => {
743790
await page.goto(postsUrl.edit(postDoc.id))
744791

0 commit comments

Comments
 (0)