-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from dylants/list_books
feat: list books
- Loading branch information
Showing
5 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use client"; | ||
|
||
import { useCallback, useEffect, useState } from "react"; | ||
import { Book as BookType } from "@/types/Book"; | ||
import { getBooks } from "@/lib/actions"; | ||
import Book from "@/components/Book"; | ||
|
||
export default function ListPage() { | ||
const [books, setBooks] = useState<Array<BookType>>([]); | ||
|
||
const loadBooks = useCallback(async () => { | ||
const loadedBooks = await getBooks(); | ||
setBooks(loadedBooks); | ||
}, [setBooks]); | ||
|
||
useEffect(() => { | ||
loadBooks(); | ||
}, [loadBooks]); | ||
|
||
return ( | ||
<> | ||
<h1 className="text-2xl text-customPalette-500 my-4">Books</h1> | ||
<hr className="mt-4 mb-8 border-customPalette-300" /> | ||
<div className="flex flex-col gap-8"> | ||
{books.map((book) => ( | ||
<Book key={book.isbn} book={book} /> | ||
))} | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,52 @@ | ||
import { Book as BookType } from "@/types/Book"; | ||
import Image from "next/image"; | ||
|
||
export default function Book({ book }: { book: BookType }) { | ||
function BookKey({ | ||
book, | ||
fieldName, | ||
}: { | ||
book: BookType; | ||
fieldName: keyof BookType; | ||
}) { | ||
let fieldNameToDisplay: string = fieldName; | ||
let fieldToDisplay = book[fieldName]?.toString(); | ||
if (fieldName === "publishedDate") { | ||
fieldNameToDisplay = "Published Date"; | ||
fieldToDisplay = book[fieldName]?.toLocaleDateString?.(); | ||
} else if (fieldName === "isbn") { | ||
fieldNameToDisplay = "ISBN"; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div>ISBN: {book.isbn}</div> | ||
<div>Title: {book.title}</div> | ||
<div>Author: {book.author}</div> | ||
{book.imageUrl && ( | ||
<span className="font-bold mr-1 capitalize">{fieldNameToDisplay}:</span> | ||
{fieldToDisplay} | ||
</div> | ||
); | ||
} | ||
|
||
export default function Book({ book }: { book: BookType }) { | ||
return ( | ||
<div className="flex gap-4"> | ||
{book.imageUrl ? ( | ||
<Image alt={book.title} src={book.imageUrl} width={128} height={192} /> | ||
) : ( | ||
<div className="border rounded-sm border-customPalette-300 w-[128px] h-[192px] flex justify-center items-center text-slate-900"> | ||
No Image | ||
</div> | ||
)} | ||
<div className="flex flex-col justify-between text-customPalette-500"> | ||
<div> | ||
<div className="text-xl font-bold mb-2">{book.title}</div> | ||
</div> | ||
<div> | ||
<BookKey book={book} fieldName="isbn" /> | ||
<BookKey book={book} fieldName="author" /> | ||
<BookKey book={book} fieldName="genre" /> | ||
<BookKey book={book} fieldName="publisher" /> | ||
<BookKey book={book} fieldName="publishedDate" /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters