From 9fc2adfed9ac926ac5552e7b7060c9b223f3a3bf Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Sun, 24 Nov 2024 20:15:49 -0500 Subject: [PATCH 1/3] Use getApiResponse on infinite scrolls, too --- src/langs/json/en.json | 4 +- .../components/documents/ResultsList.svelte | 45 ++++++++++++------- .../stories/ResultsList.stories.svelte | 6 ++- src/test/handlers/documents.ts | 4 ++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/langs/json/en.json b/src/langs/json/en.json index 0ec1235fb..215355db4 100644 --- a/src/langs/json/en.json +++ b/src/langs/json/en.json @@ -203,7 +203,9 @@ "pageAbbrev": "p.", "pageCount": "{n, plural, one {# page} other {# pages}}", "select": "Select", - "noteCount": "{n, plural, one {# note} other {# notes}}" + "noteCount": "{n, plural, one {# note} other {# notes}}", + "more": "Load more", + "retry": "Please try again." }, "error": { "report": "Report a problem" diff --git a/src/lib/components/documents/ResultsList.svelte b/src/lib/components/documents/ResultsList.svelte index c850b756b..f83d15c86 100644 --- a/src/lib/components/documents/ResultsList.svelte +++ b/src/lib/components/documents/ResultsList.svelte @@ -1,11 +1,11 @@ @@ -50,6 +55,10 @@ {#if loading} Loading past runs… + {:else if error} + + {error} + {:else} {#each runs as run} @@ -62,10 +71,10 @@ { + on:next={() => { if (next) load(new URL(next)); }} - on:previous={(e) => { + on:previous={() => { if (previous) load(new URL(previous)); }} /> diff --git a/src/lib/components/addons/Scheduled.svelte b/src/lib/components/addons/Scheduled.svelte index 10428c2bd..7c8f813a4 100644 --- a/src/lib/components/addons/Scheduled.svelte +++ b/src/lib/components/addons/Scheduled.svelte @@ -2,7 +2,7 @@ import type { Maybe, Nullable, Page, Event } from "$lib/api/types"; import { _ } from "svelte-i18n"; - import { Clock16, Hourglass24 } from "svelte-octicons"; + import { Alert16, Clock16, Hourglass24 } from "svelte-octicons"; import ScheduledEvent from "./ScheduledEvent.svelte"; import SidebarGroup from "../sidebar/SidebarGroup.svelte"; @@ -10,31 +10,37 @@ import Paginator from "$lib/components/common/Paginator.svelte"; import Empty from "../common/Empty.svelte"; + import { getApiResponse } from "$lib/utils/api"; + export let events: Event[]; export let previous: Maybe> = undefined; export let next: Maybe> = undefined; export let loading = false; + let error: string = ""; + // load the next set of results async function load(url: URL) { loading = true; - // todo: better error handling - const res = await fetch(url, { credentials: "include" }).catch( + const resp = await fetch(url, { credentials: "include" }).catch( console.error, ); - if (!res) return console.error("API error"); - if (!res.ok) { - console.error(res.statusText); - loading = false; + + const { data: results, error: err } = + await getApiResponse>(resp); + + if (err) { + error = err.message; } - const results: Page = await res.json(); + if (results) { + events = results.results; + next = results.next; + previous = results.previous; + } - events = results.results; - next = results.next; - previous = results.previous; loading = false; } @@ -47,6 +53,10 @@ {#if loading} {$_("common.loading")} + {:else if error} + + {error} + {:else} {#each events as event} @@ -61,12 +71,12 @@ { + on:next={() => { if (next) { load(new URL(next)); } }} - on:previous={(e) => { + on:previous={() => { if (previous) { load(new URL(previous)); } From 31f55fb89529779dab074d5e4f3a0fa084021300 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Mon, 25 Nov 2024 10:03:55 -0500 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Allan Lasser --- src/lib/components/addons/History.svelte | 4 ++-- src/lib/components/addons/Scheduled.svelte | 4 ++-- src/lib/components/documents/ResultsList.svelte | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/components/addons/History.svelte b/src/lib/components/addons/History.svelte index 7aa5223a7..f9f7d492a 100644 --- a/src/lib/components/addons/History.svelte +++ b/src/lib/components/addons/History.svelte @@ -3,7 +3,7 @@ import type { Run } from "$lib/api/types"; import { _ } from "svelte-i18n"; - import { Alert16, History16, History24, Hourglass24 } from "svelte-octicons"; + import { Alert24, History16, History24, Hourglass24 } from "svelte-octicons"; import HistoryEvent from "./HistoryEvent.svelte"; import Paginator from "$lib/components/common/Paginator.svelte"; @@ -56,7 +56,7 @@ {#if loading} Loading past runs… {:else if error} - + {error} {:else} diff --git a/src/lib/components/addons/Scheduled.svelte b/src/lib/components/addons/Scheduled.svelte index 7c8f813a4..4f197a9a1 100644 --- a/src/lib/components/addons/Scheduled.svelte +++ b/src/lib/components/addons/Scheduled.svelte @@ -2,7 +2,7 @@ import type { Maybe, Nullable, Page, Event } from "$lib/api/types"; import { _ } from "svelte-i18n"; - import { Alert16, Clock16, Hourglass24 } from "svelte-octicons"; + import { Alert24, Clock16, Hourglass24 } from "svelte-octicons"; import ScheduledEvent from "./ScheduledEvent.svelte"; import SidebarGroup from "../sidebar/SidebarGroup.svelte"; @@ -54,7 +54,7 @@ {#if loading} {$_("common.loading")} {:else if error} - + {error} {:else} diff --git a/src/lib/components/documents/ResultsList.svelte b/src/lib/components/documents/ResultsList.svelte index f83d15c86..a0b58600a 100644 --- a/src/lib/components/documents/ResultsList.svelte +++ b/src/lib/components/documents/ResultsList.svelte @@ -192,6 +192,7 @@ } .error { + text-align: center; color: var(--error, red); }