diff --git a/package-lock.json b/package-lock.json index 17480d8..cb666c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@nyariv/sandboxjs": "0.8.28", "@payloadcms/db-postgres": "^3.74.0", - "@payloadcms/live-preview-react": "^3.79.0", + "@payloadcms/live-preview-react": "^3.74.0", "@payloadcms/next": "^3.74.0", "@payloadcms/richtext-lexical": "^3.74.0", "@payloadcms/storage-gcs": "^3.74.0", diff --git a/package.json b/package.json index 8d2d55d..aaad524 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "dependencies": { "@nyariv/sandboxjs": "0.8.28", "@payloadcms/db-postgres": "^3.74.0", - "@payloadcms/live-preview-react": "^3.79.0", + "@payloadcms/live-preview-react": "^3.74.0", "@payloadcms/next": "^3.74.0", "@payloadcms/richtext-lexical": "^3.74.0", "@payloadcms/storage-gcs": "^3.74.0", @@ -31,7 +31,6 @@ "moment": "^2.30.1", "next": "15.4.11", "payload": "^3.74.0", - "qs-esm": "^7.0.3", "react": "19.2.4", "react-dom": "19.2.4", "resend": "^6.9.1", diff --git a/src/admin/components/calendar/Calendar.tsx b/src/admin/components/calendar/Calendar.tsx deleted file mode 100644 index 7417e12..0000000 --- a/src/admin/components/calendar/Calendar.tsx +++ /dev/null @@ -1,40 +0,0 @@ -'use client' - -import { useEffect, useState } from 'react' -import moment from 'moment' -import Day from '@/admin/components/calendar/Day' - - -const Calendar = () => { - const weekNr = moment().isoWeek(); - const [week, setWeek] = useState(weekNr.toString()) - const [days, setDays] = useState([]) - - // on week change => sey days - useEffect(() => { - const date = moment().isoWeek(Number(week)); - const newDays = []; - - for(let i=0; i<7; i++) { - let day = date.weekday(i).toISOString(); - newDays.push(day) - } - - setDays(newDays) - }, [week, setDays]) - - return ( -
-
- -
- - - {days.map(day => )} -
- ) -} - -export default Calendar \ No newline at end of file diff --git a/src/admin/components/calendar/ChurchSelect/ChurchSelect.tsx b/src/admin/components/calendar/ChurchSelect/ChurchSelect.tsx deleted file mode 100644 index c69ee4c..0000000 --- a/src/admin/components/calendar/ChurchSelect/ChurchSelect.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { useSyncExternalStore } from 'react' -import { churchStore } from '@/admin/components/calendar/ChurchSelect/churchStore' - -type ChurchSelectProps = { - value: string, - className?: string, - onChange: (value: string) => void, -} - -export const ChurchSelect = ({value, onChange, className}: ChurchSelectProps) => { - const churches = useSyncExternalStore(churchStore.subscribe, churchStore.getSnapshot) - - return ( - - ) -} \ No newline at end of file diff --git a/src/admin/components/calendar/ChurchSelect/churchStore.ts b/src/admin/components/calendar/ChurchSelect/churchStore.ts deleted file mode 100644 index 05834b4..0000000 --- a/src/admin/components/calendar/ChurchSelect/churchStore.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { fetchChurches } from '@/fetch/churches' - -type Church = { - id: string, - name: string -} - -let churches: Church[] = []; -const listeners = new Set<() => void>(); - -/** - * ChurchStore to use with Reacts `useSyncExternalStore` - */ -export const churchStore = { - // fetch all churches from API - async init() { - const data = await fetchChurches(); - - if (data) { - churches = data.docs.map(c => { - return { id: c.id, name: c.name } - }); - - emitChange() - } - }, - subscribe: (listener: () => void) => { - listeners.add(listener); - return () => listeners.delete(listener); - }, - getSnapshot() { - return churches; - } -} - -function emitChange() { - for (let listener of listeners) { - listener(); - } -} - -await churchStore.init(); \ No newline at end of file diff --git a/src/admin/components/calendar/Day.tsx b/src/admin/components/calendar/Day.tsx deleted file mode 100644 index 2678a79..0000000 --- a/src/admin/components/calendar/Day.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import { liturgicalDayName } from '@/hooks/liturgicalDayName' -import styles from "./styles.module.scss" -import MassForm from '@/admin/components/calendar/MassForm' - -type DayProps = { - date: string, - mass: Mass[] -} - -type Mass = { - id: string, - -} - -export const Day = ({date}: DayProps) => { - const dayName = liturgicalDayName(date) - const dateObj = new Date(date); - - - return ( -
-
- {dateObj.toLocaleDateString("de-De", {weekday: "long"})}
- {dayName} -
-
- {date.substring(8, 10)} -
-
- -
-
- ) -} - -export default Day \ No newline at end of file diff --git a/src/admin/components/calendar/MassForm.tsx b/src/admin/components/calendar/MassForm.tsx deleted file mode 100644 index d8411f2..0000000 --- a/src/admin/components/calendar/MassForm.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import styles from "./form.module.scss" -import { ChurchSelect } from '@/admin/components/calendar/ChurchSelect/ChurchSelect' - -const MassForm = () => { - return ( -
-
-
- -
-
- console.log("chage")} - /> -
-
- -
- -
- - - - - - - - -
- -
- -
-
- -
-
- -
-
- -
- -
-
- ) -} - -export default MassForm \ No newline at end of file diff --git a/src/admin/components/calendar/form.module.scss b/src/admin/components/calendar/form.module.scss deleted file mode 100644 index 11943f4..0000000 --- a/src/admin/components/calendar/form.module.scss +++ /dev/null @@ -1,30 +0,0 @@ -.container { - display: flex; -} - -.time { - width: 130px; - text-align: center; -} - -.formRow { - margin-bottom: 5px; -} - -.input { - font-size: 12px; - padding: 3px; - width: 350px; - border: 1px solid #ababab; - border-radius: 4px; -} - -.select { - font-size: 12px; - padding: 3px; - border: 1px solid #ababab; - border-radius: 4px; - width: 100px; - background-color: #ffffff; - font-family: inherit; -} \ No newline at end of file diff --git a/src/admin/components/calendar/styles.module.scss b/src/admin/components/calendar/styles.module.scss deleted file mode 100644 index 0778b31..0000000 --- a/src/admin/components/calendar/styles.module.scss +++ /dev/null @@ -1,13 +0,0 @@ -.container { - display: flex; - margin-bottom: 20px; -} - -.day { - width: 300px; -} - -.date { - font-size: 18px; - font-weight: bold; -} \ No newline at end of file diff --git a/src/admin/components/test/TestView.tsx b/src/admin/components/test/TestView.tsx deleted file mode 100644 index 2c9a7f9..0000000 --- a/src/admin/components/test/TestView.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import { Gutter } from '@payloadcms/ui' -import { DefaultTemplate } from '@payloadcms/next/templates' -import { AdminViewProps } from 'payload' -import Calendar from '@/admin/components/calendar/Calendar' - -export default function TestPage({ - initPageResult, - params, - searchParams, - }: AdminViewProps) { - const { - req: { - user - } - } = initPageResult - - if (!user) { - return

You must be logged in to view this page.

- } - - return ( - - -

Gottesdiensten

-

This view uses the Default Template.

- - -
-
- ) -} diff --git a/src/app/(home)/gottesdienst/[id]/page.tsx b/src/app/(home)/gottesdienst/[id]/page.tsx index 1d12ec7..afb7fb9 100644 --- a/src/app/(home)/gottesdienst/[id]/page.tsx +++ b/src/app/(home)/gottesdienst/[id]/page.tsx @@ -1,19 +1,19 @@ import { notFound } from 'next/navigation' -import { Worship as WorshipType } from '@/payload-types' import { Worship } from '@/pageComponents/Worship/Worship' import { isAuthenticated } from '@/utils/auth' import { AdminMenu } from '@/components/AdminMenu/AdminMenu' +import { fetchWorshipById } from '@/fetch/worship' export default async function WorshipPage({ params }: { params: Promise<{id: string}>}) { const id = (await params).id; - const res = await fetch(`http://localhost:3000/api/worship/${id}`); - if (!res.ok) { + const worship = await fetchWorshipById(id) + + if (!worship) { notFound() } - const authenticated = await isAuthenticated(); - const worship = await res.json() as WorshipType; + const authenticated = await isAuthenticated(); return ( <> @@ -27,4 +27,4 @@ export default async function WorshipPage({ params }: { params: Promise<{id: str /> ) -} \ No newline at end of file +} diff --git a/src/app/(home)/veranstaltungen/[id]/page.tsx b/src/app/(home)/veranstaltungen/[id]/page.tsx index 712e4b3..19ddb95 100644 --- a/src/app/(home)/veranstaltungen/[id]/page.tsx +++ b/src/app/(home)/veranstaltungen/[id]/page.tsx @@ -1,43 +1,20 @@ import { notFound } from 'next/navigation' -import { Event } from '@/payload-types' import { EventPage } from '@/pageComponents/Event/Event' -import { stringify } from 'qs-esm' import { getPhoto } from '@/utils/dto/gallery' -import { cookies } from 'next/headers' import { isAuthenticated } from '@/utils/auth' +import { fetchEventById } from '@/fetch/events' export default async function Page({ params }: { params: Promise<{id: string}>}) { const id = (await params).id; - const stringifiedQuery = stringify( - { - select: { - title: true, - date: true, - createdAt: true, - cancelled: true, - isRecurring: true, - location: true, - description: true, - shortDescription: true, - contact: true, - flyer: true, - photo: true, - group: true, - rsvpLink: true - }, - }, - { addQueryPrefix: true }, - ) - const res = await fetch(`http://localhost:3000/api/event/${id}${stringifiedQuery}`); + const event = await fetchEventById(id) - if (!res.ok) { + if (!event) { notFound() } const authenticated = await isAuthenticated(); - const event = await res.json() as Event; const group = Array.isArray(event.group) && event.group.length > 0 && typeof event.group[0] == "object" ? event.group[0].slug : undefined; const photo = getPhoto("tablet", event.photo); @@ -60,4 +37,4 @@ export default async function Page({ params }: { params: Promise<{id: string}>}) isAuthenticated={authenticated} /> ) -} \ No newline at end of file +} diff --git a/src/collections/Events.ts b/src/collections/Events.ts index 14a49b9..634400e 100644 --- a/src/collections/Events.ts +++ b/src/collections/Events.ts @@ -1,6 +1,6 @@ import { CollectionConfig } from 'payload' -import { stringify } from 'qs-esm' -import { Event, Group, User } from '@/payload-types' +import { Group, User } from '@/payload-types' +import { fetchEventById } from '@/fetch/events' export const Events: CollectionConfig = { slug: 'event', @@ -198,7 +198,7 @@ export const Events: CollectionConfig = { return false } - const event = await fetchEvent(id) + const event = await fetchEventById(id) if (hasGroup(user, event)) { return true } @@ -235,20 +235,3 @@ const hasGroup = (user: null | User , data: Partial | undefined) => { }) } -/** - * Fetch event - * @param id - */ -const fetchEvent = async (id: string): Promise => { - const stringifiedQuery = stringify( - { - select: { - group: true, - }, - }, - { addQueryPrefix: true }, - ) - const res = await fetch(`http://localhost:3000/api/event/${id}${stringifiedQuery}`); - if (!res.ok) return undefined - return res.json() -} \ No newline at end of file diff --git a/src/fetch/announcement.ts b/src/fetch/announcement.ts index e09cd88..6b2eb6e 100644 --- a/src/fetch/announcement.ts +++ b/src/fetch/announcement.ts @@ -1,87 +1,75 @@ -import { stringify } from 'qs-esm' +import { getPayload } from 'payload' +import config from '@/payload.config' import { Announcement } from '@/payload-types' -import { PaginatedDocs } from 'payload' /** * Fetch last announcement for a parish */ -export const fetchLastAnnouncement = async (parishId: string): Promise => { - const date = new Date(); +export const fetchLastAnnouncement = async ( + parishId: string, +): Promise => { + const date = new Date() date.setDate(date.getDate() - 14) - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - tomorrow.setHours(23,59,59,59); + const tomorrow = new Date() + tomorrow.setDate(tomorrow.getDate() + 1) + tomorrow.setHours(23, 59, 59, 59) - const query: any = { - and: [ - { - parish: { - equals: parishId - } - }, - { - date: { - greater_than_equal: date.toISOString(), - } - }, - { - date: { - less_than_equal: tomorrow.toISOString() - } - } - ] - } - - const stringifiedQuery = stringify( - { - sort: "-date", - where: query, - limit: 1, + const payload = await getPayload({ config }) + const result = await payload.find({ + collection: 'announcement', + sort: '-date', + where: { + and: [ + { + parish: { + equals: parishId, + }, + }, + { + date: { + greater_than_equal: date.toISOString(), + }, + }, + { + date: { + less_than_equal: tomorrow.toISOString(), + }, + }, + ], }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/announcement${stringifiedQuery}`) - if (!response.ok) return undefined - const announcements = await response.json() as PaginatedDocs - return announcements.docs[0] + limit: 1, + }) + return result.docs[0] } /** * Fetch the last few announcements */ -export const fetchLastAnnouncements = async (): Promise | undefined> => { - const date = new Date(); +export const fetchLastAnnouncements = async () => { + const date = new Date() date.setDate(date.getDate() - 14) - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - tomorrow.setHours(23,59,59,59); + const tomorrow = new Date() + tomorrow.setDate(tomorrow.getDate() + 1) + tomorrow.setHours(23, 59, 59, 59) - const query: any = { - and: [ - { - date: { - greater_than_equal: date.toISOString(), - } - }, - { - date: { - less_than_equal: tomorrow.toISOString() - } - } - ] - } - - const stringifiedQuery = stringify( - { - sort: "-date", - where: query, - limit: 3, + const payload = await getPayload({ config }) + return payload.find({ + collection: 'announcement', + sort: '-date', + where: { + and: [ + { + date: { + greater_than_equal: date.toISOString(), + }, + }, + { + date: { + less_than_equal: tomorrow.toISOString(), + }, + }, + ], }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/announcement${stringifiedQuery}`) - if (!response.ok) return undefined - return await response.json() as PaginatedDocs -} \ No newline at end of file + limit: 3, + }) +} diff --git a/src/fetch/blog.ts b/src/fetch/blog.ts index 424aefd..3259ef2 100644 --- a/src/fetch/blog.ts +++ b/src/fetch/blog.ts @@ -1,73 +1,67 @@ -import { Blog } from '@/payload-types' -import { PaginatedDocs } from 'payload' -import { stringify } from 'qs-esm' +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. - * @returns {Promise | undefined>} - A Promise that resolves to the paginated list of blog posts, or undefined if an error occurs. */ -export const fetchBlogPosts = async (displayOnFrontpage: boolean): Promise | undefined> => { - const today = new Date(); - today.setHours(23, 59); +export const fetchBlogPosts = async (displayOnFrontpage: boolean) => { + const today = new Date() + today.setHours(23, 59) - const query: any = - { - sort: "-date", - select: { - title: true, - date: true, - photo: true, - content: displayOnFrontpage ? undefined : true, // hack to fetch content only for the `/blog` page - }, - where: { - and: [ + const query: any = { + and: [ + { + or: [ { - or: [ - { - "configuration.displayFromDate": { - equals: null - } - }, - { - "configuration.displayFromDate": { - less_than_equal: today.toISOString(), - } - } - ] + 'configuration.displayFromDate': { + equals: null, + }, }, { - or: [ - { - "configuration.displayTillDate": { - equals: null - } - }, - { - "configuration.displayTillDate": { - greater_than_equal: today.toISOString(), - } - } - ] - } + 'configuration.displayFromDate': { + less_than_equal: today.toISOString(), + }, + }, ], }, - limit: 18 - }; + { + or: [ + { + 'configuration.displayTillDate': { + equals: null, + }, + }, + { + 'configuration.displayTillDate': { + greater_than_equal: today.toISOString(), + }, + }, + ], + }, + ], + } - if(displayOnFrontpage) { - query.where.and.push({ - "configuration.showOnFrontpage": { - equals: true + if (displayOnFrontpage) { + query.and.push({ + 'configuration.showOnFrontpage': { + equals: true, }, }) } - const stringifiedQuery = stringify(query, {addQueryPrefix: true}) - - - const resp = await fetch(`http://localhost:3000/api/blog${stringifiedQuery}`); - if (!resp.ok) return undefined; - return resp.json(); -} \ No newline at end of file + 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, + }) +} diff --git a/src/fetch/calendar.ts b/src/fetch/calendar.ts index ed42859..9354520 100644 --- a/src/fetch/calendar.ts +++ b/src/fetch/calendar.ts @@ -1,89 +1,75 @@ -import { stringify } from 'qs-esm' -import { PaginatedDocs } from 'payload' +import { getPayload } from 'payload' +import config from '@/payload.config' import { Calendar } from '@/payload-types' /** * Fetch last calendar for a parish */ -export const fetchLastCalendar = async (parishId: string): Promise => { - const date = new Date(); - date.setDate(date.getDate() - 14); - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - tomorrow.setHours(23,59,59,59); +export const fetchLastCalendar = async ( + parishId: string, +): Promise => { + const date = new Date() + date.setDate(date.getDate() - 14) + const tomorrow = new Date() + tomorrow.setDate(tomorrow.getDate() + 1) + tomorrow.setHours(23, 59, 59, 59) - const query: any = { - and: [ - { - parish: { - equals: parishId - } - }, - { - date: { - greater_than_equal: date.toISOString(), - } - }, - { - date: { - less_than_equal: tomorrow.toISOString() - } - } - ] - - } - - const stringifiedQuery = stringify( - { - sort: "-date", - where: query, - limit: 1, + const payload = await getPayload({ config }) + const result = await payload.find({ + collection: 'calendar', + sort: '-date', + where: { + and: [ + { + parish: { + equals: parishId, + }, + }, + { + date: { + greater_than_equal: date.toISOString(), + }, + }, + { + date: { + less_than_equal: tomorrow.toISOString(), + }, + }, + ], }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/calendar${stringifiedQuery}`) - if (!response.ok) return undefined - const announcements = await response.json() as PaginatedDocs - return announcements.docs[0] + limit: 1, + }) + return result.docs[0] } /** * Fetch last calendars */ -export const fetchLastCalendars = async (): Promise | undefined> => { - const date = new Date(); - date.setDate(date.getDate() - 14); - const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - tomorrow.setHours(23,59,59,59); +export const fetchLastCalendars = async () => { + const date = new Date() + date.setDate(date.getDate() - 14) + const tomorrow = new Date() + tomorrow.setDate(tomorrow.getDate() + 1) + tomorrow.setHours(23, 59, 59, 59) - const query: any = { - and: [ - { - date: { - greater_than_equal: date.toISOString(), - } - }, - { - date: { - less_than_equal: tomorrow.toISOString() - } - } - ] - - } - - const stringifiedQuery = stringify( - { - sort: "-date", - where: query, - limit: 3, + const payload = await getPayload({ config }) + return payload.find({ + collection: 'calendar', + sort: '-date', + where: { + and: [ + { + date: { + greater_than_equal: date.toISOString(), + }, + }, + { + date: { + less_than_equal: tomorrow.toISOString(), + }, + }, + ], }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/calendar${stringifiedQuery}`) - if (!response.ok) return undefined - return await response.json() as PaginatedDocs -} \ No newline at end of file + limit: 3, + }) +} diff --git a/src/fetch/churches.ts b/src/fetch/churches.ts deleted file mode 100644 index 63acc08..0000000 --- a/src/fetch/churches.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Church } from '@/payload-types' -import { PaginatedDocs } from 'payload' -import { stringify } from 'qs-esm' - -export const fetchChurches = async (): Promise | undefined> => { - const stringifiedQuery = stringify( - { - sort: "name", - limit: 50 - }, - { addQueryPrefix: true }, - ) - - const resp = await fetch(`http://localhost:3000/api/church`); - if (!resp.ok) return undefined; - return resp.json(); -} \ No newline at end of file diff --git a/src/fetch/classifieds.ts b/src/fetch/classifieds.ts index dcf7a65..909fbab 100644 --- a/src/fetch/classifieds.ts +++ b/src/fetch/classifieds.ts @@ -1,27 +1,19 @@ -import { PaginatedDocs } from 'payload' -import { Classified } from '@/payload-types' -import { stringify } from 'qs-esm' +import { getPayload } from 'payload' +import config from '@/payload.config' -export const fetchClassifieds = async (): Promise | undefined> => { - const date = new Date(); - date.setHours(0, 0, 0, 0); +export const fetchClassifieds = async () => { + const date = new Date() + date.setHours(0, 0, 0, 0) - const query = { - until: { - greater_than_equal: date.toISOString(), - } - } - - const stringifiedQuery = stringify( - { - sort: "date", - where: query, - limit: 50 + const payload = await getPayload({ config }) + return payload.find({ + collection: 'classifieds', + sort: 'date', + where: { + until: { + greater_than_equal: date.toISOString(), + }, }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/classifieds${stringifiedQuery}`) - if (!response.ok) return undefined - return response.json() + limit: 50, + }) } diff --git a/src/fetch/config.ts b/src/fetch/config.ts index 3c45c4e..29d6460 100644 --- a/src/fetch/config.ts +++ b/src/fetch/config.ts @@ -1,13 +1,13 @@ +import { unstable_cache } from 'next/cache' +import { getPayload } from 'payload' +import config from '@/payload.config' import { Menu } from '@/payload-types' -export const fetchMenu = async (): Promise => { - const rep = await fetch( - "http://localhost:3000/api/globals/menu", - { next: { tags: ['menu'] } } // cache fetch result - ); - - if (!rep.ok) { - throw new Error("Could not fetch menu") - } - return await rep.json() -} \ No newline at end of file +export const fetchMenu = unstable_cache( + async (): Promise => { + const payload = await getPayload({ config }) + return payload.findGlobal({ slug: 'menu' }) + }, + ['menu'], + { tags: ['menu'] }, +) diff --git a/src/fetch/donationform.ts b/src/fetch/donationform.ts index 23e13bd..b2e5785 100644 --- a/src/fetch/donationform.ts +++ b/src/fetch/donationform.ts @@ -1,21 +1,17 @@ +import { getPayload } from 'payload' +import config from '@/payload.config' import { DonationForm } from '@/payload-types' -export async function fetchDonationForm(id: string): Promise { - // const query = { - // id: { - // equals: id, - // }, - // } - // - // const stringifiedQuery = stringify( - // { - // where: query, - // }, - // { addQueryPrefix: true }, - // ) - - const res = await fetch(`http://localhost:3000/api/donation-form/${id}`); - if (!res.ok) return undefined - const response = await res.json() as DonationForm; - return response -} \ No newline at end of file +export async function fetchDonationForm( + id: string, +): Promise { + try { + const payload = await getPayload({ config }) + return await payload.findByID({ + collection: 'donation-form', + id, + }) + } catch { + return undefined + } +} diff --git a/src/fetch/events.ts b/src/fetch/events.ts index ca15d17..c8a7666 100644 --- a/src/fetch/events.ts +++ b/src/fetch/events.ts @@ -1,25 +1,30 @@ -import { stringify } from 'qs-esm' -import { PaginatedDocs } from 'payload' +import { getPayload, PaginatedDocs } from 'payload' +import config from '@/payload.config' import { Event } from '@/payload-types' type Args = { - parishId?: string; - groupId?: string; - limit?: number; - page?: number; + parishId?: string + groupId?: string + limit?: number + page?: number fromDate?: Date toDate?: Date } - /** * Fetch a list of events - * */ -export async function fetchEvents(args?: Args): Promise | undefined> { - - const {parishId, groupId, limit = 30, page = 0, fromDate = new Date(), toDate} = args || {}; - +export async function fetchEvents( + args?: Args, +): Promise> { + const { + parishId, + groupId, + limit = 30, + page = 0, + fromDate = new Date(), + toDate, + } = args || {} const query: any = { and: [ @@ -27,7 +32,7 @@ export async function fetchEvents(args?: Args): Promise | u date: { greater_than_equal: fromDate.toISOString(), }, - } + }, ], } @@ -35,45 +40,53 @@ export async function fetchEvents(args?: Args): Promise | u query.and.push({ date: { less_than: toDate.toISOString(), - } + }, }) } if (parishId) { query.and.push({ - "parish": { - equals: parishId + parish: { + equals: parishId, }, }) } if (groupId) { query.and.push({ - "group": { - equals: groupId - } + group: { + equals: groupId, + }, }) } - - const stringifiedQuery = stringify( - { - sort: "date", - where: query, - select: { - location: true, - date: true, - title: true, - cancelled: true - }, - depth: 1, - limit, - page + const payload = await getPayload({ config }) + return payload.find({ + collection: 'event', + sort: 'date', + where: query, + select: { + location: true, + date: true, + title: true, + cancelled: true, }, - { addQueryPrefix: true }, - ) - - const response = await fetch(`http://localhost:3000/api/event${stringifiedQuery}`) - if (!response.ok) return undefined - return response.json() + depth: 1, + limit, + page, + }) as Promise> +} + +/** + * Fetch a single event by ID + */ +export async function fetchEventById( + id: string, +): Promise { + try { + const payload = await getPayload({ config }) + return await payload.findByID({ collection: 'event', id }) + } catch { + return undefined + } } diff --git a/src/fetch/footer.ts b/src/fetch/footer.ts index a9ea914..7e791c8 100644 --- a/src/fetch/footer.ts +++ b/src/fetch/footer.ts @@ -1,13 +1,13 @@ +import { unstable_cache } from 'next/cache' +import { getPayload } from 'payload' +import config from '@/payload.config' import { Footer } from '@/payload-types' -export async function fetchFooter(): Promise