diff --git a/packages/next/src/routes/collections/findPreferenceByID.ts b/packages/next/src/routes/collections/findPreferenceByID.ts new file mode 100644 index 00000000000..483cd654228 --- /dev/null +++ b/packages/next/src/routes/collections/findPreferenceByID.ts @@ -0,0 +1,16 @@ +import httpStatus from 'http-status' + +import { findPreferenceByIDOperation } from 'payload/operations' +import { CollectionRouteHandler } from '../types' + +export const findPreferenceByID: CollectionRouteHandler<{ id: string }> = async ({ req, id }) => { + const result = await findPreferenceByIDOperation({ + key: id, + req, + user: req.user, + }) + + return Response.json(result, { + status: httpStatus.OK, + }) +} diff --git a/packages/next/src/routes/collections/updatePreferenceByID.ts b/packages/next/src/routes/collections/updatePreferenceByID.ts new file mode 100644 index 00000000000..4c659c25a21 --- /dev/null +++ b/packages/next/src/routes/collections/updatePreferenceByID.ts @@ -0,0 +1,36 @@ +import httpStatus from 'http-status' + +import { updatePreferenceByIDOperation } from 'payload/operations' +import { CollectionRouteHandler } from '../types' + +export const updatePreferenceByID: CollectionRouteHandler<{ id: string }> = async ({ req, id }) => { + try { + const doc = await updatePreferenceByIDOperation({ + key: id, + req, + user: req.user, + value: req.data?.value || req.data, + }) + + let message = req.t('general:updatedSuccessfully') + + return Response.json( + { + message, + doc, + }, + { + status: httpStatus.OK, + }, + ) + } catch (error) { + return Response.json( + { + message: error.message, + }, + { + status: error.status || httpStatus.INTERNAL_SERVER_ERROR, + }, + ) + } +} diff --git a/packages/next/src/routes/index.ts b/packages/next/src/routes/index.ts index a70bc7dd1f2..6f02affd541 100644 --- a/packages/next/src/routes/index.ts +++ b/packages/next/src/routes/index.ts @@ -35,6 +35,8 @@ import { docAccess as docAccessGlobal } from './globals/docAccess' import { findVersions as findVersionsGlobal } from './globals/findVersions' import { restoreVersion as restoreVersionGlobal } from './globals/restoreVersion' import { findVersionByID as findVersionByIdGlobal } from './globals/findVersionByID' +import { findPreferenceByID } from './collections/findPreferenceByID' +import { updatePreferenceByID } from './collections/updatePreferenceByID' const endpoints = { root: { @@ -49,6 +51,7 @@ const endpoints = { versions: findVersions, find, findByID, + findPreferenceByID, 'doc-access-by-id': docAccess, 'doc-versions-by-id': findVersionByID, }, @@ -58,6 +61,7 @@ const endpoints = { logout, unlock, access: docAccess, + updatePreferenceByID, 'first-register': registerFirstUser, 'forgot-password': forgotPassword, 'reset-password': resetPassword, @@ -184,13 +188,18 @@ export const GET = async ( // /:collection response = await endpoints.collection.GET.find({ req, collection }) } else if (slug.length === 2) { - if (slug2 in endpoints.collection.GET) { - // /:collection/init - // /:collection/me - // /:collection/versions - response = await endpoints.collection.GET[slug2]({ req, collection }) + if (slug1 === 'payload-preferences') { + // /payload-preferences/:key + response = endpoints.collection.GET.findPreferenceByID({ req, collection, id: slug2 }) } else { - response = await endpoints.collection.GET.findByID({ req, id: slug2, collection }) + if (slug2 in endpoints.collection.GET) { + // /:collection/init + // /:collection/me + // /:collection/versions + response = await endpoints.collection.GET[slug2]({ req, collection }) + } else { + response = await endpoints.collection.GET.findByID({ req, id: slug2, collection }) + } } } else if (slug.length === 3 && `doc-${slug2}-by-id` in endpoints.collection.GET) { // /:collection/access/:id @@ -265,16 +274,21 @@ export const POST = async ( if (slug.length === 1) { // /:collection response = await endpoints.collection.POST.create({ req, collection }) - } else if (slug.length === 2 && slug2 in endpoints.collection.POST) { - // /:collection/login - // /:collection/logout - // /:collection/unlock - // /:collection/access - // /:collection/first-register - // /:collection/forgot-password - // /:collection/reset-password - // /:collection/refresh-token - response = await endpoints.collection.POST[slug2]({ req, collection }) + } else if (slug.length === 2) { + if (slug1 === 'payload-preferences') { + // /payload-preferences/:key + response = endpoints.collection.POST.updatePreferenceByID({ req, collection, id: slug2 }) + } else if (slug2 in endpoints.collection.POST) { + // /:collection/login + // /:collection/logout + // /:collection/unlock + // /:collection/access + // /:collection/first-register + // /:collection/forgot-password + // /:collection/reset-password + // /:collection/refresh-token + response = await endpoints.collection.POST[slug2]({ req, collection }) + } } else if (slug.length === 3 && `doc-${slug2}-by-id` in endpoints.collection.POST) { // /:collection/access/:id // /:collection/versions/:id diff --git a/packages/payload/src/exports/operations.ts b/packages/payload/src/exports/operations.ts index 5d901122ed6..38434d8ad48 100644 --- a/packages/payload/src/exports/operations.ts +++ b/packages/payload/src/exports/operations.ts @@ -21,10 +21,13 @@ export { findVersionsOperation } from '../collections/operations/findVersions' export { restoreVersionOperation } from '../collections/operations/restoreVersion' export { updateOperation } from '../collections/operations/update' export { updateByIDOperation } from '../collections/operations/updateByID' - export { docAccessOperation as docAccessOperationGlobal } from '../globals/operations/docAccess' + export { findOneOperation } from '../globals/operations/findOne' export { findVersionByIDOperation as findVersionByIDOperationGlobal } from '../globals/operations/findVersionByID' export { findVersionsOperation as findVersionsOperationGlobal } from '../globals/operations/findVersions' export { restoreVersionOperation as restoreVersionOperationGlobal } from '../globals/operations/restoreVersion' export { updateOperation as updateOperationGlobal } from '../globals/operations/update' + +export { default as findPreferenceByIDOperation } from '../preferences/operations/findOne' +export { default as updatePreferenceByIDOperation } from '../preferences/operations/update' diff --git a/packages/ui/src/providers/Preferences/index.tsx b/packages/ui/src/providers/Preferences/index.tsx index befb18bdfb5..df0191e77e9 100644 --- a/packages/ui/src/providers/Preferences/index.tsx +++ b/packages/ui/src/providers/Preferences/index.tsx @@ -60,7 +60,7 @@ export const PreferencesProvider: React.FC<{ children?: React.ReactNode }> = ({ let value = null if (request.status === 200) { const preference = await request.json() - value = preference.value + value = preference?.value } preferencesRef.current[key] = value resolve(value) @@ -85,6 +85,7 @@ export const PreferencesProvider: React.FC<{ children?: React.ReactNode }> = ({ let newValue = value const currentPreference = await getPreference(key) + // handle value objects where multiple values can be set under one key if ( typeof value === 'object' &&