Skip to content

Commit 32b7203

Browse files
committed
expand async content to handle loadingStore
1 parent d810e04 commit 32b7203

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

frontend/src/ts/components/common/AsyncContent.tsx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,47 @@ import { ErrorBoundary, JSXElement, Resource, Show, Suspense } from "solid-js";
22
import { createErrorMessage } from "../../utils/misc";
33
import * as Notifications from "../../elements/notifications";
44
import { Conditional } from "./Conditional";
5+
import { LoadingStore } from "../../signals/util/loadingStore";
56

67
export default function AsyncContent<T>(
78
props: {
8-
resource: Resource<T | undefined>;
99
errorMessage?: string;
1010
} & (
1111
| {
12-
alwaysShowContent?: never;
13-
children: (data: T) => JSXElement;
12+
resource: Resource<T | undefined>;
13+
loadingStore?: never;
1414
}
1515
| {
16-
alwaysShowContent: true;
17-
showLoader?: true;
18-
children: (data: T | undefined) => JSXElement;
16+
loadingStore: LoadingStore<T | undefined>;
17+
resource?: never;
1918
}
20-
),
19+
) &
20+
(
21+
| {
22+
alwaysShowContent?: never;
23+
children: (data: T) => JSXElement;
24+
}
25+
| {
26+
alwaysShowContent: true;
27+
showLoader?: true;
28+
children: (data: T | undefined) => JSXElement;
29+
}
30+
),
2131
): JSXElement {
32+
const source =
33+
props.resource !== undefined
34+
? {
35+
value: () => props.resource(),
36+
loading: () => props.resource.loading,
37+
}
38+
: {
39+
value: () => props.loadingStore.store,
40+
loading: () => props.loadingStore.state().loading,
41+
};
42+
2243
const value = () => {
2344
try {
24-
return props.resource();
45+
return source.value();
2546
} catch (err) {
2647
const message = createErrorMessage(
2748
err,
@@ -47,7 +68,7 @@ export default function AsyncContent<T>(
4768
};
4869
return (
4970
<>
50-
<Show when={p.showLoader && props.resource.loading}>
71+
<Show when={p.showLoader && source.loading()}>
5172
<div class="preloader">
5273
<i class="fas fa-fw fa-spin fa-circle-notch"></i>
5374
</div>
@@ -68,9 +89,9 @@ export default function AsyncContent<T>(
6889
}
6990
>
7091
<Show
71-
when={props.resource() !== null && props.resource() !== undefined}
92+
when={source.value() !== null && source.value() !== undefined}
7293
>
73-
{props.children(props.resource() as T)}
94+
{props.children(source.value() as T)}
7495
</Show>
7596
</Suspense>
7697
</ErrorBoundary>

frontend/src/ts/components/pages/AboutPage.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,20 @@ export function AboutPage(): JSXElement {
4545
<Button onClick={() => connections.reset()} text="reset" />
4646
</Show>
4747

48-
<Show when={connections.state().loading}>... is loading</Show>
49-
<Show when={connections.state().error !== undefined}>
50-
oh oh, {connections.state().error}
51-
</Show>
52-
<Show when={connections.state().refreshing}>refreshing...</Show>
53-
<Show when={connections.state().ready || connections.state().refreshing}>
54-
<For each={connections.store}>
55-
{(connection) => (
56-
<p>
57-
{connection.initiatorName} to {connection.receiverName}
58-
</p>
59-
)}
60-
</For>
61-
</Show>
62-
48+
<AsyncContent
49+
loadingStore={connections}
50+
errorMessage="Failed to load connections"
51+
>
52+
{(data) => (
53+
<For each={data}>
54+
{(con) => (
55+
<p>
56+
{con.initiatorName} to {con.receiverName}
57+
</p>
58+
)}
59+
</For>
60+
)}
61+
</AsyncContent>
6362
<div class="created">
6463
Created with love by Miodec.
6564
<br />

0 commit comments

Comments
 (0)