feat: ne montre que les évènements à venir

main
Jalil Arfaoui 2024-04-23 12:04:43 +02:00
parent cb5660ed19
commit 7f29cfccb6
1 changed files with 40 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import contentful, {type Entry, type EntryFieldTypes} from "contentful";
import type { Document } from '@contentful/rich-text-types';
export const contentfulClient = contentful.createClient({
space: import.meta.env.CONTENTFUL_SPACE_ID,
@ -21,8 +22,26 @@ export interface ContentFulEvenement {
}
export const evenementFromContentfull = ({ fields: { nom, slug, description, date, affiche, lieu, lieuUrl, position } }: Entry<ContentFulEvenement>) => ({
nom, slug, description, date, affiche, lieu, lieuUrl, position
export const evenementFromContentfull = ({
fields: {
nom,
slug,
description,
date,
affiche,
lieu,
lieuUrl,
position
}
}: Entry<ContentFulEvenement>): Évènement => ({
nom: nom as string,
slug: slug as string,
description: description as Document,
date: date as ContentfulDate,
affiche,
lieu: lieu as string | undefined,
lieuUrl: lieuUrl as string | undefined,
position
})
export const sortByDate = (evenements: Entry<ContentFulEvenement>[]): Entry<ContentFulEvenement>[] => {
@ -33,10 +52,28 @@ export const sortByDate = (evenements: Entry<ContentFulEvenement>[]): Entry<Cont
});
};
type ContentfulDate = `${number}-${number}-${number}T${number}:${number}:${number}Z`
interface Évènement {
nom: string, slug: string, description: Document, date: ContentfulDate, affiche: unknown, lieu?: string | undefined, lieuUrl?: string | undefined, position: unknown
}
function estÀVenir(évènement: Évènement): boolean {
const dateEvenement = new Date(évènement.date as string);
dateEvenement.setHours(0,0,0,0);
const aujourdhui = new Date();
aujourdhui.setHours(0, 0, 0, 0);
return (dateEvenement.getTime() - aujourdhui.getTime() >= 0);
}
export const fetchEvenements = async () => {
const entries = await contentfulClient.getEntries<ContentFulEvenement>({
content_type: "evenement",
});
return sortByDate(entries.items).map(evenementFromContentfull)
return sortByDate(entries.items)
.map(evenementFromContentfull)
.filter(estÀVenir)
}