feat: payload cms
This commit is contained in:
113
lib/pages.ts
113
lib/pages.ts
@@ -5,7 +5,9 @@ export interface PageFrontmatter {
|
||||
title: string;
|
||||
excerpt: string;
|
||||
featuredImage: string | null;
|
||||
locale: string;
|
||||
focalX?: number;
|
||||
focalY?: number;
|
||||
layout?: 'default' | 'fullBleed';
|
||||
public?: boolean;
|
||||
}
|
||||
|
||||
@@ -15,6 +17,30 @@ export interface PageMdx {
|
||||
content: any; // Lexical AST Document
|
||||
}
|
||||
|
||||
function mapDoc(doc: any): PageMdx {
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
layout: doc.layout || 'default',
|
||||
} as PageFrontmatter,
|
||||
content: doc.content as any,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getPageBySlug(slug: string, locale: string): Promise<PageMdx | null> {
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
@@ -23,30 +49,14 @@ export async function getPageBySlug(slug: string, locale: string): Promise<PageM
|
||||
collection: 'pages' as any,
|
||||
where: {
|
||||
slug: { equals: slug },
|
||||
locale: { equals: locale },
|
||||
},
|
||||
locale: locale as any,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
const docs = result.docs as any[];
|
||||
|
||||
if (!docs || docs.length === 0) return null;
|
||||
|
||||
const doc = docs[0];
|
||||
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
locale: doc.locale,
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
} as PageFrontmatter,
|
||||
content: doc.content as any, // Native Lexical Editor State
|
||||
};
|
||||
return mapDoc(docs[0]);
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getPageBySlug failed for ${slug}:`, error);
|
||||
return null;
|
||||
@@ -59,31 +69,11 @@ export async function getAllPages(locale: string): Promise<PageMdx[]> {
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'pages' as any,
|
||||
where: {
|
||||
locale: {
|
||||
equals: locale,
|
||||
},
|
||||
},
|
||||
locale: locale as any,
|
||||
limit: 100,
|
||||
});
|
||||
|
||||
const docs = result.docs as any[];
|
||||
|
||||
return docs.map((doc: any) => {
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
locale: doc.locale,
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
} as PageFrontmatter,
|
||||
content: doc.content as any,
|
||||
};
|
||||
});
|
||||
return (result.docs as any[]).map(mapDoc);
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllPages failed for ${locale}:`, error);
|
||||
return [];
|
||||
@@ -96,30 +86,29 @@ export async function getAllPagesMetadata(locale: string): Promise<Partial<PageM
|
||||
|
||||
const result = await payload.find({
|
||||
collection: 'pages' as any,
|
||||
where: {
|
||||
locale: {
|
||||
equals: locale,
|
||||
},
|
||||
},
|
||||
locale: locale as any,
|
||||
limit: 100,
|
||||
});
|
||||
|
||||
const docs = result.docs as any[];
|
||||
|
||||
return docs.map((doc: any) => {
|
||||
return {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
locale: doc.locale,
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
} as PageFrontmatter,
|
||||
};
|
||||
});
|
||||
return (result.docs as any[]).map((doc: any) => ({
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
excerpt: doc.excerpt || '',
|
||||
featuredImage:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
} as PageFrontmatter,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error(`[Payload] getAllPagesMetadata failed for ${locale}:`, error);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user