diff --git a/hooks/useArtistsGuests.ts b/hooks/useArtistsGuests.ts index f084906..affa3e2 100644 --- a/hooks/useArtistsGuests.ts +++ b/hooks/useArtistsGuests.ts @@ -5,16 +5,21 @@ import { } from "../lib/contentful/pages/artists"; import { AllArtistEntry } from "../types/shared"; +const fetcher = async (url) => { + const response = await fetch(url); + if (!response.ok) { + throw new Error("An error occurred while fetching the data."); + } + return await response.json(); +}; + export default function useArtistsGuests(fallbackData: AllArtistEntry[]) { const { data, setSize, error, isValidating, isLoading } = useSWRInfinite( - (pageIndex) => [pageIndex * ARTISTS_GUESTS_PAGE_SIZE], - async (skip) => { - const r = await fetch( - `/api/artists?limit=${ARTISTS_GUESTS_PAGE_SIZE}&skip=${skip}&role=false` - ); - - return await r.json(); - }, + (pageIndex) => + `/api/artists?limit=${ARTISTS_GUESTS_PAGE_SIZE}&skip=${ + pageIndex * ARTISTS_GUESTS_PAGE_SIZE + }&role=false`, + fetcher, { fallbackData: [fallbackData], revalidateFirstPage: false, diff --git a/hooks/useNewsArticles.ts b/hooks/useNewsArticles.ts index 7367db5..83dc0f5 100644 --- a/hooks/useNewsArticles.ts +++ b/hooks/useNewsArticles.ts @@ -6,7 +6,7 @@ import { } from "../lib/contentful/pages/news"; export default function useNewsArticles(fallbackData: ArticleInterface[]) { - const { data, setSize } = useSWRInfinite( + const { data, setSize, isValidating } = useSWRInfinite( (pageIndex) => [pageIndex * NEWS_ARTICLES_PAGE_SIZE], async (skip) => getNewsPageArticles(false, NEWS_ARTICLES_PAGE_SIZE, skip[0]), @@ -30,5 +30,6 @@ export default function useNewsArticles(fallbackData: ArticleInterface[]) { articles, loadMore, isReachingEnd, + isValidating, }; } diff --git a/hooks/useRadioShows.ts b/hooks/useRadioShows.ts index ae4abfa..49488dd 100644 --- a/hooks/useRadioShows.ts +++ b/hooks/useRadioShows.ts @@ -2,21 +2,24 @@ import useSWRInfinite from "swr/infinite"; import { PastShowSchema } from "../types/shared"; import { RADIO_SHOWS_PAGE_SIZE } from "../lib/contentful/pages/radio"; +const fetcher = async (url) => { + const response = await fetch(url); + if (!response.ok) { + throw new Error("An error occurred while fetching the data."); + } + return await response.json(); +}; + export default function useRadioShows( fallbackData: PastShowSchema[], filter: string[] ) { const { data, setSize, error, isValidating, isLoading } = useSWRInfinite( - (pageIndex) => [pageIndex * RADIO_SHOWS_PAGE_SIZE, filter], - async (skip) => { - const r = await fetch( - `/api/shows?take=${RADIO_SHOWS_PAGE_SIZE}&skip=${skip}&filter=${encodeURIComponent( - filter.join(",") - )}` - ); - - return await r.json(); - }, + (pageIndex) => + `/api/shows?take=${RADIO_SHOWS_PAGE_SIZE}&skip=${ + pageIndex * RADIO_SHOWS_PAGE_SIZE + }&filter=${encodeURIComponent(filter.join(","))}`, + fetcher, { fallbackData: filter.length == 0 ? [fallbackData] : [], revalidateFirstPage: false, diff --git a/lib/contentful/pages/artists.ts b/lib/contentful/pages/artists.ts index b10337f..1510625 100644 --- a/lib/contentful/pages/artists.ts +++ b/lib/contentful/pages/artists.ts @@ -8,16 +8,13 @@ import { import { extractCollection, extractCollectionItem, sort } from "../../../util"; import { AllArtistFragment } from "../fragments"; -export const ARTISTS_GUESTS_PAGE_SIZE = 500; +export const ARTISTS_GUESTS_PAGE_SIZE = 250; export async function getArtistsPage( role: boolean, limit: number, skip: number ) { - console.log("role", role); - console.log("limit", limit); - console.log("skip", skip); const ArtistsPageQuery = /* GraphQL */ ` query ArtistsPageQuery($limit: Int, $skip: Int, $role: Boolean) { artistCollection( diff --git a/views/news/allArticles.tsx b/views/news/allArticles.tsx index 1ba9f13..6674e61 100644 --- a/views/news/allArticles.tsx +++ b/views/news/allArticles.tsx @@ -3,13 +3,15 @@ import useNewsArticles from "../../hooks/useNewsArticles"; import { ArticleInterface } from "../../types/shared"; import Image from "next/image"; import { useState, useEffect } from "react"; +import LoadMore from "../../components/loadMore"; export default function AllArticles({ articles: fallbackData, }: { articles: ArticleInterface[]; }) { - const { articles, loadMore, isReachingEnd } = useNewsArticles(fallbackData); + const { articles, loadMore, isReachingEnd, isValidating } = + useNewsArticles(fallbackData); return (
@@ -26,21 +28,13 @@ export default function AllArticles({
diff --git a/views/radio/allShows.tsx b/views/radio/allShows.tsx index a8040fc..0d165f0 100644 --- a/views/radio/allShows.tsx +++ b/views/radio/allShows.tsx @@ -48,7 +48,7 @@ export default function AllShows({ ))} - {!isReachingEnd && ( + {!isReachingEnd && !isLoading && (