|
| 1 | +import axios from "https://cdn.jsdelivr.net/npm/axios@1.6.7/dist/axios.min.js"; |
| 2 | + |
| 3 | +// --- Ayarlar --- |
| 4 | +axios.defaults.baseURL = "https://jsonplaceholder.typicode.com"; |
| 5 | +const POSTS_LIMIT = 5; |
| 6 | +let currentPage = 1; |
| 7 | + |
| 8 | +// --- HTML Elemanları --- |
| 9 | +const userList = document.querySelector("#user-list"); |
| 10 | +const postList = document.querySelector("#post-list"); |
| 11 | +const loadUsersBtn = document.querySelector("#load-users"); |
| 12 | +const loadMoreBtn = document.querySelector("#load-more"); |
| 13 | +const postForm = document.querySelector("#post-form"); |
| 14 | + |
| 15 | +// --- Bildirim Fonksiyonu --- |
| 16 | +const notify = (type, message) => { |
| 17 | + iziToast[type]({ |
| 18 | + title: type === "success" ? "Başarılı" : "Hata", |
| 19 | + message: message, |
| 20 | + position: "topRight", |
| 21 | + timeout: 3000, |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +// --- Kullanıcı İşlemleri --- |
| 26 | +async function loadUsers() { |
| 27 | + try { |
| 28 | + // Destructuring: res.data yerine { data } alıyoruz |
| 29 | + const { data: users } = await axios.get("/users"); |
| 30 | + renderUsers(users); |
| 31 | + notify("success", "Kullanıcılar yüklendi!"); |
| 32 | + } catch (err) { |
| 33 | + console.error(err); |
| 34 | + notify("error", "Kullanıcılar yüklenemedi."); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function renderUsers(users) { |
| 39 | + userList.innerHTML = users |
| 40 | + .map((user) => { |
| 41 | + // Destructuring: Nesne özelliklerini çıkarıyoruz |
| 42 | + const { name, email, company, website } = user; |
| 43 | + return ` |
| 44 | + <div class="user-card"> |
| 45 | + <h4>${name}</h4> |
| 46 | + <p>📧 ${email}</p> |
| 47 | + <p>🏢 ${company.name}</p> |
| 48 | + <p>🌐 ${website}</p> |
| 49 | + </div> |
| 50 | + `; |
| 51 | + }) |
| 52 | + .join(""); |
| 53 | +} |
| 54 | + |
| 55 | +loadUsersBtn.addEventListener("click", loadUsers); |
| 56 | + |
| 57 | +// --- Gönderi İşlemleri --- |
| 58 | + |
| 59 | +// 1. Gönderileri Getir (Async/Await + Params) |
| 60 | +async function loadPosts() { |
| 61 | + try { |
| 62 | + const { data: posts } = await axios.get("/posts", { |
| 63 | + params: { _limit: POSTS_LIMIT, _page: currentPage }, |
| 64 | + }); |
| 65 | + |
| 66 | + if (posts.length === 0) { |
| 67 | + notify("info", "Daha fazla gönderi yok."); |
| 68 | + loadMoreBtn.style.display = "none"; |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + renderPosts(posts); |
| 73 | + currentPage++; |
| 74 | + } catch (err) { |
| 75 | + console.error(err); |
| 76 | + notify("error", "Gönderiler alınamadı."); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function renderPosts(posts) { |
| 81 | + const html = posts |
| 82 | + .map((post) => { |
| 83 | + const { id, title, body } = post; // Destructuring |
| 84 | + // Olay Delegasyonu için data-id ve data-action özniteliklerini ekliyoruz |
| 85 | + // Inline onclick (onclick="...") KULLANMIYORUZ |
| 86 | + return ` |
| 87 | + <div class="post-card" id="post-${id}"> |
| 88 | + <div class="post-content"> |
| 89 | + <h4>${title}</h4> |
| 90 | + <p>${body}</p> |
| 91 | + </div> |
| 92 | + <div class="post-actions"> |
| 93 | + <button class="btn primary btn-sm" data-action="edit" data-id="${id}">Düzenle</button> |
| 94 | + <button class="btn danger btn-sm" data-action="delete" data-id="${id}">Sil</button> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + `; |
| 98 | + }) |
| 99 | + .join(""); |
| 100 | + |
| 101 | + postList.insertAdjacentHTML("beforeend", html); |
| 102 | +} |
| 103 | + |
| 104 | +loadMoreBtn.addEventListener("click", loadPosts); |
| 105 | + |
| 106 | +// 2. Yeni Gönderi Ekle (Form Submit + Destructuring) |
| 107 | +postForm.addEventListener("submit", async (e) => { |
| 108 | + e.preventDefault(); |
| 109 | + const formData = new FormData(e.target); |
| 110 | + |
| 111 | + // Form verilerini alırken trim() metodu kullanıyoruz (String metotları konusu) |
| 112 | + const title = formData.get("title").trim(); |
| 113 | + const body = formData.get("body").trim(); |
| 114 | + |
| 115 | + if (!title || !body) { |
| 116 | + notify("warning", "Lütfen tüm alanları doldurun."); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const newPost = { title, body, userId: 1 }; |
| 121 | + |
| 122 | + try { |
| 123 | + const { data } = await axios.post("/posts", newPost, { |
| 124 | + headers: { "Content-Type": "application/json" }, |
| 125 | + }); |
| 126 | + |
| 127 | + // Spread Syntax (...) ile nesne kopyalama |
| 128 | + const fakePostWithId = { ...newPost, id: data.id || Date.now() }; |
| 129 | + |
| 130 | + // Yeni gönderiyi manuel olarak listeye ekle (renderPosts fonksiyonunu burası için özelleştirebiliriz veya manuel HTML oluşturabiliriz) |
| 131 | + const { id, title: pTitle, body: pBody } = fakePostWithId; |
| 132 | + |
| 133 | + const postHTML = ` |
| 134 | + <div class="post-card new-post" id="post-${id}"> |
| 135 | + <div class="post-content"> |
| 136 | + <h4>${pTitle}</h4> |
| 137 | + <p>${pBody}</p> |
| 138 | + </div> |
| 139 | + <div class="post-actions"> |
| 140 | + <button class="btn primary btn-sm" data-action="edit" data-id="${id}">Düzenle</button> |
| 141 | + <button class="btn danger btn-sm" data-action="delete" data-id="${id}">Sil</button> |
| 142 | + </div> |
| 143 | + </div>`; |
| 144 | + |
| 145 | + postList.insertAdjacentHTML("afterbegin", postHTML); |
| 146 | + |
| 147 | + notify("success", "Yeni gönderi eklendi!"); |
| 148 | + e.target.reset(); |
| 149 | + } catch (err) { |
| 150 | + console.error(err); |
| 151 | + notify("error", "Gönderi eklenirken hata oluştu."); |
| 152 | + } |
| 153 | +}); |
| 154 | + |
| 155 | +// --- Olay Delegasyonu (Event Delegation) --- |
| 156 | +// Buttonlara tek tek olay dinleyicisi eklemek yerine, ebeveyn (postList) elemana ekliyoruz. |
| 157 | +postList.addEventListener("click", async (e) => { |
| 158 | + const target = e.target; |
| 159 | + |
| 160 | + // Tıklanan eleman bir buton mu kontrol et |
| 161 | + if (target.tagName !== "BUTTON") return; |
| 162 | + |
| 163 | + // Dataset üzerinden aksiyon ve ID bilgisini al (Destructuring) |
| 164 | + const { action, id } = target.dataset; |
| 165 | + |
| 166 | + if (action === "delete") { |
| 167 | + await deletePost(id); |
| 168 | + } else if (action === "edit") { |
| 169 | + await editPost(id); |
| 170 | + } |
| 171 | +}); |
| 172 | + |
| 173 | +// 3. Gönderi Sil |
| 174 | +async function deletePost(id) { |
| 175 | + if (!confirm("Bu gönderiyi silmek istediğine emin misin?")) return; |
| 176 | + |
| 177 | + try { |
| 178 | + await axios.delete(`/posts/${id}`); |
| 179 | + |
| 180 | + const postEl = document.getElementById(`post-${id}`); |
| 181 | + if (postEl) { |
| 182 | + postEl.remove(); |
| 183 | + notify("success", "Gönderi silindi!"); |
| 184 | + } |
| 185 | + } catch (err) { |
| 186 | + console.error(err); |
| 187 | + notify("error", "Silinirken hata oluştu."); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// 4. Gönderi Düzenle |
| 192 | +async function editPost(id) { |
| 193 | + const postEl = document.getElementById(`post-${id}`); |
| 194 | + const titleEl = postEl.querySelector("h4"); |
| 195 | + const bodyEl = postEl.querySelector("p"); |
| 196 | + |
| 197 | + const newTitle = prompt("Yeni başlık:", titleEl.innerText); |
| 198 | + if (newTitle === null) return; |
| 199 | + |
| 200 | + const newBody = prompt("Yeni içerik:", bodyEl.innerText); |
| 201 | + if (newBody === null) return; |
| 202 | + |
| 203 | + try { |
| 204 | + await axios.patch(`/posts/${id}`, { |
| 205 | + title: newTitle, |
| 206 | + body: newBody |
| 207 | + }); |
| 208 | + |
| 209 | + titleEl.innerText = newTitle; |
| 210 | + bodyEl.innerText = newBody; |
| 211 | + |
| 212 | + notify("success", "Gönderi güncellendi!"); |
| 213 | + } catch (err) { |
| 214 | + console.error(err); |
| 215 | + notify("error", "Güncelleme başarısız."); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +// --- Başlangıç --- |
| 220 | +loadPosts(); |
0 commit comments