Fix homepage books carousel shows english text after loading#8586
Conversation
…age-books-carousel-shows-English-text-after-loading
for more information, see https://pre-commit.ci
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #8586 +/- ##
==========================================
+ Coverage 16.51% 16.68% +0.16%
==========================================
Files 85 88 +3
Lines 4456 4681 +225
Branches 782 835 +53
==========================================
+ Hits 736 781 +45
- Misses 3227 3384 +157
- Partials 493 516 +23 ☔ View full report in Codecov by Sentry. |
jimchamp
left a comment
There was a problem hiding this comment.
Thanks @benbdeitch! You'll find some review notes below this comment.
Also, please replace the newly added spinner with localized "Loading..." strings. There is a second "Loading..." message in Carousel.js (near line 201). Please make sure that message is replaced, as well.
| $ "open": _("Read"), | ||
| $ "borrow_available": _("Borrow"), | ||
| $ "borrow_unavailable": _("Join Waitlist"), | ||
| $ "error": _("Not in Library") |
There was a problem hiding this comment.
| $ "error": _("Not in Library") | |
| $ "error": _("Not in Library"), | |
| $ "loading": _('Loading...') |
Add "loading" string.
| if (!loadMore.locked && !loadMore.allDone && isOn2ndLastPage) { | ||
| loadMore.locked = true; // lock for critical section | ||
| slick.addSlide('<div class="carousel__item carousel__loading-end">Loading...</div>'); | ||
| slick.addSlide('<object type="image/svg+xml" data="/static/images/spinner.svg"></object>'); |
There was a problem hiding this comment.
| slick.addSlide('<object type="image/svg+xml" data="/static/images/spinner.svg"></object>'); | |
| slick.addSlide('<div class="carousel__item carousel__loading-end">i18nValues['loading']</div>'); |
Use localized "loading" string here.
| }); | ||
|
|
||
| //This loads in i18n strings from a hidden input element, generated in the books/custom_carousel.html template. | ||
| const i18n_values = JSON.parse($('input[name=\'carousel-i18n-strings\']').attr('value')) |
There was a problem hiding this comment.
| const i18n_values = JSON.parse($('input[name=\'carousel-i18n-strings\']').attr('value')) | |
| const i18nValues = JSON.parse($('input[name="carousel-i18n-strings"]').attr('value')) |
Use camel case for JS variable names. Replace escaped single quotes with double quotes.
static/images/spinner.svg
Outdated
| <input type="hidden" name="carousel-i18n-strings" value="$json_encode(show_carousel_i18n_strings)"> | ||
|
|
||
|
|
||
| $if render_once('books/custom_carousel'): |
There was a problem hiding this comment.
| $if render_once('books/custom_carousel'): | |
| $if render_once('books/custom_carousel.i18n-strings'): |
Use similar naming conventions as the lists/widget.html example (found here).
…ish-text-after-loading' of github.com:benbdeitch/openlibraryfork into internetarchive#5932-Homepage-books-carousel-shows-English-text-after-loading
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
There are quite a few syntax errors that need to be corrected. Please be sure to test your code before pushing it.
Building the JS every time a change is made can be annoying and tedious. You can run docker compose exec web npm run watch to automatically build your JS changes when you save the file that you're working on. It's also very fast.
| $if render_once('books/custom_carousel.i18n-strings'):: | ||
| $:carousel_i18n_input() |
There was a problem hiding this comment.
| $if render_once('books/custom_carousel.i18n-strings'):: | |
| $:carousel_i18n_input() | |
| $if render_once('books/custom_carousel.i18n-strings'): | |
| $:carousel_i18n_input() |
Second colon causes an error.
Be sure to indent nested statements.
| loadMore.locked = true; // lock for critical section | ||
| slick.addSlide('<div class="carousel__item carousel__loading-end">Loading...</div>'); | ||
|
|
||
| slick.addSlide('<div class="carousel__item carousel__loading-end">i18nValues[loading"]</div>'); |
There was a problem hiding this comment.
| slick.addSlide('<div class="carousel__item carousel__loading-end">i18nValues[loading"]</div>'); | |
| slick.addSlide(`<div class="carousel__item carousel__loading-end">${i18nValues[loading"]}</div>`); |
Use template literals for string interpolation.
| // Remove the current slides | ||
| slick.removeSlide(totalSlides, true, true); | ||
| slick.addSlide('<div class="carousel__item carousel__loading-end">Loading...</div>'); | ||
| slick.addSlide('<div class="carousel__item carousel__loading-end">i18nValues["loading"]</div>'); |
There was a problem hiding this comment.
| slick.addSlide('<div class="carousel__item carousel__loading-end">i18nValues["loading"]</div>'); | |
| slick.addSlide(`<div class="carousel__item carousel__loading-end">${i18nValues["loading"]}</div>`); |
Use template literals for string interpolation.
| open: {cls: 'cta-btn--available', cta: i18n_values['open']}, | ||
| borrow_available: {cls: 'cta-btn--available', cta: i18n_values['borrow_available']}, | ||
| borrow_unavailable: {cls: 'cta-btn--unavailable', cta: i18n_values['borrow_unavailable']}, | ||
| error: {cls: 'cta-btn--missing', cta: i18n_values['error']}, |
There was a problem hiding this comment.
| open: {cls: 'cta-btn--available', cta: i18n_values['open']}, | |
| borrow_available: {cls: 'cta-btn--available', cta: i18n_values['borrow_available']}, | |
| borrow_unavailable: {cls: 'cta-btn--unavailable', cta: i18n_values['borrow_unavailable']}, | |
| error: {cls: 'cta-btn--missing', cta: i18n_values['error']}, | |
| open: {cls: 'cta-btn--available', cta: i18nValues['open']}, | |
| borrow_available: {cls: 'cta-btn--available', cta: i18nValues['borrow_available']}, | |
| borrow_unavailable: {cls: 'cta-btn--unavailable', cta: i18nValues['borrow_unavailable']}, | |
| error: {cls: 'cta-btn--missing', cta: i18nValues['error']}, |
i18n_values is no longer defined.
…ish-text-after-loading' of github.com:benbdeitch/openlibraryfork into internetarchive#5932-Homepage-books-carousel-shows-English-text-after-loading
for more information, see https://pre-commit.ci
Closes #5932
This is a fix for an error in the rendering of carousels. When it comes time to retrieve more books past those initially loaded, the Javascript would render the strings in English, regardless of the user's selected language. Additionally, this pull request replaces the 'loading' test with a small SVG, to render it more comprehensible to those traversing the library in other languages.
Technical
This implementation uses a hidden input element in [the custom carousel template(https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/books/custom_carousel.html) to provide the data for the Javascript to render.
Testing
To review, add a significant amount ( >18of books to any given carousel, while set to a non-english language. Currently, after a given point, the Carousels revert back to rendering elements with English strings. Now, they should remain uniform throughout.
Screenshot
Stakeholders
@jimchamp