From a786d0e05018002d79171da4096893914027e179 Mon Sep 17 00:00:00 2001 From: Caleb Fultz Date: Wed, 21 Aug 2024 08:46:39 -0400 Subject: [PATCH] Corrected autocomplete --- app.js | 36 +++++++++++++++++++++++++++++++++--- index.html | 50 +++++++++++++++++++------------------------------- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/app.js b/app.js index 45c8ca7..63e9d4c 100644 --- a/app.js +++ b/app.js @@ -61,11 +61,41 @@ document.addEventListener('DOMContentLoaded', () => { }); async function fetchCardSuggestions(query) { - const response = await fetch(`https://api.scryfall.com/cards/autocomplete?q=${encodeURIComponent(query)}`); - const data = await response.json(); - return data.data; + try { + const response = await fetch(`https://api.scryfall.com/cards/autocomplete?q=${encodeURIComponent(query)}`); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + const data = await response.json(); + return data.data; + } catch (error) { + console.error('Fetch error:', error); + return []; + } } +document.getElementById('card-name').addEventListener('input', async function() { + const query = this.value.trim(); + if (query.length < 2) { // Only search for queries longer than 1 character + document.getElementById('suggestions').innerHTML = ''; + return; + } + const suggestions = await fetchCardSuggestions(query); + const suggestionsDiv = document.getElementById('suggestions'); + suggestionsDiv.innerHTML = ''; // Clear previous suggestions + + suggestions.forEach(suggestion => { + const div = document.createElement('div'); + div.textContent = suggestion; + div.onclick = () => { + document.getElementById('card-name').value = suggestion; + suggestionsDiv.innerHTML = ''; // Clear suggestions after selection + }; + suggestionsDiv.appendChild(div); + }); +}); + + async function fetchCardDetails(cardName, setCode) { // Use the Scryfall 'named' endpoint to fetch the exact card by name and set code const response = await fetch(`https://api.scryfall.com/cards/named?exact=${encodeURIComponent(cardName)}&set=${encodeURIComponent(setCode)}`); diff --git a/index.html b/index.html index d870f7b..b2c3e5c 100644 --- a/index.html +++ b/index.html @@ -30,45 +30,34 @@

MTG Card CSV Generator

-
-
- - -
+ +
+ + +
- -
- - -
+
+ +
- -
- - -
+
+ +
- -
- - -
+
+ +
- -
- - -
- - + - - - +
@@ -86,6 +75,5 @@ navigator.serviceWorker.register('sw.js') } -