-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (30 loc) · 1.24 KB
/
app.js
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
const headerElement = document.querySelector("#header");
const hadithTextElement = document.querySelector("#hadith-text");
const bookElement = document.querySelector("#book");
const customButtonElement = document.querySelector("#customBtn");
const randomButtonElement = document.querySelector("#randomBtn");
const fetchJSON = async (hname, number, custom) => {
let url = `https://random-hadith-generator.vercel.app/${hname}`;
if (custom) {
url += `/${number}`;
}
const response = await fetch(url, { mode: "cors" });
const { data } = await response.json();
return data;
};
const updateHadithData = ({ header, hadith_english, refno }) => {
headerElement.innerHTML = header;
hadithTextElement.innerHTML = hadith_english;
bookElement.innerHTML = refno;
};
randomButtonElement.addEventListener("click", async (_) => {
const hname = document.querySelector("#randomHadith").value;
const hadithData = await fetchJSON(hname);
updateHadithData(hadithData);
});
customButtonElement.addEventListener("click", async (_) => {
const hname = document.querySelector("#customHadith").value;
const number = document.querySelector("#customInput").value;
const hadithData = await fetchJSON(hname, number, true);
updateHadithData(hadithData);
});