timconspicuous.neocities.org

Have reading progress skeleton show until after both API calls

+9 -11
+9 -11
src/scripts/reading-progress.js
··· 12 12 async init() { 13 13 this.showLoading(); 14 14 try { 15 - await this.fetchReadingProgress(); 15 + const progress = await this.fetchReadingProgress(); 16 + const metadata = await this.fetchMetadata(progress.isbn13); 17 + 18 + this.currentBook = { ...progress, ...metadata }; 16 19 this.render(); 17 20 } catch (error) { 18 21 this.showError( ··· 51 54 return currentDate > latestDate ? current : latest; 52 55 }); 53 56 54 - this.currentBook = { 57 + return { 55 58 isbn13: mostRecent.value.identifiers.isbn13, 56 59 progress: mostRecent.value.bookProgress.percent, 57 60 updatedAt: mostRecent.value.bookProgress.updatedAt, 58 61 }; 59 - 60 - await this.fetchMetadata(); 61 62 } 62 63 63 - async fetchMetadata() { 64 + async fetchMetadata(isbn13) { 64 65 const response = await fetch( 65 - `https://openlibrary.org/api/books?bibkeys=ISBN:${this.currentBook.isbn13}&format=json&jscmd=data`, 66 + `https://openlibrary.org/api/books?bibkeys=ISBN:${isbn13}&format=json&jscmd=data`, 66 67 ); 67 68 68 69 if (!response.ok) { ··· 72 73 const data = await response.json(); 73 74 const metadata = Object.values(data)[0]; 74 75 75 - this.currentBook = { 76 - isbn13: this.currentBook.isbn13, 77 - progress: this.currentBook.progress, 78 - updatedAt: this.currentBook.updatedAt, 76 + return { 79 77 title: metadata.title, 80 78 author: metadata.authors[0]?.name, 81 79 coverUrl: metadata.cover?.medium, ··· 173 171 new ReadingProgressWidget(widget.id); 174 172 } 175 173 }); 176 - }); 174 + });