From e74906f5550642f988a7882ec610a6f2276da27c Mon Sep 17 00:00:00 2001 From: Patrik Date: Mon, 28 Oct 2024 21:49:50 -0400 Subject: [PATCH] fix(next, ui): exclude expired locks for globals (#8914) Continued PR off of https://github.com/payloadcms/payload/pull/8899 --- .../src/views/Dashboard/Default/index.tsx | 27 +- packages/next/src/views/Dashboard/index.tsx | 23 +- .../payload/src/globals/operations/findOne.ts | 1 + packages/ui/src/utilities/buildFormState.ts | 39 +- packages/ui/src/utilities/getFormState.ts | 7 +- .../collections/Tests/index.ts | 22 ++ test/locked-documents/config.ts | 6 +- test/locked-documents/e2e.spec.ts | 373 +++++++++++++++--- test/locked-documents/globals/Admin/index.ts | 16 + test/locked-documents/payload-types.ts | 27 ++ 10 files changed, 455 insertions(+), 86 deletions(-) create mode 100644 test/locked-documents/collections/Tests/index.ts create mode 100644 test/locked-documents/globals/Admin/index.ts diff --git a/packages/next/src/views/Dashboard/Default/index.tsx b/packages/next/src/views/Dashboard/Default/index.tsx index 05da8f8c9..18dbe7169 100644 --- a/packages/next/src/views/Dashboard/Default/index.tsx +++ b/packages/next/src/views/Dashboard/Default/index.tsx @@ -16,7 +16,11 @@ import './index.scss' const baseClass = 'dashboard' export type DashboardProps = { - globalData: Array<{ data: { _isLocked: boolean; _userEditing: ClientUser | null }; slug: string }> + globalData: Array<{ + data: { _isLocked: boolean; _lastEditedAt: string; _userEditing: ClientUser | null } + lockDuration?: number + slug: string + }> Link: React.ComponentType navGroups?: ReturnType permissions: Permissions @@ -95,7 +99,7 @@ export const DefaultDashboard: React.FC = (props) => { let createHREF: string let href: string let hasCreatePermission: boolean - let lockStatus = null + let isLocked = null let userEditing = null if (type === EntityType.collection) { @@ -130,9 +134,24 @@ export const DefaultDashboard: React.FC = (props) => { const globalLockData = globalData.find( (global) => global.slug === entity.slug, ) + if (globalLockData) { - lockStatus = globalLockData.data._isLocked + isLocked = globalLockData.data._isLocked userEditing = globalLockData.data._userEditing + + // Check if the lock is expired + const lockDuration = globalLockData?.lockDuration + const lastEditedAt = new Date( + globalLockData.data?._lastEditedAt, + ).getTime() + + const lockDurationInMilliseconds = lockDuration * 1000 + const lockExpirationTime = lastEditedAt + lockDurationInMilliseconds + + if (new Date().getTime() > lockExpirationTime) { + isLocked = false + userEditing = null + } } } @@ -140,7 +159,7 @@ export const DefaultDashboard: React.FC = (props) => {
  • ) : hasCreatePermission && type === EntityType.collection ? (