-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
66 lines (56 loc) · 1.5 KB
/
javascript.js
File metadata and controls
66 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { setRows } from "./modules/setRows.js";
let table = document.querySelector(".grid-table");
let loading = document.querySelector(".loading");
let search = document.querySelector("#search");
let isLoading = true;
let spellsData = [];
let rows = [];
let searchTerm = "";
search.addEventListener("input", (event) => {
searchTerm = event.target.value;
console.log(searchTerm);
updateRows();
});
//Fetches all spell data from open5e api, then creates/maps into useable rows
async function fetchData() {
try {
const data = await fetch("https://api.open5e.com/v1/spells/?limit=1000");
spellsData = await data.json();
rows = setRows(spellsData);
isLoading = false;
handleLoading();
} catch (error) {
console.error("Fetch error:", error);
}
}
function updateRows() {
while (table.childNodes.length > 2) {
table.removeChild(table.lastChild);
}
rows
.filter((row) => {
return Array.from(row.childNodes).some((element) => {
return element.innerText
.toLowerCase()
.includes(searchTerm.toLowerCase());
});
})
.map((item) => {
table.appendChild(item);
});
}
//updates the screen
function handleLoading() {
if (isLoading) {
//shows loading element & hides the table
loading.style.display = "static";
table.classList.add("hidden");
} else {
updateRows();
//hides loading element & shows table
loading.style.display = "none";
table.classList.remove("hidden");
}
}
handleLoading();
fetchData();