// --- Section control --- function showSection(sectionId) { ['search-section','scanner-form-section','manual-search-section','recommender-section','details-section'] .forEach(id => document.getElementById(id).style.display = 'none'); document.getElementById(sectionId).style.display = 'flex'; } function goBackToStart() { showSection('search-section'); } // --- Button handlers --- document.getElementById('snap-option').addEventListener('click', () => showSection('scanner-form-section')); document.getElementById('search-option').addEventListener('click', () => showSection('manual-search-section')); document.getElementById('recommender-option').addEventListener('click', () => showSection('recommender-section')); // --- Image upload & backend call --- document.getElementById('scan-submit-button').addEventListener('click', async () => { const button = document.getElementById('scan-submit-button'); button.textContent = 'Scanning...'; button.disabled = true; const fileInput = document.getElementById('book-image'); if (!fileInput.files.length) { alert("Please upload an image first."); button.textContent = 'Find Book Details'; button.disabled = false; return; } const formData = new FormData(); formData.append("image", fileInput.files[0]); const genre = document.getElementById('genre-input')?.value || ""; formData.append("genre", genre); try { const HF_API = "https://ransted-shelf-scanner.hf.space/api/predict/"; const res = await fetch(HF_API, { method: "POST", body: formData }); const data = await res.json(); if (data.data && data.data[0].books.length > 0) { showBookDetails(data.data[0].books[0]); } else { alert("No books detected. Try again with a clearer image."); } } catch (err) { console.error(err); alert("Error contacting server."); } button.textContent = 'Find Book Details'; button.disabled = false; }); // --- Display book details --- function showBookDetails(data) { showSection('details-section'); document.getElementById('book-cover').src = data.cover_image || 'https://via.placeholder.com/200x300?text=No+Cover'; document.getElementById('book-title').textContent = data.title; document.getElementById('book-author').textContent = 'By ' + (data.authors || "Unknown"); document.getElementById('book-genre').textContent = data.publisher || "N/A"; document.getElementById('book-synopsis').textContent = data.description || "No description available"; document.getElementById('book-isbn').textContent = data.isbn ? `ISBN: ${data.isbn}` : "ISBN: Not found"; // Ratings placeholder document.getElementById('book-stars').textContent = "⭐⭐⭐⭐"; document.getElementById('review-count').textContent = "N/A"; // Recommendations placeholder document.getElementById('recommendations-list').innerHTML = "

No recommendations implemented yet.

"; // Price links const priceTableBody = document.getElementById('price-table-body'); priceTableBody.innerHTML = ''; for (const [store, link] of Object.entries(data.buy_links || {})) { const row = document.createElement('tr'); row.innerHTML = ` ${store} - - Shop Now `; priceTableBody.appendChild(row); } }