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

chore(next): handles preferences routes #5041

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/next/src/routes/collections/findPreferenceByID.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
36 changes: 36 additions & 0 deletions packages/next/src/routes/collections/updatePreferenceByID.ts
Original file line number Diff line number Diff line change
@@ -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,
},
)
}
}
46 changes: 30 additions & 16 deletions packages/next/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -49,6 +51,7 @@ const endpoints = {
versions: findVersions,
find,
findByID,
findPreferenceByID,
'doc-access-by-id': docAccess,
'doc-versions-by-id': findVersionByID,
},
Expand All @@ -58,6 +61,7 @@ const endpoints = {
logout,
unlock,
access: docAccess,
updatePreferenceByID,
'first-register': registerFirstUser,
'forgot-password': forgotPassword,
'reset-password': resetPassword,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/payload/src/exports/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 2 additions & 1 deletion packages/ui/src/providers/Preferences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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' &&
Expand Down
Loading