From c52959fe7db950842bee2445734425952b77e538 Mon Sep 17 00:00:00 2001 From: Felix Schlegel Date: Sun, 19 Jan 2025 18:28:19 +0100 Subject: [PATCH] feat: add catch cases to loading paper promises --- .../composites/PaperBookmarkButton.svelte | 2 +- .../paper-view/PaperDetail.svelte | 2 +- .../paper-view/cards/PaperDetailsCard.svelte | 2 +- .../decision-buttons/AcceptButton.svelte | 10 ++++---- .../decision-buttons/DecisionButton.svelte | 23 ++++++++++++++++--- .../decision-buttons/DeclineButton.svelte | 10 ++++---- .../decision-buttons/MaybeButton.svelte | 10 ++++---- src/routes/paper/[paperId]/+page.svelte | 4 ++-- .../[projectId]/paper/[paperId]/+page.svelte | 3 ++- 9 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/lib/components/composites/PaperBookmarkButton.svelte b/src/lib/components/composites/PaperBookmarkButton.svelte index 70c5a631..7d700ebf 100644 --- a/src/lib/components/composites/PaperBookmarkButton.svelte +++ b/src/lib/components/composites/PaperBookmarkButton.svelte @@ -17,7 +17,7 @@ let isHovered = $state(false); const tooltipText = $derived(isBookmarked ? "Remove from Reading List" : "Add to Reading List"); let paperId: number | undefined = $state(undefined); - loadingPaperId.then((id) => (paperId = id)); + loadingPaperId.then((id) => (paperId = id)).catch(() => (paperId = undefined)); const onMouseEnter = () => (isHovered = true); const onMouseLeave = () => (isHovered = false); diff --git a/src/lib/components/composites/paper-components/paper-view/PaperDetail.svelte b/src/lib/components/composites/paper-components/paper-view/PaperDetail.svelte index 05ad013f..49033f8a 100644 --- a/src/lib/components/composites/paper-components/paper-view/PaperDetail.svelte +++ b/src/lib/components/composites/paper-components/paper-view/PaperDetail.svelte @@ -35,6 +35,6 @@ Usage: {:then} {:catch error} - Coudn't load {key}: {error} + Coudn't load {key}{error ? `: ${error}` : ""} {/await} diff --git a/src/lib/components/composites/paper-components/paper-view/cards/PaperDetailsCard.svelte b/src/lib/components/composites/paper-components/paper-view/cards/PaperDetailsCard.svelte index a2f8858a..6714a45f 100644 --- a/src/lib/components/composites/paper-components/paper-view/cards/PaperDetailsCard.svelte +++ b/src/lib/components/composites/paper-components/paper-view/cards/PaperDetailsCard.svelte @@ -153,7 +153,7 @@ Usage: value={paper.abstrakt} /> {:catch error} - Coudn't load abstract: {error} + Coudn't load abstract{error ? `: ${error}` : ""} {/await} diff --git a/src/lib/components/composites/paper-components/paper-view/decision-buttons/AcceptButton.svelte b/src/lib/components/composites/paper-components/paper-view/decision-buttons/AcceptButton.svelte index 97fde4de..61ff237b 100644 --- a/src/lib/components/composites/paper-components/paper-view/decision-buttons/AcceptButton.svelte +++ b/src/lib/components/composites/paper-components/paper-view/decision-buttons/AcceptButton.svelte @@ -6,15 +6,17 @@ } const { loadingPaperId }: Props = $props(); - let paperId: number | undefined = $state(undefined); - loadingPaperId.then((id) => (paperId = id)); - function acceptPaper() { + function acceptPaper(paperId: number) { console.log(`Accepted paper with id ${paperId}`); } - + {#snippet buttonContent()}

Accept

Ctrl+A

diff --git a/src/lib/components/composites/paper-components/paper-view/decision-buttons/DecisionButton.svelte b/src/lib/components/composites/paper-components/paper-view/decision-buttons/DecisionButton.svelte index 634c284a..8cf51460 100644 --- a/src/lib/components/composites/paper-components/paper-view/decision-buttons/DecisionButton.svelte +++ b/src/lib/components/composites/paper-components/paper-view/decision-buttons/DecisionButton.svelte @@ -8,16 +8,29 @@ type Props = WithElementRef & { buttonContent: Snippet; tooltipContent: Snippet; - onClick: () => void; + loadingPaperId: Promise; + onClick: (paperId: number) => void; }; const { buttonContent, tooltipContent, + loadingPaperId, onClick, class: className, ...restProps }: Props = $props(); + + let paperId = $state(undefined); + loadingPaperId.then((id) => (paperId = id)).catch(() => (paperId = undefined)); + + function onButtonClick() { + if (paperId) { + onClick(paperId); + } else { + console.error("Paper ID is not set"); + } + } @@ -29,7 +42,11 @@ Rather use `AcceptButton` or `DeclineButton` or `MaybeButton` instead of this co Usage: ```svelte - console.log("clicked button")}> + console.log("clicked button")} + {loadingPaperId} + > {#snippet buttonContent()}

This is a button

{/snippet} @@ -43,7 +60,7 @@ Usage: class={cn("text-primary max-w-[20rem] shadow-lg flex-grow-1000", className)} trigger={buttonContent} content={tooltipContent} - onclick={onClick} + onclick={onButtonClick} {...restProps} data-testid="decision-button" > diff --git a/src/lib/components/composites/paper-components/paper-view/decision-buttons/DeclineButton.svelte b/src/lib/components/composites/paper-components/paper-view/decision-buttons/DeclineButton.svelte index 7c259e51..11b3be15 100644 --- a/src/lib/components/composites/paper-components/paper-view/decision-buttons/DeclineButton.svelte +++ b/src/lib/components/composites/paper-components/paper-view/decision-buttons/DeclineButton.svelte @@ -6,15 +6,17 @@ } const { loadingPaperId }: Props = $props(); - let paperId: number | undefined = $state(undefined); - loadingPaperId.then((id) => (paperId = id)); - function declinePaper() { + function declinePaper(paperId: number) { console.log(`Declined paper with id ${paperId}`); } - + {#snippet buttonContent()}

Decline

Ctrl+D

diff --git a/src/lib/components/composites/paper-components/paper-view/decision-buttons/MaybeButton.svelte b/src/lib/components/composites/paper-components/paper-view/decision-buttons/MaybeButton.svelte index a3e58c15..b3cb3df1 100644 --- a/src/lib/components/composites/paper-components/paper-view/decision-buttons/MaybeButton.svelte +++ b/src/lib/components/composites/paper-components/paper-view/decision-buttons/MaybeButton.svelte @@ -6,15 +6,17 @@ } const { loadingPaperId }: Props = $props(); - let paperId: number | undefined = $state(undefined); - loadingPaperId.then((id) => (paperId = id)); - function markPaperAsUndecided() { + function markPaperAsUndecided(paperId: number) { console.log(`Undecided paper with id ${paperId}`); } - + {#snippet buttonContent()}

Maybe

Ctrl+S

diff --git a/src/routes/paper/[paperId]/+page.svelte b/src/routes/paper/[paperId]/+page.svelte index d836a67a..892804cb 100644 --- a/src/routes/paper/[paperId]/+page.svelte +++ b/src/routes/paper/[paperId]/+page.svelte @@ -7,11 +7,11 @@ {#await loadingPaper} - Loading Paper... + Loading paper... {:then paper} {paper.title} {:catch} - Error + Failed loading paper {/await} {#await Promise.all([loadingProject, loadingPaper])} - Loading Paper and Project... + Loading paper and project... {:then [project, paper]} {paper.title} | {project.name} {:catch} Failed loading data {/await} +