|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { FC, useEffect, useState } from "react"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { supabase } from "@/lib/supabase"; |
| 6 | + |
| 7 | +// Define TypeScript types for books |
| 8 | +interface Book { |
| 9 | + id: string; |
| 10 | + title: string; |
| 11 | + description: string; |
| 12 | + link: string; |
| 13 | +} |
| 14 | + |
| 15 | +const BooksPage: FC = () => { |
| 16 | + const [books, setBooks] = useState<Book[]>([]); |
| 17 | + const [loading, setLoading] = useState<boolean>(true); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const fetchBooks = async () => { |
| 21 | + setLoading(true); |
| 22 | + const { data, error } = await supabase.from("books").select("*"); |
| 23 | + if (error) { |
| 24 | + console.error("Error fetching books:", error.message); |
| 25 | + } else { |
| 26 | + setBooks(data); |
| 27 | + } |
| 28 | + setLoading(false); |
| 29 | + }; |
| 30 | + |
| 31 | + fetchBooks(); |
| 32 | + }, []); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="min-h-screen bg-gray-100 flex flex-col items-center p-6"> |
| 36 | + <h1 className="text-4xl font-bold text-center text-gray-900">All Books</h1> |
| 37 | + <p className="text-lg text-gray-700 text-center mt-4 max-w-2xl"> |
| 38 | + Download high-quality educational resources for free. |
| 39 | + </p> |
| 40 | + |
| 41 | + <div className="mt-8 w-full max-w-2xl bg-white shadow-md rounded-lg p-6"> |
| 42 | + {loading ? ( |
| 43 | + <p className="text-center text-gray-600">Loading books...</p> |
| 44 | + ) : books.length === 0 ? ( |
| 45 | + <p className="text-center text-gray-600">No books available.</p> |
| 46 | + ) : ( |
| 47 | + books.map((book) => ( |
| 48 | + <div key={book.id} className="flex flex-col sm:flex-row justify-between items-start sm:items-center py-4 border-b last:border-b-0"> |
| 49 | + <div> |
| 50 | + <h3 className="text-lg font-semibold text-gray-900">{book.title}</h3> |
| 51 | + <p className="text-gray-600">{book.description}</p> |
| 52 | + </div> |
| 53 | + <a href={book.link} target="_blank" rel="noopener noreferrer"> |
| 54 | + <Button className="mt-3 sm:mt-0">Download</Button> |
| 55 | + </a> |
| 56 | + </div> |
| 57 | + )) |
| 58 | + )} |
| 59 | + </div> |
| 60 | + |
| 61 | + <footer className="mt-16 text-gray-500 text-sm"> |
| 62 | + © {new Date().getFullYear()} Corpora Inc - All Rights Reserved. |
| 63 | + </footer> |
| 64 | + </div> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +export default BooksPage; |
0 commit comments