Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/access/checkCollectionAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { COLLECTION_PERMISSION_TO_ACCESS_TYPES } from '@/constants/collectionPer

export function checkCollectionAccess(
{ req }: AccessArgs<User>,
collectionSlug: CollectionSlug,
collectionSlug: CollectionSlug | string,
accessType?: AccessType,
) {
const user = req.user
Expand Down
3 changes: 0 additions & 3 deletions src/app/(payload)/admin/importMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 = {
Expand Down Expand Up @@ -47,7 +46,5 @@ export const importMap = {
'@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,
}
33 changes: 33 additions & 0 deletions src/components/OrganizationStatus/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react'
import Image from 'next/image'
import { getGlobal } from '@/utilities/getGlobals'
import { getMediaUrl } from '@/utilities/getMediaUrl'
import type { Media } from '@/payload-types'

export async function OrganizationStatusComponent() {
const data = await getGlobal('organization-status', 1)

if (!data?.isPSFPartner) {
return null
}

const logoObj = typeof data.psfPartnerLogo === 'object' ? (data.psfPartnerLogo as Media) : null
const logoUrl = getMediaUrl(logoObj?.url)
const altText = logoObj?.alt || 'PSF Partner Logo'

if (!logoUrl) {
return null
}

return (
<div className="psf-partner-status flex items-center gap-2">
<Image
src={logoUrl}
alt={altText}
width={logoObj?.width || 120}
height={logoObj?.height || 40}
className="h-auto max-h-12 w-auto object-contain"
/>
</div>
)
}
4 changes: 2 additions & 2 deletions src/constants/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export function getCollectionGroupLabel(groupSlug: CollectionGroupSlug) {
return COLLECTION_GROUPS_LABEL[groupSlug]
}

export const COLLECTION_GROUP_ITEMS: Record<CollectionGroupSlug, CollectionSlug[]> = {
'durianpy-website': [],
export const COLLECTION_GROUP_ITEMS: Record<CollectionGroupSlug, string[]> = {
'durianpy-website': ['sample', 'organization-status'],
admin: ['users'],
} as const

Expand Down
68 changes: 68 additions & 0 deletions src/globals/durianpy-website/OrganizationStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { AccessArgs, GlobalConfig } from 'payload'

import { checkCollectionAccess } from '@/access/checkCollectionAccess'
import { AccessType } from '@/constants/accessTypes'
import { getCollectionGroupLabel } from '@/constants/collections'

const checkOrganizationStatusAccess = (accessType?: AccessType) => (access: AccessArgs) =>
checkCollectionAccess(access, 'organization-status', accessType)

export const OrganizationStatus: GlobalConfig = {
slug: 'organization-status',
access: {
read: (access: AccessArgs) => {
const isDraft =
Boolean((access.req as { draft?: boolean }).draft) || access.req.query?.draft === 'true'
if (isDraft) {
return checkOrganizationStatusAccess('read')(access)
}
return true
},
update: checkOrganizationStatusAccess('update'),
},
admin: {
group: getCollectionGroupLabel('durianpy-website'),
},
versions: {
drafts: {
autosave: {
showSaveDraftButton: true,
},
schedulePublish: true,
},
max: 50,
},
fields: [
{
name: 'isPSFPartner',
type: 'checkbox',
label: 'Is PSF Partner',
defaultValue: false,
},
{
name: 'psfPartnerLogo',
type: 'upload',
relationTo: 'media',
label: 'PSF Partner Logo',
admin: {
condition: (data) => Boolean(data?.isPSFPartner),
},
},
{
name: 'status',
type: 'select',
options: [
{ label: 'Active', value: 'active' },
{ label: 'Inactive', value: 'inactive' },
{ label: 'Pending', value: 'pending' },
],
defaultValue: 'active',
required: true,
},
{
name: 'message',
type: 'textarea',
label: 'Status Message',
},
],
}
40 changes: 36 additions & 4 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: {
'organization-status': OrganizationStatus;
};
globalsSelect: {
'organization-status': OrganizationStatusSelect<false> | OrganizationStatusSelect<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` "organization-status".
*/
export interface OrganizationStatus {
id: string;
isPSFPartner?: boolean | null;
psfPartnerLogo?: (string | null) | Media;
status: 'active' | 'inactive' | 'pending';
message?: string | null;
_status?: ('draft' | 'published') | null;
updatedAt?: string | null;
createdAt?: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "organization-status_select".
*/
export interface OrganizationStatusSelect<T extends boolean = true> {
isPSFPartner?: T;
psfPartnerLogo?: T;
status?: T;
message?: 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?: 'organization-status' | null;
user?: (string | null) | User;
};
output?: unknown;
Expand All @@ -1133,4 +1165,4 @@ export interface Auth {

declare module 'payload' {
export interface GeneratedTypes extends Config {}
}
}
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 { OrganizationStatus } from './globals/durianpy-website/OrganizationStatus'

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: [OrganizationStatus],
cors: [getServerSideOrigin()].filter(Boolean).map((url) => {
try {
const { origin } = new URL(url!)
Expand Down
25 changes: 25 additions & 0 deletions src/utilities/getGlobals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Config } from '@/payload-types'
import configPromise from '@payload-config'
import { getPayload } from 'payload'
import { draftMode } from 'next/headers'

type GlobalSlug = keyof Config['globals']

/**
* Fetches Global data inside Server Components with Next.js Draft Mode support.
*/
export async function getGlobal<T extends GlobalSlug>(
slug: T,
depth = 1,
): Promise<Config['globals'][T]> {
const { isEnabled: draft } = await draftMode()
const payload = await getPayload({ config: configPromise })

const globalData = await payload.findGlobal({
slug,
depth,
draft,
})

return globalData as Config['globals'][T]
}
Loading