X Tutup
Skip to content

Fix homepage books carousel shows english text after loading#8586

Merged
jimchamp merged 13 commits intointernetarchive:masterfrom
benbdeitch:#5932-Homepage-books-carousel-shows-English-text-after-loading
Dec 14, 2023
Merged

Fix homepage books carousel shows english text after loading#8586
jimchamp merged 13 commits intointernetarchive:masterfrom
benbdeitch:#5932-Homepage-books-carousel-shows-English-text-after-loading

Conversation

@benbdeitch
Copy link
Collaborator

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

image

Stakeholders

@jimchamp

@codecov-commenter
Copy link

codecov-commenter commented Dec 4, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (308a35d) 16.51% compared to head (3ee404b) 16.68%.
Report is 177 commits behind head on master.

Files Patch % Lines
...ibrary/plugins/openlibrary/js/carousel/Carousel.js 0.00% 3 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@jimchamp jimchamp changed the title #5932 homepage books carousel shows english text after loading homepage books carousel shows english text after loading Dec 5, 2023
@jimchamp jimchamp changed the title homepage books carousel shows english text after loading Fix homepage books carousel shows english text after loading Dec 5, 2023
Copy link
Collaborator

@jimchamp jimchamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$ "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>');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this spinner.

<input type="hidden" name="carousel-i18n-strings" value="$json_encode(show_carousel_i18n_strings)">


$if render_once('books/custom_carousel'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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).

@jimchamp jimchamp added the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Dec 9, 2023
benbdeitch and others added 3 commits December 10, 2023 16:24
…ish-text-after-loading' of github.com:benbdeitch/openlibraryfork into internetarchive#5932-Homepage-books-carousel-shows-English-text-after-loading
Copy link
Collaborator

@jimchamp jimchamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +22 to +23
$if render_once('books/custom_carousel.i18n-strings')::
$:carousel_i18n_input()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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>');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines +79 to +82
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']},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

benbdeitch and others added 3 commits December 13, 2023 18:57
…ish-text-after-loading' of github.com:benbdeitch/openlibraryfork into internetarchive#5932-Homepage-books-carousel-shows-English-text-after-loading
@jimchamp jimchamp removed the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Dec 14, 2023
Copy link
Collaborator

@jimchamp jimchamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @benbdeitch

@jimchamp jimchamp merged commit 9e76ff8 into internetarchive:master Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Homepage books carousel shows english text after loading

3 participants

X Tutup