church-website/src/fetch/highlights.ts
2024-12-12 19:13:58 +01:00

64 lines
No EOL
1.4 KiB
TypeScript

import { stringify } from 'qs-esm'
import { PaginatedDocs } from 'payload'
import { Highlight } from '@/payload-types'
export const fetchHighlights = async (): Promise<PaginatedDocs<Highlight> | undefined> => {
const date = new Date();
date.setHours(0, 0, 0, 0);
const query: any = {
and: [
{
from: {
less_than_equal: date.toISOString(),
},
until: {
greater_than_equal: date.toISOString(),
}
}
],
}
const stringifiedQuery = stringify(
{
sort: "date",
where: query,
limit: 3
},
{ addQueryPrefix: true },
)
const response = await fetch(`http://localhost:3000/api/highlight${stringifiedQuery}`)
if (!response.ok) return undefined
return response.json()
}
export const fetchHighlightsBetweenDates = async (from: Date, until: Date): Promise<PaginatedDocs<Highlight> | undefined> => {
const query: any = {
and: [
{
date: {
greater_than_equal: from.toISOString(),
}
},
{
date: {
less_than: until.toISOString(),
}
}
],
}
const stringifiedQuery = stringify(
{
sort: "date",
where: query,
limit: 5
},
{ addQueryPrefix: true },
)
const response = await fetch(`http://localhost:3000/api/highlight${stringifiedQuery}`)
if (!response.ok) return undefined
return response.json()
}