|
| 1 | +/** @jsxImportSource solid-js */ |
| 2 | + |
| 3 | +import { |
| 4 | + type Accessor, |
| 5 | + createResource, |
| 6 | + createSignal, |
| 7 | + For, |
| 8 | + onCleanup, |
| 9 | + Show, |
| 10 | +} from "solid-js"; |
| 11 | +import { render } from "solid-js/web"; |
| 12 | +import type { User } from "jsonplaceholder-types/types/user"; |
| 13 | +import type { Post } from "jsonplaceholder-types/types/post"; |
| 14 | + |
| 15 | +const urlBase = "https://jsonplaceholder.typicode.com"; |
| 16 | + |
| 17 | +function createUsersResource() { |
| 18 | + return createResource(async function fetchUsers() { |
| 19 | + const ctrl = new AbortController(); |
| 20 | + onCleanup(() => ctrl.abort()); |
| 21 | + const response = await fetch(`${urlBase}/users`, { signal: ctrl.signal }); |
| 22 | + return await response.json() as User[]; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function createPostsResource(userId: Accessor<number | undefined>) { |
| 27 | + return createResource( |
| 28 | + userId, |
| 29 | + async function fetchPosts(userId) { |
| 30 | + const ctrl = new AbortController(); |
| 31 | + onCleanup(() => ctrl.abort()); |
| 32 | + const response = await fetch(`${urlBase}/posts?userId=${userId}`, { |
| 33 | + signal: ctrl.signal, |
| 34 | + }); |
| 35 | + return await response.json() as Post[]; |
| 36 | + }, |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +const App = () => { |
| 41 | + const [selectedUserId, setSelectedUserId] = createSignal<number>(); |
| 42 | + const [users] = createUsersResource(); |
| 43 | + const [posts] = createPostsResource(selectedUserId); |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <h1>Buildless SolidJS 1 app</h1> |
| 48 | + <Show |
| 49 | + when={users()} |
| 50 | + fallback={ |
| 51 | + <Show when={users.loading}> |
| 52 | + <p>Loading Users...</p> |
| 53 | + </Show> |
| 54 | + } |
| 55 | + > |
| 56 | + {(users) => ( |
| 57 | + <label> |
| 58 | + Select User: |
| 59 | + <select |
| 60 | + onChange={(e) => setSelectedUserId(+e.currentTarget.value)} |
| 61 | + > |
| 62 | + <For each={users()}> |
| 63 | + {(user) => ( |
| 64 | + <option value={user.id}>@{user.username}: {user.name}</option> |
| 65 | + )} |
| 66 | + </For> |
| 67 | + </select> |
| 68 | + </label> |
| 69 | + )} |
| 70 | + </Show> |
| 71 | + <Show |
| 72 | + when={posts()} |
| 73 | + fallback={ |
| 74 | + <Show |
| 75 | + when={posts.loading} |
| 76 | + fallback={ |
| 77 | + <Show when={users}> |
| 78 | + <p>Select User to view posts</p> |
| 79 | + </Show> |
| 80 | + } |
| 81 | + > |
| 82 | + <p>Loading Posts...</p> |
| 83 | + </Show> |
| 84 | + } |
| 85 | + > |
| 86 | + {(posts) => ( |
| 87 | + <ul> |
| 88 | + <For each={posts()}> |
| 89 | + {(post) => <li>{post.title}</li>} |
| 90 | + </For> |
| 91 | + </ul> |
| 92 | + )} |
| 93 | + </Show> |
| 94 | + <p> |
| 95 | + Data Source: |
| 96 | + <a href="https://jsonplaceholder.typicode.com/" target="_blank"> |
| 97 | + JSONPlaceholder |
| 98 | + </a> |
| 99 | + </p> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +render(() => <App />, document.body); |
0 commit comments