Skip to content
Open
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
50 changes: 18 additions & 32 deletions src/app/(payload)/admin/importMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,25 @@ import { HeadingFeatureClient as HeadingFeatureClient_e70f5e05f09f93e00b997edb1e
import { FolderTypeField as FolderTypeField_2b8867833a34864a02ddf429b0728a40 } from '@payloadcms/next/client'
import { default as default_1a7510af427896d367a49dbf838d2de6 } from '@/components/BeforeDashboard'
import { default as default_8a7ab0eb7ab5c511aba12e68480bfe5e } from '@/components/BeforeLogin'
import { S3ClientUploadHandler as S3ClientUploadHandler_f97aa6c64367fa259c5bc0567239ef24 } from '@payloadcms/storage-s3/client'
import { CollectionCards as CollectionCards_f9c02e79a4aed9a3924487c0cd4cafb1 } from '@payloadcms/next/rsc'

export const importMap = {
'@payloadcms/richtext-lexical/rsc#RscEntryLexicalCell':
RscEntryLexicalCell_44fe37237e0ebf4470c9990d8cb7b07e,
'@payloadcms/richtext-lexical/rsc#RscEntryLexicalField':
RscEntryLexicalField_44fe37237e0ebf4470c9990d8cb7b07e,
'@payloadcms/richtext-lexical/rsc#LexicalDiffComponent':
LexicalDiffComponent_44fe37237e0ebf4470c9990d8cb7b07e,
'@payloadcms/richtext-lexical/client#InlineToolbarFeatureClient':
InlineToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#FixedToolbarFeatureClient':
FixedToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#ParagraphFeatureClient':
ParagraphFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#UnderlineFeatureClient':
UnderlineFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#BoldFeatureClient':
BoldFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#ItalicFeatureClient':
ItalicFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/richtext-lexical/client#LinkFeatureClient':
LinkFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/next/rsc#FolderTableCell': FolderTableCell_f9c02e79a4aed9a3924487c0cd4cafb1,
'@payloadcms/next/rsc#FolderField': FolderField_f9c02e79a4aed9a3924487c0cd4cafb1,
'@payloadcms/next/client#SlugField': SlugField_2b8867833a34864a02ddf429b0728a40,
'@payloadcms/richtext-lexical/client#HeadingFeatureClient':
HeadingFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
'@payloadcms/next/client#FolderTypeField': FolderTypeField_2b8867833a34864a02ddf429b0728a40,
'@/components/BeforeDashboard#default': default_1a7510af427896d367a49dbf838d2de6,
'@/components/BeforeLogin#default': default_8a7ab0eb7ab5c511aba12e68480bfe5e,
'@payloadcms/storage-s3/client#S3ClientUploadHandler':
S3ClientUploadHandler_f97aa6c64367fa259c5bc0567239ef24,
'@payloadcms/next/rsc#CollectionCards': CollectionCards_f9c02e79a4aed9a3924487c0cd4cafb1,
"@payloadcms/richtext-lexical/rsc#RscEntryLexicalCell": RscEntryLexicalCell_44fe37237e0ebf4470c9990d8cb7b07e,
"@payloadcms/richtext-lexical/rsc#RscEntryLexicalField": RscEntryLexicalField_44fe37237e0ebf4470c9990d8cb7b07e,
"@payloadcms/richtext-lexical/rsc#LexicalDiffComponent": LexicalDiffComponent_44fe37237e0ebf4470c9990d8cb7b07e,
"@payloadcms/richtext-lexical/client#InlineToolbarFeatureClient": InlineToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#FixedToolbarFeatureClient": FixedToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#ParagraphFeatureClient": ParagraphFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#UnderlineFeatureClient": UnderlineFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#BoldFeatureClient": BoldFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#ItalicFeatureClient": ItalicFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/richtext-lexical/client#LinkFeatureClient": LinkFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/next/rsc#FolderTableCell": FolderTableCell_f9c02e79a4aed9a3924487c0cd4cafb1,
"@payloadcms/next/rsc#FolderField": FolderField_f9c02e79a4aed9a3924487c0cd4cafb1,
"@payloadcms/next/client#SlugField": SlugField_2b8867833a34864a02ddf429b0728a40,
"@payloadcms/richtext-lexical/client#HeadingFeatureClient": HeadingFeatureClient_e70f5e05f09f93e00b997edb1ef0c864,
"@payloadcms/next/client#FolderTypeField": FolderTypeField_2b8867833a34864a02ddf429b0728a40,
"@/components/BeforeDashboard#default": default_1a7510af427896d367a49dbf838d2de6,
"@/components/BeforeLogin#default": default_8a7ab0eb7ab5c511aba12e68480bfe5e,
"@payloadcms/next/rsc#CollectionCards": CollectionCards_f9c02e79a4aed9a3924487c0cd4cafb1
}
55 changes: 55 additions & 0 deletions src/globals/durianpy-website/HomepageConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { checkCollectionAccess } from '@/access/checkCollectionAccess'
import { getCollectionGroupLabel } from '@/constants/collections'
import { GlobalConfig } from 'payload'

// We will let VS Code import these automatically!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment

// 1. Click on getCollectionGroupLabel below, press Ctrl + . (or Cmd + .), and click "Update import" or "Add import"
// 2. Click on checkCollectionAccess below, press Ctrl + . (or Cmd + .), and click "Add import"

export const HomepageConfig: GlobalConfig = {
slug: 'homepage-config',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a constant from our src/constants directory for the slug instead of a hardcoded string.

Also, let's update the literal slug value to 'durianpy-website-homepage-config' to avoid any potential naming collisions. To keep the UI clean, we should explicitly set the admin label to "Homepage Config" so the longer slug doesn't show up in the admin panel.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, let's create a new src/constants/globals.ts file to store our global slugs and labels. This will help us avoid hardcoding strings and keep things consistent.

You can take inspiration from how we structure src/constants/collections.ts. Here is a sample of what it could look like:

export const GLOBALS = {
  DURIANPY_WEBSITE_HOMEPAGE_CONFIG: 'durianpy-website-homepage-config',
} as const

export type GlobalSlug = (typeof GLOBALS)[keyof typeof GLOBALS]

export const GLOBAL_LABELS: Record<GlobalSlug, { singular: string; plural: string }> = {
  [GLOBALS.DURIANPY_WEBSITE_HOMEPAGE_CONFIG]: { singular: 'Homepage Config', plural: 'Homepage Configs' },
}

Once created, you can use it in HomepageConfig.ts like this:

import { GLOBALS, GLOBAL_LABELS } from '@/constants/globals'

export const HomepageConfig: GlobalConfig = {
  slug: GLOBALS.DURIANPY_WEBSITE_HOMEPAGE_CONFIG,
  label: GLOBAL_LABELS[GLOBALS.DURIANPY_WEBSITE_HOMEPAGE_CONFIG].singular, 
  admin: {
    // ...
  },
  // ...
}

admin: {
group: getCollectionGroupLabel('durianpy-website'),
},
access: {
read: () => true,
// We explicitly type args as 'any' to satisfy TS, and removed the invalid 'admin' access rule!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment

update: (args: any) => checkCollectionAccess(args, 'homepage-config' as any, 'update'),

Check warning on line 17 in src/globals/durianpy-website/HomepageConfig.ts

View workflow job for this annotation

GitHub Actions / Validation & Security

Unexpected any. Specify a different type

Check warning on line 17 in src/globals/durianpy-website/HomepageConfig.ts

View workflow job for this annotation

GitHub Actions / Validation & Security

Unexpected any. Specify a different type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor the access control to use the same higher-order function pattern we use in sample-website-collection.index.ts. This will also allow us to drop the any types.

It should look something like this:

const checkHomepageConfigAccess = (accessType?: AccessType) => (access: AccessArgs) =>
  checkCollectionAccess(access, GLOBALS.DURIANPY_WEBSITE_HOMEPAGE_CONFIG, accessType)

// ... inside the config:
  access: {
    read: anyone,
    update: checkHomepageConfigAccess('update'),
  },

},
versions: {
// Globals use 'max' instead of 'maxPerDoc'
max: 50,
drafts: {
autosave: {
showSaveDraftButton: true,
},
schedulePublish: true,
},
},
fields: [
{
name: 'heroImageDesktop',
type: 'upload',
relationTo: 'media',
required: true,
label: 'Hero Image (Desktop)',
},
{
name: 'heroImageMobile',
type: 'upload',
relationTo: 'media',
required: true,
label: 'Hero Image (Mobile)',
},
{
name: 'heroTitle',
type: 'text',
label: 'Hero Title',
},
{
name: 'heroSubtitle',
type: 'text',
label: 'Hero Subtitle',
},
],
}
38 changes: 35 additions & 3 deletions src/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ export interface Config {
defaultIDType: string;
};
fallbackLocale: null;
globals: {};
globalsSelect: {};
globals: {
'homepage-config': HomepageConfig;
};
globalsSelect: {
'homepage-config': HomepageConfigSelect<false> | HomepageConfigSelect<true>;
};
locale: null;
widgets: {
collections: CollectionsWidget;
Expand Down Expand Up @@ -1095,6 +1099,34 @@ export interface PayloadMigrationsSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "homepage-config".
*/
export interface HomepageConfig {
id: string;
heroImageDesktop: string | Media;
heroImageMobile: string | Media;
heroTitle?: string | null;
heroSubtitle?: string | null;
_status?: ('draft' | 'published') | null;
updatedAt?: string | null;
createdAt?: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "homepage-config_select".
*/
export interface HomepageConfigSelect<T extends boolean = true> {
heroImageDesktop?: T;
heroImageMobile?: T;
heroTitle?: T;
heroSubtitle?: T;
_status?: T;
updatedAt?: T;
createdAt?: T;
globalType?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "collections_widget".
Expand All @@ -1117,7 +1149,7 @@ export interface TaskSchedulePublish {
relationTo: 'sample';
value: string | Sample;
} | null;
global?: string | null;
global?: 'homepage-config' | null;
user?: (string | null) | User;
};
output?: unknown;
Expand Down
2 changes: 2 additions & 0 deletions src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { plugins } from './plugins'
import { defaultLexical } from '@/fields/defaultLexical'
import { getServerSideURL, getServerSideOrigin } from './utilities/getURL'
import { Sample } from './collections/durianpy-website/sample-website-collection.index'
import { HomepageConfig } from './globals/durianpy-website/HomepageConfig'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -94,6 +95,7 @@ export default buildConfig({
}),
}),
collections: [Media, Categories, Users, Sample],
globals: [HomepageConfig],
cors: [getServerSideOrigin()].filter(Boolean).map((url) => {
try {
const { origin } = new URL(url!)
Expand Down
Loading