|
| 1 | +console.log("forum-fetch.js loaded!"); |
| 2 | + |
| 3 | +document.addEventListener("DOMContentLoaded", async function () { |
| 4 | + console.log("FAQ loader running..."); |
| 5 | + |
| 6 | + const list = document.getElementById("list"); |
| 7 | + const empty = document.getElementById("empty"); |
| 8 | + const searchInput = document.getElementById("q"); |
| 9 | + const sortSelect = document.getElementById("sort"); |
| 10 | + |
| 11 | + const loadingText = document.createElement("p"); |
| 12 | + loadingText.textContent = "Loading FAQs..."; |
| 13 | + list.parentElement.insertBefore(loadingText, list); |
| 14 | + |
| 15 | + try { |
| 16 | + const res = await fetch("/assets/data/faq.json"); |
| 17 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 18 | + |
| 19 | + const data = await res.json(); |
| 20 | + const topics = data.topics || []; |
| 21 | + loadingText.remove(); |
| 22 | + |
| 23 | + if (!topics.length) { |
| 24 | + empty.style.display = "block"; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + let visibleCount = 10; |
| 29 | + let filteredTopics = [...topics]; |
| 30 | + |
| 31 | + function filterAndSort() { |
| 32 | + const q = searchInput.value.trim().toLowerCase(); |
| 33 | + const sortKey = sortSelect.value; |
| 34 | + |
| 35 | + // Filter |
| 36 | + filteredTopics = topics.filter((t) => |
| 37 | + (t.title + " " + (t.excerpt || "")).toLowerCase().includes(q) |
| 38 | + ); |
| 39 | + |
| 40 | + // Sort |
| 41 | + filteredTopics.sort((a, b) => { |
| 42 | + if (sortKey === "created_at" || sortKey === "last_posted_at") { |
| 43 | + return new Date(b[sortKey]) - new Date(a[sortKey]); |
| 44 | + } |
| 45 | + return (b[sortKey] || 0) - (a[sortKey] || 0); |
| 46 | + }); |
| 47 | + |
| 48 | + visibleCount = 10; |
| 49 | + renderTopics(); |
| 50 | + } |
| 51 | + |
| 52 | + function renderTopics() { |
| 53 | + list.innerHTML = ""; |
| 54 | + |
| 55 | + const shown = filteredTopics.slice(0, visibleCount); |
| 56 | + |
| 57 | + if (!shown.length) { |
| 58 | + empty.style.display = "block"; |
| 59 | + return; |
| 60 | + } else { |
| 61 | + empty.style.display = "none"; |
| 62 | + } |
| 63 | + |
| 64 | + shown.forEach((t) => { |
| 65 | + const card = document.createElement("div"); |
| 66 | + card.className = "faq-card"; |
| 67 | + card.style.cssText = |
| 68 | + "border:1px solid #ddd;padding:12px;border-radius:8px;background:#fff;"; |
| 69 | + |
| 70 | + const excerpt = |
| 71 | + t.excerpt && t.excerpt.trim().length > 0 |
| 72 | + ? t.excerpt |
| 73 | + : "No description available."; |
| 74 | + |
| 75 | + card.innerHTML = ` |
| 76 | + <h4 style="margin-bottom:8px;"> |
| 77 | + <strong>${t.title}</strong> |
| 78 | + </h4> |
| 79 | + <p style="color:#333;line-height:1.4;"> |
| 80 | + ${excerpt} |
| 81 | + <a href="${t.url}" target="_blank" rel="noopener noreferrer" |
| 82 | + style="text-decoration:none;color:#0069c2;" data-noicon> |
| 83 | + Read more |
| 84 | + </a> |
| 85 | + </p> |
| 86 | + <p style="color:#666;font-size:0.9em;"> |
| 87 | + Last updated: ${new Date(t.last_posted_at).toLocaleDateString("en-GB")} | |
| 88 | + Replies: ${t.posts_count} | Views: ${t.views} |
| 89 | + </p> |
| 90 | + `; |
| 91 | + list.appendChild(card); |
| 92 | + }); |
| 93 | + |
| 94 | + if (visibleCount < filteredTopics.length) { |
| 95 | + const btn = document.createElement("button"); |
| 96 | + btn.textContent = "Load more"; |
| 97 | + btn.style.cssText = |
| 98 | + "padding:8px 16px;margin:12px 0 12px auto;display:block;border:1px solid #ccc;border-radius:8px;background:#f9f9f9;cursor:pointer;"; |
| 99 | + btn.addEventListener("click", () => { |
| 100 | + visibleCount += 10; |
| 101 | + renderTopics(); |
| 102 | + }); |
| 103 | + list.appendChild(btn); |
| 104 | + } |
| 105 | + |
| 106 | + // 🧽 JS-based CSS injection to remove external icon pseudo-element |
| 107 | + if (!document.getElementById("noicon-style")) { |
| 108 | + const style = document.createElement("style"); |
| 109 | + style.id = "noicon-style"; |
| 110 | + style.textContent = ` |
| 111 | + [data-noicon]::after { |
| 112 | + content: none !important; |
| 113 | + background-image: none !important; |
| 114 | + } |
| 115 | + `; |
| 116 | + document.head.appendChild(style); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Event listeners |
| 121 | + searchInput.addEventListener("input", filterAndSort); |
| 122 | + sortSelect.addEventListener("change", filterAndSort); |
| 123 | + |
| 124 | + // Initial render |
| 125 | + filterAndSort(); |
| 126 | + } catch (err) { |
| 127 | + console.error("Error loading FAQ:", err); |
| 128 | + loadingText.textContent = "Failed to load FAQs."; |
| 129 | + } |
| 130 | +}); |
0 commit comments