File size: 10,431 Bytes
8e8aafd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Free Library - BookWorm Haven</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Fredoka+One&display=swap" rel="stylesheet">
</head>
<body class="min-h-screen flex flex-col bg-gradient-to-br from-blue-50 to-purple-50">
    <custom-navbar></custom-navbar>
    
    <main class="flex-grow container mx-auto px-4 py-12">
        <div class="text-center mb-16">
            <h1 class="text-4xl md:text-5xl font-bold text-gray-900 font-fredoka mb-4">Free <span class="text-purple-600">Library</span></h1>
            <p class="text-xl text-gray-600 max-w-2xl mx-auto">Search millions of books from around the world - all available for free reading.</p>
        </div>

        <!-- Search Section -->
        <div class="max-w-2xl mx-auto mb-12">
            <div class="relative">
                <input type="text" id="bookSearch" placeholder="Search for books by title, author or ISBN..." 
                    class="w-full px-6 py-4 rounded-full border border-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-600 shadow-lg">
                <button id="searchBtn" 
                    class="absolute right-2 top-2 bg-purple-600 text-white p-2 rounded-full hover:bg-purple-700 transition">
                    <i data-feather="search"></i>
                </button>
            </div>
            <div class="flex justify-center gap-4 mt-4">
                <button class="filter-btn active" data-filter="all">All</button>
                <button class="filter-btn" data-filter="fiction">Fiction</button>
                <button class="filter-btn" data-filter="nonfiction">Non-Fiction</button>
                <button class="filter-btn" data-filter="classics">Classics</button>
            </div>
        </div>

        <!-- Books Grid -->
        <div id="booksGrid" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8">
            <!-- Books will be loaded here dynamically -->
            <div class="text-center py-8">
                <i data-feather="book-open" class="w-12 h-12 mx-auto text-gray-400"></i>
                <p class="text-gray-500 mt-2">Search for books to display results</p>
            </div>
        </div>

        <!-- Loading and Pagination -->
        <div id="loading" class="text-center py-8 hidden">
            <div class="inline-block animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-purple-600 mb-2"></div>
            <p>Loading books...</p>
        </div>
        <div id="pagination" class="flex justify-center mt-12 hidden">
            <button class="px-4 py-2 mx-1 rounded bg-purple-600 text-white disabled:opacity-50" id="prevBtn">Previous</button>
            <div class="flex items-center mx-4">
                <span id="pageInfo">Page 1 of 1</span>
            </div>
            <button class="px-4 py-2 mx-1 rounded bg-purple-600 text-white disabled:opacity-50" id="nextBtn">Next</button>
        </div>
    </main>

    <custom-footer></custom-footer>

    <!-- Scripts -->
    <script src="components/navbar.js"></script>
    <script src="components/footer.js"></script>
    <script src="script.js"></script>
    <script>
        feather.replace();

        // Book search functionality
        document.addEventListener('DOMContentLoaded', function() {
            const searchBtn = document.getElementById('searchBtn');
            const bookSearch = document.getElementById('bookSearch');
            const booksGrid = document.getElementById('booksGrid');
            const loading = document.getElementById('loading');
            const pagination = document.getElementById('pagination');
            const prevBtn = document.getElementById('prevBtn');
            const nextBtn = document.getElementById('nextBtn');
            const pageInfo = document.getElementById('pageInfo');
            const filterBtns = document.querySelectorAll('.filter-btn');

            let currentPage = 1;
            let currentQuery = '';
            let currentFilter = 'all';

            // Search function using Open Library API
            async function searchBooks(query, page = 1) {
                loading.classList.remove('hidden');
                booksGrid.innerHTML = '';
                
                try {
                    const response = await fetch(`https://openlibrary.org/search.json?q=${encodeURIComponent(query)}&page=${page}`);
                    const data = await response.json();
                    
                    if (data.docs && data.docs.length > 0) {
                        displayBooks(data.docs);
                        updatePagination(data.numFound, page);
                    } else {
                        booksGrid.innerHTML = `
                            <div class="text-center py-8 col-span-full">
                                <i data-feather="frown" class="w-12 h-12 mx-auto text-gray-400"></i>
                                <p class="text-gray-500 mt-2">No books found. Try a different search term.</p>
                            </div>
                        `;
                        pagination.classList.add('hidden');
                    }
                } catch (error) {
                    console.error('Error fetching books:', error);
                    booksGrid.innerHTML = `
                        <div class="text-center py-8 col-span-full">
                            <i data-feather="alert-triangle" class="w-12 h-12 mx-auto text-red-400"></i>
                            <p class="text-gray-500 mt-2">Error loading books. Please try again later.</p>
                        </div>
                    `;
                } finally {
                    loading.classList.add('hidden');
                    feather.replace();
                }
            }

            // Display books in grid
            function displayBooks(books) {
                booksGrid.innerHTML = '';
                
                books.forEach(book => {
                    const coverId = book.cover_i;
                    const coverUrl = coverId ? `https://covers.openlibrary.org/b/id/${coverId}-M.jpg` : 'http://static.photos/book/200x300';
                    const title = book.title || 'Untitled';
                    const author = book.author_name ? book.author_name.join(', ') : 'Unknown Author';
                    const publishYear = book.first_publish_year || 'Unknown Year';
                    const subjects = book.subject ? book.subject.slice(0, 3).join(', ') : '';
                    
                    const bookCard = document.createElement('div');
                    bookCard.className = 'bg-white rounded-xl overflow-hidden shadow-lg hover:shadow-xl transition duration-300 transform hover:-translate-y-1';
                    bookCard.innerHTML = `
                        <div class="h-64 bg-gray-100 flex items-center justify-center">
                            <img src="${coverUrl}" alt="${title}" class="h-full w-full object-contain">
                        </div>
                        <div class="p-6">
                            <h3 class="text-lg font-bold mb-1 line-clamp-2">${title}</h3>
                            <p class="text-sm text-gray-600 mb-2">by ${author}</p>
                            <div class="flex justify-between text-xs text-gray-500 mb-3">
                                <span>${publishYear}</span>
                                <span class="bg-purple-100 text-purple-800 px-2 py-1 rounded-full">${subjects}</span>
                            </div>
                            <a href="https://openlibrary.org${book.key}" target="_blank" 
                               class="block text-center w-full px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition">
                                Read Free
                            </a>
                        </div>
                    `;
                    
                    booksGrid.appendChild(bookCard);
                });
                
                pagination.classList.remove('hidden');
            }

            // Update pagination controls
            function updatePagination(totalBooks, page) {
                const totalPages = Math.ceil(totalBooks / 100); // Open Library returns 100 results per page
                pageInfo.textContent = `Page ${page} of ${totalPages}`;
                prevBtn.disabled = page <= 1;
                nextBtn.disabled = page >= totalPages;
            }

            // Event listeners
            searchBtn.addEventListener('click', () => {
                currentQuery = bookSearch.value.trim();
                if (currentQuery) {
                    currentPage = 1;
                    searchBooks(currentQuery, currentPage);
                }
            });

            bookSearch.addEventListener('keypress', (e) => {
                if (e.key === 'Enter') {
                    currentQuery = bookSearch.value.trim();
                    if (currentQuery) {
                        currentPage = 1;
                        searchBooks(currentQuery, currentPage);
                    }
                }
            });

            prevBtn.addEventListener('click', () => {
                if (currentPage > 1) {
                    currentPage--;
                    searchBooks(currentQuery, currentPage);
                }
            });

            nextBtn.addEventListener('click', () => {
                currentPage++;
                searchBooks(currentQuery, currentPage);
            });

            // Filter buttons
            filterBtns.forEach(btn => {
                btn.addEventListener('click', () => {
                    filterBtns.forEach(b => b.classList.remove('active'));
                    btn.classList.add('active');
                    currentFilter = btn.dataset.filter;
                    // Implement filter logic here based on currentFilter
                });
            });
        });
    </script>
</body>
</html>