|
1 |
| -const hadithHeader = document.querySelector("#header"); |
2 |
| -const hadithText = document.querySelector("#hadith-text"); |
3 |
| -const book = document.querySelector("#book"); |
4 |
| -const customBtn = document.querySelector("#customBtn"); |
5 |
| -const wrapper = document.querySelector("#wrapper"); |
| 1 | +const headerElement = document.querySelector("#header"); |
| 2 | +const hadithTextElement = document.querySelector("#hadith-text"); |
| 3 | +const bookElement = document.querySelector("#book"); |
6 | 4 |
|
7 |
| -const fetchHadithJSON = async (hname) => { |
8 |
| - const response = await fetch( |
9 |
| - `https://random-hadith-generator.vercel.app/${hname}`, |
10 |
| - { mode: "cors" } |
11 |
| - ); |
12 |
| - const { data } = await response.json(); |
13 |
| - return data; |
14 |
| -}; |
| 5 | +const customButtonElement = document.querySelector("#customBtn"); |
| 6 | +const randomButtonElement = document.querySelector("#randomBtn"); |
15 | 7 |
|
16 |
| -const fetchCustom = async (hname, number) => { |
17 |
| - const response = await fetch( |
18 |
| - `https://random-hadith-generator.vercel.app/${hname}/${number}`, |
19 |
| - { mode: "cors" } |
20 |
| - ); |
| 8 | +const fetchJSON = async (hname, number, custom) => { |
| 9 | + let url = `https://random-hadith-generator.vercel.app/${hname}`; |
| 10 | + if (custom) { |
| 11 | + url += `/${number}`; |
| 12 | + } |
| 13 | + const response = await fetch(url, { mode: "cors" }); |
21 | 14 | const { data } = await response.json();
|
22 | 15 | return data;
|
23 | 16 | };
|
24 | 17 |
|
25 |
| -const loadHadithData = async (e) => { |
26 |
| - let hname; |
27 |
| - |
28 |
| - switch (e.target.id) { |
29 |
| - case "bukhari": |
30 |
| - hname = "bukhari"; |
31 |
| - break; |
32 |
| - case "muslim": |
33 |
| - hname = "muslim"; |
34 |
| - break; |
35 |
| - case "customBtn": |
36 |
| - const customHadith = document.querySelector("#customHadith").value; |
37 |
| - const number = document.querySelector("#customInput").value; |
38 |
| - if (customHadith === "bukhari") { |
39 |
| - hname = "bukhari"; |
40 |
| - const hadithData = await fetchCustom("bukhari", number); |
41 |
| - |
42 |
| - const { header, hadith_english, refno } = hadithData; |
43 |
| - hadithHeader.innerHTML = header; |
44 |
| - hadithText.innerHTML = hadith_english; |
45 |
| - book.innerHTML = refno; |
46 |
| - } else if (customHadith === "muslim") { |
47 |
| - hname = "muslim"; |
48 |
| - const hadithData = await fetchCustom("muslim", number); |
49 |
| - const { header, hadith_english, refno } = hadithData; |
50 |
| - hadithHeader.innerHTML = header; |
51 |
| - hadithText.innerHTML = hadith_english; |
52 |
| - book.innerHTML = refno; |
53 |
| - } |
54 |
| - return; |
55 |
| - default: |
56 |
| - return; |
57 |
| - } |
58 |
| - const hadithData = await fetchHadithJSON(hname); |
59 |
| - const { header, hadith_english, refno } = hadithData; |
60 |
| - hadithHeader.innerHTML = header; |
61 |
| - hadithText.innerHTML = hadith_english; |
62 |
| - book.innerHTML = refno; |
| 18 | +const updateHadithData = ({ header, hadith_english, refno }) => { |
| 19 | + headerElement.innerHTML = header; |
| 20 | + hadithTextElement.innerHTML = hadith_english; |
| 21 | + bookElement.innerHTML = refno; |
63 | 22 | };
|
64 | 23 |
|
65 |
| -const loadHadithOnButtonClick = async (e) => { |
66 |
| - await loadHadithData(e); |
67 |
| -}; |
| 24 | +randomButtonElement.addEventListener("click", async (_) => { |
| 25 | + const hname = document.querySelector("#randomHadith").value; |
| 26 | + const hadithData = await fetchJSON(hname); |
| 27 | + updateHadithData(hadithData); |
| 28 | +}); |
68 | 29 |
|
69 |
| -wrapper.addEventListener("click", loadHadithOnButtonClick); |
| 30 | +customButtonElement.addEventListener("click", async (_) => { |
| 31 | + const hname = document.querySelector("#customHadith").value; |
| 32 | + const number = document.querySelector("#customInput").value; |
| 33 | + const hadithData = await fetchJSON(hname, number, true); |
| 34 | + updateHadithData(hadithData); |
| 35 | +}); |
0 commit comments