|
| 1 | +const ERROR_ELEMENT_ID = "error-message"; |
| 2 | + |
| 3 | +function displayErrorMessage(message) { |
| 4 | + const errorElement = document.getElementById(ERROR_ELEMENT_ID); |
| 5 | + if (errorElement) { |
| 6 | + errorElement.textContent = message; |
| 7 | + errorElement.style.display = "block"; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +async function getLiveData() { |
| 12 | + try { |
| 13 | + const response = await fetch("../../fellows_search.json", { mode: "cors" }); |
| 14 | + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); |
| 15 | + return await response.json(); |
| 16 | + } catch (error) { |
| 17 | + console.error("Error fetching or processing data:", error); |
| 18 | + displayErrorMessage("Failed to load data. Please try again later."); |
| 19 | + return null; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +async function main() { |
| 24 | + const data = await getLiveData(); |
| 25 | + if (!data) return; |
| 26 | + |
| 27 | + const input = document.querySelector("input[type=search]"); |
| 28 | + const container = document.createElement("div"); |
| 29 | + container.id = "search-results"; |
| 30 | + input.after(container); |
| 31 | + |
| 32 | + const miniSearch = new MiniSearch({ |
| 33 | + fields: ["name", "projects"], |
| 34 | + extractField: (doc, field) => |
| 35 | + field === "projects" |
| 36 | + ? doc.projects.map((p) => p.name).join(" ") |
| 37 | + : doc[field], |
| 38 | + }); |
| 39 | + miniSearch.addAll(data); |
| 40 | + |
| 41 | + input.addEventListener("input", (event) => { |
| 42 | + const query = event.target.value; |
| 43 | + const results = miniSearch.search(query); |
| 44 | + const findings = results.map((result) => { |
| 45 | + const entry = data.find((e) => e.id === result.id); |
| 46 | + return { |
| 47 | + id: entry.id, |
| 48 | + name: entry.name, |
| 49 | + url: entry.url, |
| 50 | + projects: entry.projects, |
| 51 | + }; |
| 52 | + }); |
| 53 | + |
| 54 | + const list = document.createElement("ul"); |
| 55 | + findings.forEach((finding) => { |
| 56 | + const item = document.createElement("li"); |
| 57 | + const link = document.createElement("a"); |
| 58 | + const dl = document.createElement("dl"); |
| 59 | + finding.projects.forEach((project) => { |
| 60 | + const projectNameDt = document.createElement("dt"); |
| 61 | + projectNameDt.appendChild(document.createTextNode("Project:")); |
| 62 | + const projectNameDd = document.createElement("dd"); |
| 63 | + projectNameDd.appendChild(document.createTextNode(project.name)); |
| 64 | + const projectMentorDt = document.createElement("dt"); |
| 65 | + projectMentorDt.appendChild(document.createTextNode("Mentor(s):")); |
| 66 | + const projectMentorDd = document.createElement("dd"); |
| 67 | + projectMentorDd.appendChild( |
| 68 | + document.createTextNode(project.mentors.join(", ")), |
| 69 | + ); |
| 70 | + dl.appendChild(projectNameDt); |
| 71 | + dl.appendChild(projectNameDd); |
| 72 | + dl.appendChild(projectMentorDt); |
| 73 | + dl.appendChild(projectMentorDd); |
| 74 | + }); |
| 75 | + |
| 76 | + link.setAttribute("href", finding.url); |
| 77 | + link.appendChild(document.createTextNode(finding.name)); |
| 78 | + item.appendChild(link); |
| 79 | + item.appendChild(dl); |
| 80 | + list.append(item); |
| 81 | + }); |
| 82 | + |
| 83 | + container.replaceChildren(list); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +main().catch((error) => { |
| 88 | + console.error("Error loading data:", error); |
| 89 | + displayErrorMessage("Failed to load data. Please try again later."); |
| 90 | +}); |
0 commit comments