Skip to content

Commit

Permalink
Add function to determine current school year and update queries for …
Browse files Browse the repository at this point in the history
…events
  • Loading branch information
ferdinandsalis committed Jan 23, 2025
1 parent 81d573e commit 6ba5e82
Show file tree
Hide file tree
Showing 13 changed files with 5,109 additions and 4,384 deletions.
10 changes: 10 additions & 0 deletions app/routes/_index/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export const homeQuery = defineQuery(`{
"caption": image->caption,
"attribution": image->attribution
},
"closestEvent": *[_type == "event" && start.date >= now()] | order(start.date asc)[0] {
_id,
_type,
title,
location,
description,
start,
end,
type
},
"testimonials": *[_type == "testimonial"] | order(_createdAt desc) {
_id,
_type,
Expand Down
32 changes: 9 additions & 23 deletions app/routes/_index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,22 @@ import { type HomeQueryResult } from '#app/sanity/types.ts'
import { cn } from '#app/utils/misc.js'
import { pillars } from '../ueber-uns+/philosophie+/_layout.tsx'
import { homeQuery } from './query.ts'
import { EventSchema } from '#app/sanity/schema/event.tsx'

export async function loader() {
const queryResult = await loadQuery<HomeQueryResult>(homeQuery)
const upcomingEvents = eventsData
.filter(date => new Date(date.startDate).getTime() > new Date().getTime())
.sort(
(a, b) =>
new Date(a.startDate).getTime() - new Date(b.startDate).getTime(),
) // Sort to ensure the first element is the earliest upcoming date

const latestEvent =
upcomingEvents.length > 0
? {
...upcomingEvents[0],
startDate: new Date(upcomingEvents[0].startDate),
key: `${slug(upcomingEvents[0].title)}_${upcomingEvents[0].startDate}`,
}
: null
console.log(queryResult)

return {
query: homeQuery,
latestEvent,
data: queryResult.data,
}
}

export default function Home() {
const loaderData = useLoaderData<typeof loader>()
const [latestPost, ...restPosts] = loaderData.data.posts
const latestDate = loaderData.latestEvent
const closestEvent = EventSchema.parse(loaderData.data.closestEvent)
const testimonials = loaderData.data.testimonials
const hero = loaderData.data.hero

Expand Down Expand Up @@ -96,7 +82,7 @@ export default function Home() {
{hero?.caption && <p className="relative">{hero.caption}</p>}
</div>

{latestDate && (
{closestEvent && (
<article className="col-start-1 row-start-2 flex items-center gap-2 bg-muted/60 p-3 px-4 shadow-inner sm:rounded-b-md sm:px-8 md:px-12">
<div className="flex flex-col gap-2 md:flex-row md:items-center md:gap-4">
<div className="flex items-center gap-2">
Expand All @@ -108,16 +94,16 @@ export default function Home() {
/>
</h1>
<Link
key={latestDate.key}
to={`/aktuelles#${latestDate.key}`}
key={closestEvent._id}
to={`/aktuelles#${closestEvent._id}`}
className="font-condensed font-bold"
>
<span className="">{latestDate.title}</span> am{' '}
<span className="">{closestEvent.title}</span> am{' '}
<time
dateTime={latestDate.startDate.toISOString()}
dateTime={closestEvent.start.date.toISOString()}
className=""
>
{latestDate.startDate.toLocaleDateString('de-AT', {
{closestEvent.start.date.toLocaleDateString('de-AT', {
day: 'numeric',
month: 'long',
})}
Expand Down
65 changes: 42 additions & 23 deletions app/routes/aktuelles/query.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
import { defineQuery } from 'groq'
import { z } from 'zod'
import { type Event } from '#app/sanity/schema/event.tsx'
import { PhotoSchema } from '#app/sanity/schema/year.ts'
import { PersonSchema } from '../ueber-uns+/_index.query.ts'

type Post = {
title: string
cover: any
slug: { current: string }
previewText: string
publishedAt: string
}

export const YearSchema = z.object({
letter: z.string(),
startedAt: z.coerce.date(),
graduatedAt: z.coerce.date().nullable(),
mentor: PersonSchema.pick({ givenNames: true, familyName: true }).extend({
name: z.string(),
slug: z.object({ current: z.string() }),
}),
photos: z.array(PhotoSchema).nullable(),
plan: z.string().nullable(),
})

export type Year = z.infer<typeof YearSchema>

export const aktuellesQuery = defineQuery(`{
"posts": *[_type == "post"] | order(publishedAt desc) {
"posts": *[_type == "post"] | order(publishedAt desc)[0...3] {
_id,
_type,
title,
Expand All @@ -35,6 +14,23 @@ export const aktuellesQuery = defineQuery(`{
slug,
publishedAt
},
// sort by start date from and till a date; exclude holidays
"events": *[_type == "event" && start.date >= $fromDate && start.date <= $toDate && type != "holiday"] | order(start.date asc) {
_id,
_type,
title,
location,
description,
start,
end,
type,
attachments {
_type,
asset->{
url
}
}
},
// filter years by those with empty graduatedAt
"years": *[_type == "year" && !defined(graduatedAt)] | order(startedAt desc) {
_id,
Expand All @@ -53,7 +49,30 @@ export const aktuellesQuery = defineQuery(`{
}
}`)

type Post = {
title: string
cover: any
slug: { current: string }
previewText: string
publishedAt: string
}

export const YearSchema = z.object({
letter: z.string(),
startedAt: z.coerce.date(),
graduatedAt: z.coerce.date().nullable(),
mentor: PersonSchema.pick({ givenNames: true, familyName: true }).extend({
name: z.string(),
slug: z.object({ current: z.string() }),
}),
photos: z.array(PhotoSchema).nullable(),
plan: z.string().nullable(),
})

export type Year = z.infer<typeof YearSchema>

export type QueryResult = {
posts: Post[]
years: Year[]
events: Event[]
}
Loading

0 comments on commit 6ba5e82

Please sign in to comment.