diff --git a/25_fetch_await/index.html b/25_fetch_await/index.html index bfb8e3f..0e218fc 100644 --- a/25_fetch_await/index.html +++ b/25_fetch_await/index.html @@ -4,20 +4,23 @@ Fetch API +

Hledání uživatele (Fetch API)

- - + + +

Připraveno k hledání....

+
diff --git a/25_fetch_await/script.js b/25_fetch_await/script.js index 86a5c37..df20698 100644 --- a/25_fetch_await/script.js +++ b/25_fetch_await/script.js @@ -3,6 +3,10 @@ const btnNajdi = document.getElementById("btnNajdi"); const btnVse = document.getElementById("btnVse"); const stav = document.getElementById("stav"); const vysledek = document.getElementById("vysledek"); +const postyKontejner = document.getElementById("posty-kontejner"); +const btnPosty = document.getElementById("btnPosty"); + +let aktualniUserId = null; // FUNKCE 1 : Načtení všech uživatelů async function nactiVsechny() { @@ -36,6 +40,9 @@ async function nactiVsechny() { async function nactiJednoho() { const id = inputId.value; vysledek.innerHTML = ""; + postyKontejner.innerHTML = ""; + btnPosty.disabled = true; + aktualniUserId = null; stav.innerText = `Hledám uživatele s ID: ${id}`; try{ @@ -43,16 +50,23 @@ async function nactiJednoho() { if(!odpoved.ok) { throw new Error(`Uživatel s ID ${id} neexistuje (HTTP ${odpoved.status})`); } + const u = await odpoved.json(); vysledek.innerHTML = ` -

${u.name}

- +
+

${u.name} (ID:${u.id})

+ +
`;//AltGR + ý = ` (Template literal) stav.innerText = "Uživatel nalezen" + //aktivace vyhledávání postů + aktualniUserId = u.id; + btnPosty.disabled = false; + btnPosty.innerText = `Načíst příspvěky (autor: ${u.name})` } catch (error) { stav.innerText = "Došlo k chybě"; @@ -61,8 +75,36 @@ async function nactiJednoho() { } finally { console.log(`Pokus o načtení ID ${id} dokončen`); } +} +async function nactiPosty() { + //Pojistka pokud nemame ID + if(!aktualniUserId) return; + postyKontejner.innerHTML = "

Načítám příspěvky

"; + btnPosty.disabled = true; //zabráníme opakovanému klikání během načítaní + + try { + const odpoved = await fetch(`https://jsonplaceholder.typicode.com/users/${aktualniUserId}/posts`) + if(!odpoved.ok) { + throw new Error(`Uživatel s ID ${id} neexistuje (HTTP ${odpoved.status})`); + } + const posty = await odpoved.json(); + const html = posty.map(p => ` +
+ ${p.title} +

${p.body}

+
+ `).join(""); + + postyKontejner.innerHTML = `

Příspěvky autora (${posty.length})

` +html; + stav.innerText = "Data načtena" + } catch(e){ + postyKontejner.innerHTML = `

chyba:${e.message}

` + } finally { + btnPosty.disabled = false + } } btnVse.addEventListener("click",nactiVsechny); -btnNajdi.addEventListener("click",nactiJednoho); \ No newline at end of file +btnNajdi.addEventListener("click",nactiJednoho); +btnPosty.addEventListener("click",nactiPosty) \ No newline at end of file diff --git a/25_fetch_await/style.css b/25_fetch_await/style.css new file mode 100644 index 0000000..8f54c09 --- /dev/null +++ b/25_fetch_await/style.css @@ -0,0 +1,21 @@ +body { + line-height: 1.6; + max-width: 800px; + margin: 20px auto; + padding: 0 20px; +} +#stav { + font-style: italic; + color: #666 +} +.user-card { + background-color: #f8f8f8; + border-radius: 15px; + border: 1px solid lightskyblue; + padding: 15px; +} +.post { + border-left: 5px blue solid; + padding: 10px; + margin: 10px 0; +} \ No newline at end of file