|
| 1 | +/** @jsxImportSource hono/jsx */ |
| 2 | +/// <reference lib="DOM" /> |
| 3 | + |
| 4 | +import { useEffect, useState } from "hono/jsx"; |
| 5 | +import { createRoot } from "hono/jsx/dom/client"; |
| 6 | +import type { User } from "jsonplaceholder-types/types/user"; |
| 7 | +import type { Post } from "jsonplaceholder-types/types/post"; |
| 8 | + |
| 9 | +const urlBase = "https://jsonplaceholder.typicode.com"; |
| 10 | + |
| 11 | +function useUsers() { |
| 12 | + const [users, setUsers] = useState<User[] | undefined>(undefined); |
| 13 | + const [loading, setLoading] = useState(false); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + const ctrl = new AbortController(); |
| 17 | + const signal = ctrl.signal; |
| 18 | + |
| 19 | + setLoading(true); |
| 20 | + fetch(`${urlBase}/users`, { signal }) |
| 21 | + .then(async (response) => { |
| 22 | + const data = await response.json(); |
| 23 | + if (signal.aborted) return; |
| 24 | + setUsers(data); |
| 25 | + }) |
| 26 | + .catch((error) => { |
| 27 | + if (!(error instanceof DOMException && error.name === "AbortError")) { |
| 28 | + throw error; |
| 29 | + } |
| 30 | + }) |
| 31 | + .finally(() => setLoading(false)); |
| 32 | + |
| 33 | + return () => ctrl.abort(); |
| 34 | + }, []); |
| 35 | + |
| 36 | + return [users, { loading }] as const; |
| 37 | +} |
| 38 | + |
| 39 | +function usePosts(userId: number | undefined) { |
| 40 | + const [posts, setPosts] = useState<Post[] | undefined>(undefined); |
| 41 | + const [loading, setLoading] = useState(false); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (userId === undefined) return; |
| 45 | + |
| 46 | + const ctrl = new AbortController(); |
| 47 | + const signal = ctrl.signal; |
| 48 | + setLoading(true); |
| 49 | + fetch(`${urlBase}/posts?userId=${userId}`, { signal }) |
| 50 | + .then(async (response) => { |
| 51 | + const data = await response.json(); |
| 52 | + if (signal.aborted) return; |
| 53 | + setPosts(data); |
| 54 | + }) |
| 55 | + .catch((error) => { |
| 56 | + if (!(error instanceof DOMException && error.name === "AbortError")) { |
| 57 | + throw error; |
| 58 | + } |
| 59 | + }) |
| 60 | + .finally(() => setLoading(false)); |
| 61 | + |
| 62 | + return () => ctrl.abort(); |
| 63 | + }, [userId]); |
| 64 | + |
| 65 | + return [posts, { loading }] as const; |
| 66 | +} |
| 67 | + |
| 68 | +function App() { |
| 69 | + const [selectedUserId, setSelectedUserId] = useState<number | undefined>( |
| 70 | + undefined, |
| 71 | + ); |
| 72 | + const [users, { loading: loadingUsers }] = useUsers(); |
| 73 | + const [posts, { loading: loadingPosts }] = usePosts(selectedUserId); |
| 74 | + |
| 75 | + const handleChange = ({ currentTarget }: Event) => { |
| 76 | + if (!(currentTarget instanceof HTMLSelectElement)) return; |
| 77 | + setSelectedUserId(+currentTarget.value); |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <h1>Buildless Hono 4 app</h1> |
| 83 | + {users !== undefined && ( |
| 84 | + <label> |
| 85 | + Select User: |
| 86 | + <select onChange={handleChange}> |
| 87 | + <option hidden selected></option> |
| 88 | + {users.map((user) => ( |
| 89 | + <option key={user.id} value={user.id}> |
| 90 | + @{user.username}: {user.name} |
| 91 | + </option> |
| 92 | + ))} |
| 93 | + </select> |
| 94 | + </label> |
| 95 | + ) || loadingUsers && <p>Loading Users...</p>} |
| 96 | + {posts !== undefined && ( |
| 97 | + <ul> |
| 98 | + {posts.map((post) => <li key={post.id}>{post.title}</li>)} |
| 99 | + </ul> |
| 100 | + ) || |
| 101 | + loadingPosts && <p>Loading Posts...</p> || |
| 102 | + users !== undefined && <p>Select User to view posts</p>} |
| 103 | + <p> |
| 104 | + Data Source: |
| 105 | + <a href="https://jsonplaceholder.typicode.com/" target="_blank"> |
| 106 | + JSONPlaceholder |
| 107 | + </a> |
| 108 | + </p> |
| 109 | + </> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +createRoot(document.body).render(<App />); |
0 commit comments