-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
119 lines (111 loc) · 3.12 KB
/
main.tsx
File metadata and controls
119 lines (111 loc) · 3.12 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/** @jsxImportSource solid-js */
// deno-lint-ignore-file no-import-prefix
import type {
Post,
User,
} from "https://esm.sh/*@untypeable/jsonplaceholder@1.0.2";
import {
type Accessor,
createResource,
createSignal,
For,
Match,
onCleanup,
Switch,
} from "solid-js";
import { render } from "solid-js/web";
const urlBase = "https://jsonplaceholder.typicode.com";
function createUsersResource() {
return createResource(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[];
});
}
function createPostsResource(userId: Accessor<number | undefined>) {
return createResource(
userId,
async function fetchPosts(userId) {
const ctrl = new AbortController();
onCleanup(() => ctrl.abort());
const response = await fetch(
`${urlBase}/posts?userId=${userId}`,
{ signal: ctrl.signal },
);
return await response.json() as Post[];
},
);
}
function createReadmeResource() {
return createResource(async function fetchReadme() {
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] = createUsersResource();
const [posts] = createPostsResource(selectedUserId);
const [readmeHTML] = createReadmeResource();
return (
<>
<section innerHTML={readmeHTML()}></section>
<Switch>
<Match when={users()}>
{(users) => (
<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>
)}
</Match>
<Match when={users.loading}>
<p>Loading Users...</p>
</Match>
</Switch>
<Switch>
<Match when={posts()}>
{(posts) => (
<ul>
<For each={posts()}>
{(post) => <li>{post.title}</li>}
</For>
</ul>
)}
</Match>
<Match when={posts.loading}>
<p>Loading Posts...</p>
</Match>
<Match when={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);