Skip to content

Commit d6bbed8

Browse files
committed
load data from local json + hindi translations
1 parent 053e978 commit d6bbed8

File tree

1 file changed

+69
-56
lines changed

1 file changed

+69
-56
lines changed

script.js

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ for (var i = 0; i < classes.length; i++) {
99
gitaObj[classes[i]] = Array.from({ length: verses[i] }, (_, index) => index + 1);
1010
}
1111

12-
console.log(gitaObj)
13-
1412
window.onload = function () {
1513
var chapterSel = document.getElementById("chapter");
1614
var slokaSel = document.getElementById("sloka");
@@ -27,7 +25,6 @@ window.onload = function () {
2725

2826
//display correct values
2927
var y = gitaObj[this.value];
30-
console.log(y)
3128
for (var i = 1; i <= y.length; i++) {
3229
slokaSel.options[slokaSel.options.length] = new Option(i, i);
3330
}
@@ -38,66 +35,82 @@ form.addEventListener('submit', event => {
3835
event.preventDefault();
3936
const chapter = document.querySelector('#chapter').value;
4037
const sloka = document.querySelector('#sloka').value;
41-
const url = `https://bhagavadgitaapi.in/slok/${chapter}/${sloka}`;
38+
// const url = `https://bhagavadgitaapi.in/slok/${chapter}/${sloka}`;
39+
const queryId = `BG${chapter}.${sloka}`;
4240

4341

4442
// Check if data is already in local storage
45-
const storedData = localStorage.getItem(`gita${chapter}-${sloka}`);
46-
if (storedData) {
47-
// Data is already in local storage
48-
const data = JSON.parse(storedData);
49-
const slokaText = data.slok;
50-
const formattedSloka = slokaText.replace(/\n/g, '<br>');
51-
const translation = data.siva.et;
52-
slokaContainer.innerHTML = `
53-
<h2>||श्रीमद्‍भगवद्‍-गीता ${chapter}.${sloka}||</h2>
54-
<h3>${formattedSloka}</h3>
55-
<br>
56-
<p><strong>Translation:</strong> ${translation}</p>
57-
`;
58-
} else {
59-
// Data is not in local storage, make API request
60-
var headers = {};
61-
fetch(url,
62-
{
63-
method : "GET",
64-
mode: 'cors',
65-
headers: {
66-
'Access-Control-Allow-Origin':'*'
67-
}
68-
})
69-
.then(response => response.json())
70-
.then(data => {
71-
// Store data in local storage
72-
localStorage.setItem(`gita${chapter}-${sloka}`, JSON.stringify(data));
73-
// Use the data as needed
74-
const slokaText = data.slok;
75-
const formattedSloka = slokaText.replace(/\n/g, '<br>');
76-
const translation = data.siva.et;
77-
slokaContainer.innerHTML = `
78-
<h2>||श्रीमद्‍भगवद्‍-गीता ${chapter}.${sloka}||</h2>
79-
<h3>${formattedSloka}</h3>
80-
<br>
81-
<p><strong>Translation:</strong> ${translation}</p>
82-
`;
83-
})
84-
.catch(error => console.error(error));
85-
}
86-
87-
//
88-
89-
// fetch(url)
90-
// .then(response => response.json())
91-
// .then(data => {
92-
// const slokaText = data.slok;
93-
// const formattedSloka = slokaText.replace(/\n/g, '<br>');
94-
// const translation = data.siva.et;
95-
// slokaContainer.innerHTML = `
43+
// const storedData = localStorage.getItem(`gita${chapter}-${sloka}`);
44+
// if (storedData) {
45+
// // Data is already in local storage
46+
// const data = JSON.parse(storedData);
47+
// const slokaText = data.slok;
48+
// const formattedSloka = slokaText.replace(/\n/g, '<br>');
49+
// const translation = data.siva.et;
50+
// slokaContainer.innerHTML = `
9651
// <h2>||श्रीमद्‍भगवद्‍-गीता ${chapter}.${sloka}||</h2>
9752
// <h3>${formattedSloka}</h3>
9853
// <br>
9954
// <p><strong>Translation:</strong> ${translation}</p>
10055
// `;
56+
// } else {
57+
// // Data is not in local storage, make API request
58+
// var headers = {};
59+
// fetch(url,
60+
// {
61+
// method : "GET",
62+
// mode: 'cors',
63+
// headers: {
64+
// 'Access-Control-Allow-Origin':'*'
65+
// }
10166
// })
102-
// .catch(error => console.error(error));
67+
// .then(response => response.json())
68+
// .then(data => {
69+
// // Store data in local storage
70+
// localStorage.setItem(`gita${chapter}-${sloka}`, JSON.stringify(data));
71+
// // Use the data as needed
72+
// const slokaText = data.slok;
73+
// const formattedSloka = slokaText.replace(/\n/g, '<br>');
74+
// const translation = data.siva.et;
75+
// slokaContainer.innerHTML = `
76+
// <h2>||श्रीमद्‍भगवद्‍-गीता ${chapter}.${sloka}||</h2>
77+
// <h3>${formattedSloka}</h3>
78+
// <br>
79+
// <p><strong>Translation:</strong> ${translation}</p>
80+
// `;
81+
// })
82+
// .catch(error => console.error(error));
83+
// }
84+
85+
86+
87+
// use the local json file
88+
89+
fetch('shlokas_limited.json')
90+
.then(response => response.json())
91+
.then(data => {
92+
// Store data in local storage
93+
// localStorage.setItem(`gita${chapter}-${sloka}`, JSON.stringify(data));
94+
95+
// Filter the data based on the user input
96+
const filteredData = data.filter(item => item.id === queryId);
97+
console.log(filteredData[0])
98+
99+
100+
// Use the data as needed
101+
const slokaText = filteredData[0].slok;
102+
const formattedSloka = slokaText.replace(/\n/g, '<br>');
103+
const translation = filteredData[0].siva.et;
104+
const hindi = filteredData[0].tej.ht;
105+
slokaContainer.innerHTML = `
106+
<h2>||श्रीमद्‍भगवद्‍-गीता ${chapter}.${sloka}||</h2>
107+
<h3>${formattedSloka}</h3>
108+
<br>
109+
<p> ${hindi}</p>
110+
<br>
111+
<p><strong>Translation:</strong> ${translation}</p>
112+
`;
113+
})
114+
.catch(error => console.error(error));
115+
103116
});

0 commit comments

Comments
 (0)