-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (138 loc) · 4.63 KB
/
Copy pathscript.js
File metadata and controls
166 lines (138 loc) · 4.63 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const searchBtn = document.getElementById("button-search");
function listenForm() {
searchBtn.addEventListener("click", (e) => {
e.preventDefault();
const dayInput = parseInt(document.getElementById("day-input").value);
const monthInput = parseInt(document.getElementById("month-input").value);
const selectInput = document.getElementById("select-input").value;
const errorMsg = document.getElementById("input-error");
if (dayInput <= 0 || dayInput > 31 || !dayInput) {
errorMsg.textContent = "Day value has to be between 1-31";
return;
}
if (monthInput <= 0 || monthInput > 12 || !monthInput) {
errorMsg.textContent = "Month value has to be between 1-12";
return;
}
if (selectInput === "not-chosen") {
errorMsg.textContent = "Pick selection between births, deaths and events";
return;
}
if (
(monthInput === 2 && dayInput === 30) ||
(monthInput === 2 && dayInput === 31)
) {
errorMsg.textContent = "February doesn't have day 30 and 31, input again";
return;
}
if (monthInput === 4 && dayInput === 31) {
errorMsg.textContent = "April doesn't have day 31, input again";
return;
}
if (monthInput === 6 && dayInput === 31) {
errorMsg.textContent = "June doesn't have day 31, input again";
return;
}
if (monthInput === 9 && dayInput === 31) {
errorMsg.textContent = "September doesn't have day 31, input again";
return;
}
if (monthInput === 11 && dayInput === 31) {
errorMsg.textContent = "November doesn't have day 31, input again";
return;
}
// passed checks
errorMsg.textContent = "";
fetchData(dayInput, monthInput, selectInput);
});
}
async function fetchData(day, month, event) {
try {
const response = await fetch(
`https://history.muffinlabs.com/date/${month}/${day}`
);
if (!response.ok) {
throw new Error("Network error is not OK");
}
const data = await response.json();
// next here
displayData(data, event);
dataGlobal = data;
eventGlobal = event;
} catch (error) {
console.error("Error is: ", error);
}
}
function displayData(data, chosenEvent) {
const userData = data.data[chosenEvent];
if (!userData || !Array.isArray(userData)) {
console.error(`No Data Found for ${chosenEvent}`);
return;
}
// results info
const daySearched = document.getElementById("day-searched");
daySearched.textContent = data.date;
const totalSearched = document.getElementById("total-searched");
totalSearched.textContent = `Total ${chosenEvent}: ${userData.length}`;
const appendRow = document.getElementById("t-body");
appendRow.innerHTML = "";
userData.forEach((data, index) => {
const row = document.createElement("tr");
// index
const numberRow = document.createElement("th");
numberRow.scope = "row";
numberRow.textContent = index + 1;
// years
const yearRow = document.createElement("td");
yearRow.textContent = data.year;
// name
const nameRow = document.createElement("td");
nameRow.textContent = data.text;
// links
const linkRow = document.createElement("td");
if (data.links && data.links.length > 0) {
const firstLink = data.links[0];
const anchor = document.createElement("a");
anchor.href = firstLink.link;
anchor.textContent = firstLink.title;
anchor.target = "_blank";
anchor.rel = "noopener noreferrer";
linkRow.appendChild(anchor);
} else {
linkRow.textContent = "No links available";
}
row.appendChild(numberRow);
row.appendChild(yearRow);
row.appendChild(nameRow);
row.appendChild(linkRow);
appendRow.appendChild(row);
});
}
function addFooter() {
const footerDate = document.getElementById("footer-date");
const date = new Date();
const year = date.getFullYear();
footerDate.textContent = `@ Arman ${year}`;
}
let dataGlobal = null;
let eventGlobal = null;
const sortBtn = document.getElementById("year");
let isYearAsc = true;
// main
function main() {
listenForm();
addFooter();
sortBtn.addEventListener("click", () => {
if (!dataGlobal || !eventGlobal) return; // Skip if no data
isYearAsc = !isYearAsc; // Toggle sort direction
const sortedData = [...dataGlobal.data[eventGlobal]]; // Copy array
sortedData.sort((a, b) => {
const yearA = parseInt(a.year) || 0;
const yearB = parseInt(b.year) || 0;
return isYearAsc ? yearA - yearB : yearB - yearA;
});
displayData({ data: { [eventGlobal]: sortedData } }, eventGlobal);
sortBtn.textContent = `Year ${isYearAsc ? "↑" : "↓"}`; // UI feedback
});
}
main();