Skip to content

Commit fb77e1c

Browse files
committed
Final Commit
1 parent c4dbe62 commit fb77e1c

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

solidjs-tutorial/src/AddBook.tsx

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,64 @@
1-
import { createSignal, JSX } from "solid-js";
1+
import { createResource, createSignal } from "solid-js";
2+
import { For, Show } from "solid-js/web";
3+
import { searchBooks } from "./utils/searchBooks";
24

35
import type { Setter } from "solid-js";
46
import type { Book } from "./App";
7+
58
export interface IAddBookProps {
69
setBooks: Setter<Book[]>;
710
}
811

912
const emptyBook: Book = { title: "", author: "" };
1013

1114
export default function AddBook(props: IAddBookProps) {
12-
const [newBook, setNewBook] = createSignal(emptyBook);
13-
const addBook: JSX.EventHandler<HTMLButtonElement, MouseEvent> = (event) => {
14-
event.preventDefault();
15-
props.setBooks((books) => [...books, newBook()]);
16-
setNewBook(emptyBook);
17-
};
15+
const [input, setInput] = createSignal("");
16+
const [query, setQuery] = createSignal("");
17+
const [data] = createResource<Array<Book>, string>(query, searchBooks);
18+
1819
return (
19-
<form>
20-
<div>
21-
<label for="title">Book name</label>
22-
<input
23-
id="title"
24-
value={newBook().title}
25-
onInput={(e) => {
26-
setNewBook({ ...newBook(), title: e.currentTarget.value });
27-
}}
28-
/>
29-
</div>
30-
<br />
31-
<div>
32-
<label for="author">Author</label>
33-
<input
34-
id="author"
35-
value={newBook().author}
36-
onInput={(e) => {
37-
setNewBook({ ...newBook(), author: e.currentTarget.value });
20+
<>
21+
<form>
22+
<div>
23+
<label for="title">Search books</label>
24+
<input
25+
id="title"
26+
value={input()}
27+
onInput={(e) => {
28+
setInput(e.currentTarget.value);
29+
}}
30+
/>
31+
</div>
32+
<button
33+
type="submit"
34+
onClick={(e) => {
35+
e.preventDefault();
36+
setQuery(input());
3837
}}
39-
/>
40-
</div>
41-
<button type="submit" onClick={addBook}>
42-
Add book
43-
</button>
44-
</form>
38+
>
39+
Search
40+
</button>
41+
</form>
42+
<Show when={!data.loading} fallback={<>Searching...</>}>
43+
<ul>
44+
<For each={data()}>
45+
{(book) => (
46+
<li>
47+
{book.title} by {book.author}{" "}
48+
<button
49+
aria-label={`Add ${book.title} by ${book.author} to the bookshelf`}
50+
onClick={(e) => {
51+
e.preventDefault();
52+
props.setBooks((books) => [...books, book]);
53+
}}
54+
>
55+
Add
56+
</button>
57+
</li>
58+
)}
59+
</For>
60+
</ul>
61+
</Show>
62+
</>
4563
);
4664
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export async function searchBooks(query: string): Promise<any> {
2+
if (query.trim() === "") return [];
3+
4+
const resp = await fetch(
5+
`https://openlibrary.org/search.json?q=${encodeURIComponent(query)}`,
6+
{
7+
headers: {
8+
Accept: "application/json",
9+
},
10+
}
11+
);
12+
13+
const results = await resp.json();
14+
return results.docs.slice(0, 10).map(({ title, author_name }: any) => ({
15+
title,
16+
author: author_name?.join(", "),
17+
}));
18+
}

0 commit comments

Comments
 (0)