From 505511fd638141b6d8cca15ef56257720ab0c684 Mon Sep 17 00:00:00 2001 From: Hampus Hallkvist Date: Sun, 10 Mar 2024 15:55:39 +0100 Subject: [PATCH] fix: Query optimizations and suspense --- src/app/_components/FairDates.tsx | 26 +++++++++++ src/app/_components/Recruitment.tsx | 24 ++++++++++ src/app/page.tsx | 46 ++++--------------- .../student/recruitment/RecruitmentList.tsx | 4 +- src/app/team/page.tsx | 4 +- .../shared/hooks/api/useExhibitors.tsx | 1 + .../shared/hooks/api/useOrganization.tsx | 11 +++-- .../shared/hooks/api/useRecruitment.tsx | 15 +++--- 8 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 src/app/_components/FairDates.tsx create mode 100644 src/app/_components/Recruitment.tsx diff --git a/src/app/_components/FairDates.tsx b/src/app/_components/FairDates.tsx new file mode 100644 index 0000000..6e8c781 --- /dev/null +++ b/src/app/_components/FairDates.tsx @@ -0,0 +1,26 @@ +import { fetchDates } from "@/components/shared/hooks/api/useDates" +import { DateTime } from "luxon" + +export async function FairDates() { + const dates = await fetchDates() + + return ( +
+

Fair:

+
+ {[ + // Pick first and last day (ASSUMPTION: days are sorted and continuous) + dates.fair.days[0], + dates.fair.days[dates.fair.days.length - 1] + ].map((date, index, list) => ( +

+ {DateTime.fromISO(date).toFormat( + // Only add month and year to last + `d${index == list.length - 1 ? " MMM" : "-"}${DateTime.fromISO(date).year !== DateTime.now().year ? " YYYY" : ""}` + )} +

+ ))} +
+
+ ) +} diff --git a/src/app/_components/Recruitment.tsx b/src/app/_components/Recruitment.tsx new file mode 100644 index 0000000..281545d --- /dev/null +++ b/src/app/_components/Recruitment.tsx @@ -0,0 +1,24 @@ +import { fetchRecruitment } from "@/components/shared/hooks/api/useRecruitment" +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" +import { UserRoundIcon } from "lucide-react" +import Link from "next/link" + +export async function RecruitmentBanner() { + const recruitment = await fetchRecruitment({ + cache: "no-cache" + }) + + if (recruitment == null) return null + + return ( + + + + Recruitment open! + + Apply to become a part of Armada 2024 + + + + ) +} diff --git a/src/app/page.tsx b/src/app/page.tsx index 4f2046f..9307765 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,17 +1,13 @@ +import { FairDates } from "@/app/_components/FairDates" +import { RecruitmentBanner } from "@/app/_components/Recruitment" import { NavigationMenu } from "@/components/shared/NavigationMenu" import { Page } from "@/components/shared/Page" -import { fetchDates } from "@/components/shared/hooks/api/useDates" -import { fetchRecruitment } from "@/components/shared/hooks/api/useRecruitment" -import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Button } from "@/components/ui/button" -import { ArrowRightIcon, UserRoundIcon } from "lucide-react" -import { DateTime } from "luxon" +import { ArrowRightIcon } from "lucide-react" import Link from "next/link" +import { Suspense } from "react" export default async function HomePage() { - const recruitment = await fetchRecruitment() - const dates = await fetchDates() - return ( <>
- {recruitment != null && ( - - - - Recruitment open! - - Apply to become a part of Armada 2024 - - - - )} + + +
@@ -61,23 +49,9 @@ export default async function HomePage() {
-
-

Fair:

-
- {[ - // Pick first and last day (ASSUMPTION: days are sorted and continuous) - dates.fair.days[0], - dates.fair.days[dates.fair.days.length - 1] - ].map((date, index, list) => ( -

- {DateTime.fromISO(date).toFormat( - // Only add month and year to last - `d${index == list.length - 1 ? " MMM" : "-"}${DateTime.fromISO(date).year !== DateTime.now().year ? " YYYY" : ""}` - )} -

- ))} -
-
+ + + diff --git a/src/app/student/recruitment/RecruitmentList.tsx b/src/app/student/recruitment/RecruitmentList.tsx index e0a15c8..919418e 100644 --- a/src/app/student/recruitment/RecruitmentList.tsx +++ b/src/app/student/recruitment/RecruitmentList.tsx @@ -13,7 +13,9 @@ import { env } from "@/env" import { DateTime } from "luxon" export function RecruitmentList() { - const { data, isLoading } = useRecruitment() + const { data, isLoading } = useRecruitment({ + cache: "no-cache" + }) if (isLoading) return

Loading...

if (data == null) { diff --git a/src/app/team/page.tsx b/src/app/team/page.tsx index 07edab0..20e5310 100644 --- a/src/app/team/page.tsx +++ b/src/app/team/page.tsx @@ -12,7 +12,9 @@ export const metadata: Metadata = { } export default async function TeamPage() { - const organization = await fetchOrganization() + const organization = await fetchOrganization({ + cache: "no-cache" + }) return ( diff --git a/src/components/shared/hooks/api/useExhibitors.tsx b/src/components/shared/hooks/api/useExhibitors.tsx index 1664347..ed8e8a1 100644 --- a/src/components/shared/hooks/api/useExhibitors.tsx +++ b/src/components/shared/hooks/api/useExhibitors.tsx @@ -58,6 +58,7 @@ export async function fetchExhibitors(options?: { year?: number }) { `${env.NEXT_PUBLIC_API_URL}/api/exhibitors?year=${options?.year ?? defaultYear}`, { next: { + revalidate: 3600 * 24, // 24 cache hours tags: [ "exhibitors", options?.year?.toString() ?? defaultYear.toString() diff --git a/src/components/shared/hooks/api/useOrganization.tsx b/src/components/shared/hooks/api/useOrganization.tsx index b60c5dc..396d79f 100644 --- a/src/components/shared/hooks/api/useOrganization.tsx +++ b/src/components/shared/hooks/api/useOrganization.tsx @@ -16,15 +16,18 @@ export interface Person { role: string } -export async function fetchOrganization() { - const res = await fetch(`${env.NEXT_PUBLIC_API_URL}/api/organization/v2`, {}) +export async function fetchOrganization(options?: RequestInit) { + const res = await fetch( + `${env.NEXT_PUBLIC_API_URL}/api/organization/v2`, + options ?? {} + ) const result = await res.json() return result as Organization[] } -export function useOrganization() { +export function useOrganization(options?: RequestInit) { return useQuery({ queryKey: ["recruitment"], - queryFn: fetchOrganization + queryFn: () => fetchOrganization(options) }) } diff --git a/src/components/shared/hooks/api/useRecruitment.tsx b/src/components/shared/hooks/api/useRecruitment.tsx index 05186bb..00b2d48 100644 --- a/src/components/shared/hooks/api/useRecruitment.tsx +++ b/src/components/shared/hooks/api/useRecruitment.tsx @@ -15,19 +15,22 @@ interface RecruitmentGroup { description: string } -export async function fetchRecruitment() { - const res = await fetch(`${env.NEXT_PUBLIC_API_URL}/api/recruitment`, { - cache: "no-cache" - }) +export async function fetchRecruitment(options?: RequestInit) { + const res = await fetch( + `${env.NEXT_PUBLIC_API_URL}/api/recruitment`, + options ?? { + cache: "no-cache" + } + ) const result = await res.json() if (result == null || !Array.isArray(result) || result.length <= 0) return null return result[0] as Recruitment } -export function useRecruitment() { +export function useRecruitment(options?: RequestInit) { return useQuery({ queryKey: ["recruitment"], - queryFn: fetchRecruitment + queryFn: () => fetchRecruitment(options) }) }