81 lines
No EOL
1.6 KiB
TypeScript
81 lines
No EOL
1.6 KiB
TypeScript
import { getPayload } from 'payload'
|
|
import config from '@/payload.config'
|
|
|
|
/**
|
|
* Fetches blog posts based on given criteria.
|
|
*
|
|
* @param {boolean} displayOnFrontpage - Indicates whether to display posts on the front page.
|
|
*/
|
|
export const fetchBlogPosts = async (displayOnFrontpage: boolean) => {
|
|
const today = new Date()
|
|
today.setHours(23, 59)
|
|
|
|
const query: any = {
|
|
and: [
|
|
{
|
|
'_status': {
|
|
equals: 'published',
|
|
}
|
|
},
|
|
{
|
|
or: [
|
|
{
|
|
'configuration.displayFromDate': {
|
|
equals: null,
|
|
},
|
|
},
|
|
{
|
|
'configuration.displayFromDate': {
|
|
less_than_equal: today.toISOString(),
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
or: [
|
|
{
|
|
'configuration.displayTillDate': {
|
|
equals: null,
|
|
},
|
|
},
|
|
{
|
|
'configuration.displayTillDate': {
|
|
greater_than_equal: today.toISOString(),
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
if (displayOnFrontpage) {
|
|
query.and.push({
|
|
'configuration.showOnFrontpage': {
|
|
equals: true,
|
|
},
|
|
})
|
|
}
|
|
|
|
const payload = await getPayload({ config })
|
|
return payload.find({
|
|
collection: 'blog',
|
|
sort: '-date',
|
|
select: {
|
|
title: true,
|
|
date: true,
|
|
photo: true,
|
|
content: displayOnFrontpage ? undefined : true,
|
|
},
|
|
where: query,
|
|
limit: 18,
|
|
})
|
|
}
|
|
|
|
export async function fetchBlog(id: string, draft: boolean) {
|
|
const payload = await getPayload({ config })
|
|
return await payload.findByID({
|
|
collection: 'blog',
|
|
id: id,
|
|
draft,
|
|
})
|
|
} |