|
| 1 | +"use client"; |
| 2 | +import React, { useState, useEffect } from "react"; |
| 3 | +import { Input, List, Pagination, Modal, Spin } from "antd"; |
| 4 | +import { PlusOutlined } from "@ant-design/icons"; |
| 5 | +import BookForm from "./bookform"; |
| 6 | + |
| 7 | +interface Book { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | + url: string; |
| 11 | +} |
| 12 | + |
| 13 | +async function getData() { |
| 14 | + const res = await fetch("http://localhost:3000/books"); |
| 15 | + if (!res.ok) { |
| 16 | + throw new Error("Failed to fetch data"); |
| 17 | + } |
| 18 | + return res.json(); |
| 19 | +} |
| 20 | + |
| 21 | +const BookList = () => { |
| 22 | + const [books, setBooks] = useState<Book[]>([]); |
| 23 | + const [loading, setLoading] = useState(true); |
| 24 | + const [currentPage, setCurrentPage] = useState(1); |
| 25 | + const [pageSize, setPageSize] = useState(20); |
| 26 | + const [editingBook, setEditingBook] = useState<Book | null>(null); |
| 27 | + const [editing, setEditing] = useState(false); |
| 28 | + const [showAddBookForm, setShowAddBookForm] = useState(false); |
| 29 | + const handleAddBook = () => { |
| 30 | + setShowAddBookForm(true); |
| 31 | + }; |
| 32 | + const saveBookData = async (bookData) => { |
| 33 | + try { |
| 34 | + const res = await fetch("http://localhost:3000/books", { |
| 35 | + method: "POST", |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + }, |
| 39 | + body: JSON.stringify(bookData), |
| 40 | + }); |
| 41 | + if (!res.ok) { |
| 42 | + throw new Error("Failed to add book"); |
| 43 | + } |
| 44 | + const newBook = await res.json(); |
| 45 | + setBooks((prevBooks) => [...prevBooks, newBook]); |
| 46 | + setShowAddBookForm(false); // Close the form/modal on success |
| 47 | + } catch (error) { |
| 48 | + console.error(error); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + const fetchBooks = async () => { |
| 54 | + try { |
| 55 | + const data = await getData(); |
| 56 | + setBooks(data.data); |
| 57 | + setLoading(false); |
| 58 | + } catch (error) { |
| 59 | + console.error(error); |
| 60 | + setLoading(false); |
| 61 | + } |
| 62 | + }; |
| 63 | + fetchBooks(); |
| 64 | + }, []); |
| 65 | + |
| 66 | + const currentBooks = books.slice( |
| 67 | + (currentPage - 1) * pageSize, |
| 68 | + currentPage * pageSize, |
| 69 | + ); |
| 70 | + const handlePageChange = (page, pageSize) => { |
| 71 | + setCurrentPage(page); |
| 72 | + setPageSize(pageSize); |
| 73 | + }; |
| 74 | + const handleEdit = (book: Book) => { |
| 75 | + setEditingBook(book); |
| 76 | + setEditing(true); |
| 77 | + }; |
| 78 | + |
| 79 | + const handleSave = async () => { |
| 80 | + if (!editingBook) return; |
| 81 | + try { |
| 82 | + const res = await fetch(`http://localhost:3000/books/${editingBook.id}`, { |
| 83 | + method: "PUT", |
| 84 | + headers: { |
| 85 | + "Content-Type": "application/json", |
| 86 | + }, |
| 87 | + body: JSON.stringify(editingBook), |
| 88 | + }); |
| 89 | + if (!res.ok) { |
| 90 | + throw new Error("Failed to update book"); |
| 91 | + } |
| 92 | + setBooks((prevBooks) => |
| 93 | + prevBooks.map((b) => (b.id === editingBook.id ? editingBook : b)), |
| 94 | + ); |
| 95 | + setEditingBook(null); |
| 96 | + setEditing(false); |
| 97 | + } catch (error) { |
| 98 | + console.error(error); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + const handleCancel = () => { |
| 103 | + setEditingBook(null); |
| 104 | + setEditing(false); |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <main className="flex min-h-screen flex-col items-center justify-between p-24"> |
| 109 | + <h1>Books</h1> |
| 110 | + <button onClick={handleAddBook} className="add-book-button"> |
| 111 | + <PlusOutlined /> |
| 112 | + </button> |
| 113 | + <Modal open={showAddBookForm} onCancel={() => setShowAddBookForm(false)} okText="Save"> |
| 114 | + <BookForm |
| 115 | + onSave={saveBookData} |
| 116 | + onCancel={() => setShowAddBookForm(false)} |
| 117 | + /> |
| 118 | + </Modal> |
| 119 | + |
| 120 | + {loading ? ( |
| 121 | + <Spin size="large" fullscreen /> |
| 122 | + ) : ( |
| 123 | + <List |
| 124 | + bordered |
| 125 | + pagination={{ |
| 126 | + current: currentPage, |
| 127 | + pageSize, |
| 128 | + total: books.length, |
| 129 | + responsive: true, |
| 130 | + showSizeChanger: true, |
| 131 | + size: "small", |
| 132 | + onChange: handlePageChange, |
| 133 | + }} |
| 134 | + dataSource={books} |
| 135 | + renderItem={(book: Book) => ( |
| 136 | + <List.Item key={book.id} onClick={() => handleEdit(book)}> |
| 137 | + {editing && editingBook?.id === book.id ? ( |
| 138 | + <Modal |
| 139 | + title="Edit Book" |
| 140 | + open={editing && editingBook?.id === book.id} |
| 141 | + onOk={handleSave} |
| 142 | + onCancel={(e) => { |
| 143 | + e.stopPropagation(); // Prevent event bubbling |
| 144 | + handleCancel(); |
| 145 | + }} |
| 146 | + > |
| 147 | + <Input |
| 148 | + value={editingBook.name} |
| 149 | + onChange={(e) => |
| 150 | + setEditingBook({ ...editingBook, name: e.target.value }) |
| 151 | + } |
| 152 | + /> |
| 153 | + </Modal> |
| 154 | + ) : ( |
| 155 | + book.name |
| 156 | + )} |
| 157 | + </List.Item> |
| 158 | + )} |
| 159 | + /> |
| 160 | + )} |
| 161 | + </main> |
| 162 | + ); |
| 163 | +}; |
| 164 | + |
| 165 | +export default BookList; |
0 commit comments