Files
payloadcms/packages/plugin-search/src/Search/index.ts
Said Akhrarov cdaebcc79c fix(plugin-search): respect custom api route in reindexButton (#10258)
<!--

Thank you for the PR! Please go through the checklist below and make
sure you've completed all the steps.

Please review the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository if you haven't already.

The following items will ensure that your PR is handled as smoothly as
possible:

- PR Title must follow conventional commits format. For example, `feat:
my new feature`, `fix(plugin-seo): my fix`.
- Minimal description explained as if explained to someone not
immediately familiar with the code.
- Provide before/after screenshots or code diffs if applicable.
- Link any related issues/discussions from GitHub or Discord.
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Fixes #

-->
### What?
This PR fixes an issue with `plugin-search` where the ReindexButton was
not directing the reindex call to the correct endpoint location if the
user defined a custom config api route.

### Why?
To allow collection reindexing even when the default base api path gets
overriden with a user-provided one.

### How?
By threading the custom route to the ReindexButton component that calls
the reindex endpoint.

Fixes #10245

Notes:
- I think the `basePath` check/manipulation might be better in the RSC
instead of the client

Edit: @JessChowdhury Didn't see you were assigned until after! Felt bad
about this since it's my bad, wanted to take some ownership over the bug
here, my mistake!
2024-12-31 00:08:58 +00:00

114 lines
3.0 KiB
TypeScript

import type { CollectionConfig, Field } from 'payload'
import type { SearchPluginConfigWithLocales } from '../types.js'
import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
// all settings can be overridden by the config
export const generateSearchCollection = (
pluginConfig: SearchPluginConfigWithLocales,
): CollectionConfig => {
const apiBasePath = pluginConfig?.apiBasePath || '/api'
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
const searchCollections = pluginConfig?.collections || []
const collectionLabels = pluginConfig?.labels
const defaultFields: Field[] = [
{
name: 'title',
type: 'text',
admin: {
readOnly: true,
},
localized: pluginConfig.localize,
},
{
name: 'priority',
type: 'number',
admin: {
position: 'sidebar',
},
},
{
name: 'doc',
type: 'relationship',
admin: {
position: 'sidebar',
readOnly: true,
},
index: true,
maxDepth: 0,
relationTo: searchCollections,
required: true,
},
{
name: 'docUrl',
type: 'ui',
admin: {
components: {
Field: {
path: '@payloadcms/plugin-search/client#LinkToDoc',
},
},
position: 'sidebar',
},
},
]
const newConfig: CollectionConfig = {
...(pluginConfig?.searchOverrides || {}),
slug: searchSlug,
access: {
create: (): boolean => false,
read: (): boolean => true,
...(pluginConfig?.searchOverrides?.access || {}),
},
admin: {
components: {
views: {
list: {
actions: [
{
path: '@payloadcms/plugin-search/client#ReindexButton',
serverProps: {
apiBasePath,
collectionLabels,
searchCollections,
searchSlug,
},
},
],
},
},
},
defaultColumns: ['title'],
description:
'This is a collection of automatically created search results. These results are used by the global site search and will be updated automatically as documents in the CMS are created or updated.',
enableRichTextRelationship: false,
useAsTitle: 'title',
...(pluginConfig?.searchOverrides?.admin || {}),
},
endpoints: [
...(pluginConfig?.searchOverrides?.endpoints || []),
{
handler: generateReindexHandler(pluginConfig),
method: 'post',
path: '/reindex',
},
],
fields:
pluginConfig?.searchOverrides?.fields &&
typeof pluginConfig?.searchOverrides?.fields === 'function'
? pluginConfig?.searchOverrides.fields({ defaultFields })
: defaultFields,
labels: {
...(pluginConfig?.searchOverrides?.labels || {
plural: 'Search Results',
singular: 'Search Result',
}),
},
}
return newConfig
}