Files
payloadcms/test/helpers/reInitEndpoint.ts
Patrik 7e81d30808 fix(ui): ensure document unlocks when logging out from edit view of a locked document (#13142)
### What?

Refactors the `LeaveWithoutSaving` modal to be generic and delegates
document unlock logic back to the `DefaultEditView` component via a
callback.

### Why?

Previously, `unlockDocument` was triggered in a cleanup `useEffect` in
the edit view. When logging out from the edit view, the unlock request
would often fail due to the session ending — leaving the document in a
locked state.

### How?

- Introduced `onConfirm` and `onPrevent` props for `LeaveWithoutSaving`.
- Moved all document lock/unlock logic into `DefaultEditView`’s
`handleLeaveConfirm`.
- Captures the next navigation target via `onPrevent` and evaluates
whether to unlock based on:
  - Locking being enabled.
  - Current user owning the lock.
- Navigation not targeting internal admin views (`/preview`, `/api`,
`/versions`).

---------

Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
2025-07-24 09:18:49 -07:00

59 lines
1.4 KiB
TypeScript

import type { Endpoint, PayloadHandler } from 'payload'
import { status as httpStatus } from 'http-status'
import * as qs from 'qs-esm'
import { path } from './reInitializeDB.js'
import { seedDB } from './seed.js'
const handler: PayloadHandler = async (req) => {
process.env.SEED_IN_CONFIG_ONINIT = 'true'
const { payload } = req
if (!req.url) {
throw new Error('Request URL is required')
}
const query: {
deleteOnly?: string
snapshotKey?: string
uploadsDir?: string | string[]
} = qs.parse(req.url.split('?')[1] ?? '', {
depth: 10,
ignoreQueryPrefix: true,
})
try {
await seedDB({
_payload: payload,
collectionSlugs: payload.config.collections.map(({ slug }) => slug),
seedFunction: payload.config.onInit,
snapshotKey: String(query.snapshotKey),
// uploadsDir can be string or stringlist
uploadsDir: query.uploadsDir as string | string[],
// query value will be a string of 'true' or 'false'
deleteOnly: query.deleteOnly === 'true',
})
return Response.json(
{
message: 'Database reset and onInit run successfully.',
},
{
status: httpStatus.OK,
},
)
} catch (err) {
payload.logger.error(err)
return Response.json(err, {
status: httpStatus.BAD_REQUEST,
})
}
}
export const reInitEndpoint: Endpoint = {
path,
method: 'get',
handler,
}