Skip to content

Commit

Permalink
feat: add catch cases to loading paper promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Slartibartfass2 committed Jan 19, 2025
1 parent ee7b404 commit c52959f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/composites/PaperBookmarkButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Usage:
{:then}
<ToggleableInput isEditable={areDetailsInEditMode} {value} />
{:catch error}
<span class="pt-2 text-error">Coudn't load {key}: {error}</span>
<span class="pt-2 text-error">Coudn't load {key}{error ? `: ${error}` : ""}</span>
{/await}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Usage:
value={paper.abstrakt}
/>
{:catch error}
<span class="text-error">Coudn't load abstract: {error}</span>
<span class="text-error">Coudn't load abstract{error ? `: ${error}` : ""}</span>
{/await}
</section>
</PaperCardContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
</script>

<DecisionButton class="bg-accept-green hover:bg-accept-green-hover" onClick={acceptPaper}>
<DecisionButton
class="bg-accept-green hover:bg-accept-green-hover"
onClick={acceptPaper}
{loadingPaperId}
>
{#snippet buttonContent()}
<p>Accept</p>
<p class="text-accept-green-shortcut">Ctrl+A</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@
type Props = WithElementRef<TooltipTriggerProps> & {
buttonContent: Snippet;
tooltipContent: Snippet;
onClick: () => void;
loadingPaperId: Promise<number>;
onClick: (paperId: number) => void;
};
const {
buttonContent,
tooltipContent,
loadingPaperId,
onClick,
class: className,
...restProps
}: Props = $props();
let paperId = $state<number | undefined>(undefined);
loadingPaperId.then((id) => (paperId = id)).catch(() => (paperId = undefined));
function onButtonClick() {
if (paperId) {
onClick(paperId);
} else {
console.error("Paper ID is not set");
}
}
</script>

<!-- max width is fixed, see PaperView component for reason -->
Expand All @@ -29,7 +42,11 @@ Rather use `AcceptButton` or `DeclineButton` or `MaybeButton` instead of this co
Usage:
```svelte
<DecisionButton class="bg-decline-red" onClick={() => console.log("clicked button")}>
<DecisionButton
class="bg-decline-red"
onClick={(paperId) => console.log("clicked button")}
{loadingPaperId}
>
{#snippet buttonContent()}
<p>This is a button</p>
{/snippet}
Expand All @@ -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"
></Tooltip>
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
</script>

<DecisionButton class="bg-decline-red hover:bg-decline-red-hover" onClick={declinePaper}>
<DecisionButton
class="bg-decline-red hover:bg-decline-red-hover"
onClick={declinePaper}
{loadingPaperId}
>
{#snippet buttonContent()}
<p>Decline</p>
<p class="text-decline-red-shortcut">Ctrl+D</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
</script>

<DecisionButton class="bg-maybe-yellow hover:bg-maybe-yellow-hover" onClick={markPaperAsUndecided}>
<DecisionButton
class="bg-maybe-yellow hover:bg-maybe-yellow-hover"
onClick={markPaperAsUndecided}
{loadingPaperId}
>
{#snippet buttonContent()}
<p>Maybe</p>
<p class="text-maybe-yellow-shortcut">Ctrl+S</p>
Expand Down
4 changes: 2 additions & 2 deletions src/routes/paper/[paperId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

<svelte:head>
{#await loadingPaper}
<title>Loading Paper...</title>
<title>Loading paper...</title>
{:then paper}
<title>{paper.title}</title>
{:catch}
<title>Error</title>
<title>Failed loading paper</title>
{/await}
</svelte:head>
<PaperView
Expand Down
3 changes: 2 additions & 1 deletion src/routes/project/[projectId]/paper/[paperId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

<svelte:head>
{#await Promise.all([loadingProject, loadingPaper])}
<title>Loading Paper and Project...</title>
<title>Loading paper and project...</title>
{:then [project, paper]}
<title>{paper.title} | {project.name}</title>
{:catch}
<title>Failed loading data</title>
{/await}
</svelte:head>

<PaperView
{user}
{loadingPaper}
Expand Down

0 comments on commit c52959f

Please sign in to comment.