const MAX_POKEMON = 151; const listWrapper = document.querySelector(".list-wrapper"); const searchInput = document.querySelector("#search-input"); const numberFilter = document.querySelector("#number"); const nameFilter = document.querySelector("#name"); const notFoundMessage = document.querySelector("#not-found-message"); let allPokemons = []; fetch(`./get-pokemon.php`) .then((response) => response.json()) .then((data) => { if (Array.isArray(data)) { allPokemons = data; cacheImages(allPokemons); displayPokemons(allPokemons); } else { console.error("Unexpected response format:", data); } }); async function fetchPokemonDataBeforeRedirect(id) { try { const pokemon = await fetch(`./get-pokemon.php?id=${id}`).then((res) => res.json() ); if (pokemon.error) { throw new Error(pokemon.error); } return true; } catch (error) { console.error("Failed to fetch Pokémon data before redirect:", error); return false; } } function cacheImages(pokemons) { pokemons.forEach((pokemon) => { const img = new Image(); img.src = pokemon.image_url; img.onload = () => { localStorage.setItem(`pokemon_image_${pokemon.id}`, pokemon.image_url); }; }); } function getCachedImageUrl(pokemonId) { return localStorage.getItem(`pokemon_image_${pokemonId}`); } function displayPokemons(pokemons) { listWrapper.innerHTML = ""; pokemons.forEach((pokemon) => { const cachedImageUrl = getCachedImageUrl(pokemon.id); const imageUrl = cachedImageUrl || pokemon.image_url; const listItem = document.createElement("div"); listItem.className = "list-item"; listItem.innerHTML = `
#${pokemon.id}
#${pokemon.name}