-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
104 lines (96 loc) · 2.73 KB
/
main.tsx
File metadata and controls
104 lines (96 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/** @jsxImportSource solid-js */
// deno-lint-ignore-file no-import-prefix
import { render } from "@solidjs/web";
import type {
Post,
User,
} from "https://esm.sh/*@untypeable/jsonplaceholder@1.0.2";
import {
createMemo,
createSignal,
For,
isPending,
Loading,
Match,
onCleanup,
Switch,
} from "solid-js";
const urlBase = "https://jsonplaceholder.typicode.com";
async function fetchUsers() {
const ctrl = new AbortController();
onCleanup(() => ctrl.abort());
const response = await fetch(`${urlBase}/users`, { signal: ctrl.signal });
return await response.json() as User[];
}
async function fetchPosts(userId: number) {
const ctrl = new AbortController();
onCleanup(() => ctrl.abort());
const response = await fetch(
`${urlBase}/posts?userId=${userId}`,
{ signal: ctrl.signal },
);
return await response.json() as Post[];
}
async function fetchReadmeHTML() {
const ctrl = new AbortController();
onCleanup(() => ctrl.abort());
const [{ marked }, readmeMarkdown] = await Promise.all([
import("https://esm.sh/*marked@17.0.0"),
fetch("./README.md", { signal: ctrl.signal }).then((res) => res.text()),
]);
return marked(readmeMarkdown);
}
function App() {
const [selectedUserId, setSelectedUserId] = createSignal<number>();
const users = createMemo(() => fetchUsers());
const posts = createMemo(() => {
const userId = selectedUserId();
return userId !== undefined ? fetchPosts(userId) : [];
});
const readmeHTML = createMemo(() => fetchReadmeHTML());
return (
<>
<section innerHTML={readmeHTML()}></section>
<Loading fallback={<p>Loading Users...</p>}>
<label>
Select User:
<select
onChange={function handleChange(event) {
setSelectedUserId(+event.currentTarget.value);
}}
>
<option hidden selected></option>
<For each={users()}>
{(user) => (
<option value={user().id}>
@{user().username}: {user().name}
</option>
)}
</For>
</select>
</label>
</Loading>
<Switch>
<Match when={selectedUserId() !== undefined}>
<Loading fallback={<p>Loading Posts...</p>}>
<ul>
<For each={posts()}>
{(post) => <li>{post().title}</li>}
</For>
</ul>
</Loading>
</Match>
<Match when={!isPending(users)}>
<p>Select User to view posts</p>
</Match>
</Switch>
<p>
Data Source:
<a href="https://jsonplaceholder.typicode.com/" target="_blank">
JSONPlaceholder
</a>
</p>
</>
);
}
render(() => <App />, document.body);