-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
139 lines (115 loc) · 4.57 KB
/
script.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
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
const themeToggle = document.getElementById("themeToggle");
const body = document.body;
function toggleDarkTheme() {
body.classList.toggle("dark-theme");
const isDarkTheme = body.classList.contains("dark-theme");
if (isDarkTheme) {
themeToggle.classList.replace('bx-moon', 'bx-sun');
} else {
themeToggle.classList.replace('bx-sun', 'bx-moon');
}
}
themeToggle.addEventListener("click", toggleDarkTheme);
function openApiKeyModal() {
const apiKeyModal = document.getElementById('apiKeyModal');
apiKeyModal.style.display = 'flex';
// Close the modal when the close button is clicked
const closeBtn = document.querySelector('.api-close');
closeBtn.addEventListener('click', function () {
apiKeyModal.style.display = 'none';
});
// Handle API key submission
const apiKeySubmitBtn = document.getElementById('apiKeySubmit');
apiKeySubmitBtn.addEventListener('click', function () {
const apiKeyInput = document.getElementById('apiKeyInput');
const apiKeyValue = apiKeyInput.value.trim();
if (apiKeyValue) {
// Save the API key in localStorage
localStorage.setItem('apiKey', apiKeyValue);
// Close the modal
apiKeyModal.style.display = 'none';
}
});
}
const apiKey = localStorage.getItem('apiKey');
if (!apiKey) {
openApiKeyModal();
}
const credentials = {
method: 'GET',
headers: {
'X-RapidAPI-Key': apiKey,
'X-RapidAPI-Host': 'bhagavad-gita3.p.rapidapi.com'
}
};
let currentIndex = 0;
async function loadData(offset) {
try {
currentIndex += offset;
if (currentIndex < 0) {
currentIndex = 0;
} else if (currentIndex >= 18) {
currentIndex = 17;
}
let response = await fetch("https://bhagavad-gita3.p.rapidapi.com/v2/chapters/?limit=18", credentials);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
let data = await response.json();
const { chapter_number, name_translated, name_meaning, verses_count, chapter_summary } = data[currentIndex];
const sub_sec_one = document.querySelector(".sub_sec_one");
sub_sec_one.innerHTML = `
<div class="image">
<img src="shree-krishna.jpeg" alt="Shree Krishna">
</div>
<div class="detail">
<div class="info">
<span>Chapter ${chapter_number}</span>
<span>Verse ${verses_count}</span>
</div>
<h1>${name_translated}</h1>
<h2>${name_meaning}</h2>
</div>
<div class="description">
<p>
${chapter_summary}
</p>
</div>
`;
const sub_sec_three = document.querySelector(".sub_sec_three");
sub_sec_three.innerHTML = ""; // Clear previous content
// Fetch all verses for the current chapter
response = await fetch(`https://bhagavad-gita3.p.rapidapi.com/v2/chapters/${chapter_number}/verses/`, credentials);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const versesData = await response.json();
versesData.forEach((verse) => {
const verseElement = document.createElement("div");
verseElement.classList.add("verse");
verseElement.innerHTML = `
<span>Verse ${verse.verse_number}</span>
<p>${verse.translations.find(translation => translation.language === 'english').description}</p>
`;
sub_sec_three.appendChild(verseElement);
});
const modal = document.getElementById("myModal");
sub_sec_one.addEventListener("click", () => {
modal.style.display = "flex";
});
const closeModal = document.querySelector(".close");
closeModal.addEventListener("click", () => {
modal.style.display = "none";
});
// Store modal content
const modalContent = document.getElementById("modalContent");
modalContent.innerHTML = `
<h1>${name_translated}</h1>
<h2>${name_meaning}</h2>
<p>${chapter_summary}</p>
`;
} catch (error) {
console.error("Error loading data:", error.message);
}
}
loadData(0);