const MAX_POKEMON = 151; const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds 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 = []; document.addEventListener("DOMContentLoaded", () => { const cachedData = localStorage.getItem("pokemons"); const cacheTimestamp = localStorage.getItem("pokemons_timestamp"); if (cachedData && cacheTimestamp && (Date.now() - cacheTimestamp) < CACHE_DURATION) { allPokemons = JSON.parse(cachedData); displayPokemons(allPokemons); } else { fetchPokemons(); } }); function fetchPokemons() { fetch(`./get-pokemon.php`) .then((response) => response.json()) .then((data) => { if (Array.isArray(data)) { allPokemons = data; localStorage.setItem("pokemons", JSON.stringify(allPokemons)); localStorage.setItem("pokemons_timestamp", Date.now().toString()); displayPokemons(allPokemons); } else { console.error("Unexpected response format:", data); } }) .catch((error) => { console.error("Error fetching Pokémon data:", error); }); } async function fetchPokemonDataBeforeRedirect(id) { try { const response = await fetch(`./get-pokemon.php?id=${id}`); const text = await response.text(); console.log(`Response for Pokémon ID ${id}:`, text); const pokemon = JSON.parse(text); 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 displayPokemons(pokemons) { listWrapper.innerHTML = ""; pokemons.forEach((pokemon) => { const listItem = document.createElement("div"); listItem.className = "list-item"; listItem.innerHTML = `
#${pokemon.id}
#${pokemon.name}