1
+ const form = document . querySelector ( 'form' ) ;
2
+ const slokaContainer = document . querySelector ( '#sloka-container' ) ;
3
+
4
+ const classes = Array . from ( { length : 18 } , ( _ , index ) => index + 1 ) ;
5
+ const verses = [ 47 , 72 , 43 , 42 , 29 , 47 , 30 , 28 , 34 , 42 , 55 , 20 , 34 , 27 , 20 , 24 , 28 , 78 ] ;
6
+ var gitaObj = { }
7
+
8
+ for ( var i = 0 ; i < classes . length ; i ++ ) {
9
+ gitaObj [ classes [ i ] ] = Array . from ( { length : verses [ i ] } , ( _ , index ) => index + 1 ) ;
10
+ }
11
+
12
+ console . log ( gitaObj )
13
+
14
+ window . onload = function ( ) {
15
+ var chapterSel = document . getElementById ( "chapter" ) ;
16
+ var slokaSel = document . getElementById ( "sloka" ) ;
17
+
18
+ for ( var x in gitaObj ) {
19
+ chapterSel . options [ chapterSel . options . length ] = new Option ( x , x ) ;
20
+ }
21
+
22
+ chapterSel . onchange = function ( ) {
23
+ //empty sloka dropdowns
24
+ slokaSel . length = 1 ;
25
+ if ( this . selectedIndex < 1 )
26
+ return ;
27
+
28
+ //display correct values
29
+ var y = gitaObj [ this . value ] ;
30
+ console . log ( y )
31
+ for ( var i = 1 ; i <= y . length ; i ++ ) {
32
+ slokaSel . options [ slokaSel . options . length ] = new Option ( i , i ) ;
33
+ }
34
+ }
35
+ }
36
+
37
+ form . addEventListener ( 'submit' , event => {
38
+ event . preventDefault ( ) ;
39
+ const chapter = document . querySelector ( '#chapter' ) . value ;
40
+ const sloka = document . querySelector ( '#sloka' ) . value ;
41
+ const url = `https://bhagavadgitaapi.in/slok/${ chapter } /${ sloka } ` ;
42
+
43
+
44
+ //tried code
45
+ // Check if data is already in local storage
46
+ const storedData = localStorage . getItem ( `gita${ chapter } -${ sloka } ` ) ;
47
+ if ( storedData ) {
48
+ // Data is already in local storage
49
+ const data = JSON . parse ( storedData ) ;
50
+ const slokaText = data . slok ;
51
+ const formattedSloka = slokaText . replace ( / \n / g, '<br>' ) ;
52
+ const translation = data . siva . et ;
53
+ slokaContainer . innerHTML = `
54
+ <h2>||श्रीमद्भगवद्-गीता ${ chapter } .${ sloka } ||</h2>
55
+ <h3>${ formattedSloka } </h3>
56
+ <br>
57
+ <p><strong>Translation:</strong> ${ translation } </p>
58
+ ` ;
59
+ } else {
60
+ // Data is not in local storage, make API request
61
+ fetch ( url )
62
+ . then ( response => response . json ( ) )
63
+ . then ( data => {
64
+ // Store data in local storage
65
+ localStorage . setItem ( `gita${ chapter } -${ sloka } ` , JSON . stringify ( data ) ) ;
66
+ // Use the data as needed
67
+ const slokaText = data . slok ;
68
+ const formattedSloka = slokaText . replace ( / \n / g, '<br>' ) ;
69
+ const translation = data . siva . et ;
70
+ slokaContainer . innerHTML = `
71
+ <h2>||श्रीमद्भगवद्-गीता ${ chapter } .${ sloka } ||</h2>
72
+ <h3>${ formattedSloka } </h3>
73
+ <br>
74
+ <p><strong>Translation:</strong> ${ translation } </p>
75
+ ` ;
76
+ } )
77
+ . catch ( error => console . error ( error ) ) ;
78
+ }
79
+
80
+ //
81
+
82
+ // fetch(url)
83
+ // .then(response => response.json())
84
+ // .then(data => {
85
+ // const slokaText = data.slok;
86
+ // const formattedSloka = slokaText.replace(/\n/g, '<br>');
87
+ // const translation = data.siva.et;
88
+ // slokaContainer.innerHTML = `
89
+ // <h2>||श्रीमद्भगवद्-गीता ${chapter}.${sloka}||</h2>
90
+ // <h3>${formattedSloka}</h3>
91
+ // <br>
92
+ // <p><strong>Translation:</strong> ${translation}</p>
93
+ // `;
94
+ // })
95
+ // .catch(error => console.error(error));
96
+ } ) ;
0 commit comments