11// projects section script
22
33// get project container
4- let container = document . getElementById ( "project-container" )
4+ let container
55let org_name = "LizardByte"
66let base_url = `https://app.${ org_name . toLowerCase ( ) } .dev`
77let cache_repo = "dashboard"
88
99
10+ function onDocumentReady ( callback ) {
11+ if ( document . readyState === "loading" ) {
12+ document . addEventListener ( "DOMContentLoaded" , callback )
13+ return
14+ }
15+
16+ callback ( )
17+ }
18+
19+ function fetchWithNoCache ( url , options = { } ) {
20+ return fetch ( url , {
21+ ...options ,
22+ cache : "no-store" ,
23+ } )
24+ }
25+
26+ function fetchJson ( url ) {
27+ return fetchWithNoCache ( url )
28+ . then ( function ( response ) {
29+ if ( ! response . ok ) {
30+ throw new Error ( `Request failed for ${ url } : ${ response . status } ` )
31+ }
32+
33+ return response . json ( )
34+ } )
35+ }
36+
1037function shouldProcessRepo ( repo ) {
1138 if ( repo [ 'archived' ] === true ) return false ;
1239 if ( repo [ 'description' ] === null ) return false ;
@@ -64,16 +91,13 @@ function createCardBody(repo) {
6491}
6592
6693function loadCommitActivity ( repo , card_footer ) {
67- $ . ajax ( {
68- url : `${ base_url } /${ cache_repo } /github/commitActivity/${ repo [ 'name' ] } .json` ,
69- type : "GET" ,
70- dataType : "json" ,
71- success : function ( commitActivity ) {
94+ fetchJson ( `${ base_url } /${ cache_repo } /github/commitActivity/${ repo [ 'name' ] } .json` )
95+ . then ( function ( commitActivity ) {
7296 let activity_container = document . createElement ( "div" )
7397 activity_container . className = "commit-activity-graph mt-2 mb-2 mx-3"
7498 activity_container . style . cssText = "display: flex; gap: 2px; height: 32px; align-items: flex-end;"
7599
76- let maxCommits = Math . max ( ...commitActivity . map ( week => week . total ) )
100+ let maxCommits = Math . max ( ...commitActivity . map ( week => week . total ) , 0 )
77101
78102 for ( let week of commitActivity ) {
79103 let bar = document . createElement ( "div" )
@@ -94,11 +118,10 @@ function loadCommitActivity(repo, card_footer) {
94118 }
95119
96120 card_footer . insertBefore ( activity_container , card_footer . firstChild )
97- } ,
98- error : function ( ) {
121+ } )
122+ . catch ( function ( ) {
99123 console . log ( `No commit activity data available for ${ repo [ 'name' ] } ` )
100- }
101- } )
124+ } )
102125}
103126
104127function createRepoDataRow ( repo , card_footer , banner_link , card_title_link ) {
@@ -116,14 +139,14 @@ function createRepoDataRow(repo, card_footer, banner_link, card_title_link) {
116139 github_link_image . className = "fa-fw fa-brands fa-github"
117140 github_link . prepend ( github_link_image )
118141
119- $ . ajax ( {
120- url : ` ${ base_url } / ${ repo [ 'name' ] } /` ,
121- type : "GET" ,
122- success : function ( ) {
142+ fetchWithNoCache ( ` ${ base_url } / ${ repo [ 'name' ] } /` )
143+ . then ( function ( response ) {
144+ if ( ! response . ok ) return
145+
123146 banner_link . href = `${ base_url } /${ repo [ 'name' ] } /`
124147 card_title_link . href = `${ base_url } /${ repo [ 'name' ] } /`
125- } ,
126- } )
148+ } )
149+ . catch ( function ( ) { } )
127150
128151 let star_link = document . createElement ( "a" )
129152 star_link . className = "nav-link nav-link-sm project-nav-link ms-3 crowdin-ignore"
@@ -184,11 +207,8 @@ function addDocsLink(repo, readthedocs, repo_data_row) {
184207}
185208
186209function loadLanguageIcons ( repo , card_footer ) {
187- $ . ajax ( {
188- url : `${ base_url } /${ cache_repo } /github/languages/${ repo [ 'name' ] } .json` ,
189- type : "GET" ,
190- dataType : "json" ,
191- success : function ( languages ) {
210+ fetchJson ( `${ base_url } /${ cache_repo } /github/languages/${ repo [ 'name' ] } .json` )
211+ . then ( function ( languages ) {
192212 let language_data_row = document . createElement ( "div" )
193213 language_data_row . className = "card-group p-3 align-items-center"
194214 card_footer . appendChild ( language_data_row )
@@ -202,8 +222,8 @@ function loadLanguageIcons(repo, card_footer) {
202222 language_icon . title = language
203223 language_data_row . append ( language_icon )
204224 }
205- }
206- } ) ;
225+ } )
226+ . catch ( function ( ) { } )
207227}
208228
209229function buildRepoCard ( repo , readthedocs ) {
@@ -234,37 +254,32 @@ function buildRepoCard(repo, readthedocs) {
234254
235255
236256// create project cards
237- $ ( document ) . ready ( function ( ) {
238- // Set cache = false for all jquery ajax requests.
239- $ . ajaxSetup ( {
240- cache : false ,
241- } ) ;
242-
243- // get readthedocs projects
244- let readthedocs = [ ]
245- $ . ajax ( {
246- url : `${ base_url } /${ cache_repo } /readthedocs/projects.json` ,
247- type : "GET" ,
248- dataType : "json" ,
249- success : function ( data ) {
250- for ( let item in data ) {
251- readthedocs . push ( data [ item ] )
252- }
253- }
254- } ) ;
255-
256- $ . ajax ( {
257- url : `${ base_url } /${ cache_repo } /github/repos.json` ,
258- type : "GET" ,
259- dataType :"json" ,
260- success : function ( result ) {
257+ onDocumentReady ( function ( ) {
258+ container = document . getElementById ( "project-container" )
259+ if ( ! container ) return
260+
261+ let readthedocsRequest = fetchJson ( `${ base_url } /${ cache_repo } /readthedocs/projects.json` )
262+ . then ( function ( data ) {
263+ return Object . values ( data )
264+ } )
265+ . catch ( function ( error ) {
266+ console . log ( "No ReadTheDocs project data available" , error )
267+ return [ ]
268+ } )
269+
270+ let reposRequest = fetchJson ( `${ base_url } /${ cache_repo } /github/repos.json` )
271+
272+ Promise . all ( [ readthedocsRequest , reposRequest ] )
273+ . then ( function ( [ readthedocs , result ] ) {
261274 let sorted = result . sort ( globalThis . rankingSorter ( "stargazers_count" , "name" ) )
262275
263- for ( let repo in sorted ) {
264- if ( shouldProcessRepo ( sorted [ repo ] ) ) {
265- buildRepoCard ( sorted [ repo ] , readthedocs )
276+ for ( let repo of sorted ) {
277+ if ( shouldProcessRepo ( repo ) ) {
278+ buildRepoCard ( repo , readthedocs )
266279 }
267280 }
268- }
269- } ) ;
270- } ) ;
281+ } )
282+ . catch ( function ( error ) {
283+ console . error ( "Error loading project data:" , error )
284+ } )
285+ } )
0 commit comments