Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix old results shown on predictive search #2249

Merged
merged 5 commits into from
Jan 27, 2023

Conversation

FCalabria
Copy link
Contributor

@FCalabria FCalabria commented Jan 23, 2023

PR Summary:

Hide stale results on the predictive search when writting a new query and refresh the "search for" button with the new term as soon as possible.

Why are these changes introduced?

Fixes #2199 and improves the user experience updating the "search for" button while the user types, instead of waiting for the predictive search query to resolve.

What approach did you take?

To fix the bug, delete the results and show the loading spinner when the new query is not an addition over the old one.

For the improvement, I used a search and replace technique modifying the html on the fly with JS. I could not figure out a way to identify the search term that wasn't just looking for the string itself (depending on the language the quote symbol and the term position changes). This can cause a problem when the user looks for exactly the label on the search button ("Search for...") which i avoided not replacing anything if there is more than one match.

Testing steps/scenarios

  • When writing a query, the search term in the button updates before the predictive search results
  • When a query is total or partially deleted and then a different term is written, the old predictive search results are not shown, just a loading state.

Demo links

Checklist

@FCalabria FCalabria force-pushed the fix-old-results-shown-on-predictive-search branch from 5e6ef87 to 9b0d555 Compare January 23, 2023 17:19
@sofiamatulis sofiamatulis self-requested a review January 25, 2023 16:58
@ludoboludo ludoboludo self-requested a review January 25, 2023 19:30
Comment on lines 115 to 118
const buttonIcon = searchForButton.innerHTML.replace(
currentButtonText,
""
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Separating from the button SVG can removed here by wrapping the translation string in a span and searching for that; I'd recommend a data-attribute of data-search-term over a class or any other selector type.

<span data-search-term>{{ 'templates.search.search_for' | t: terms: predictive_search.terms }}</span>

Note: There is a way in Liquid to put HTML in the translation string so you could wrap just the query, but I think that makes the translation string too difficult to work with.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd suggest more grabbing the icon via it's class or element name 🤔 searchForButton.querySelector('svg')
It would be easier to read.

Copy link
Contributor

@NathanPJF NathanPJF Jan 26, 2023

Choose a reason for hiding this comment

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

My point is that I don't believe this function even needs to know that there is an svg in the HTML. Right now, this function is taking the innerHTML of a <button> and changing it by concatentating a string and an svg. However, there's nothing about the svg that is going to change, so why include it? Adding a new span would give the function a target element to inspect the text on.

  updateSearchForTerm(previousTerm, newTerm) {
    const searchForText = this.querySelector("#search-for-button [data-search-term]");
    const currentText = searchForButton?.innerText;
    if (currentText) {
      if (currentText.match(new RegExp(previousTerm, "g")).length > 1) {
        // The new term matches part of the button text and not just the search term, do not replace to avoid mistakes
        return;
      }
      searchForText.innerText = currentButtonText.replace(previousTerm, newTerm);
    }
  }

@@ -263,6 +289,10 @@ class PredictiveSearch extends SearchForm {
this.resultsMaxHeight = false
this.predictiveSearchResults.removeAttribute('style');
}

clearResources() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use the existing closeResults method for this instead? Is this additional method necessary?

Copy link
Contributor

@ludoboludo ludoboludo left a comment

Choose a reason for hiding this comment

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

Looking good. Ill test a bit tomorrow some scenarios.
But no bugs/issue noticed 👍

@@ -7,7 +7,7 @@
%}
<div id="predictive-search-results" role="listbox">
{%- if first_column_results_size > 0 or predictive_search.resources.products.size > 0 -%}
<div class="predictive-search__results-groups-wrapper{% unless predictive_search.resources.products.size > 0 %} predictive-search__results-groups-wrapper--no-products{% endunless %}{% unless predictive_search.resources.queries.size > 0 or predictive_search.resources.collections.size > 0 %} predictive-search__results-groups-wrapper--no-suggestions{% endunless %}">
<div id="predictive-search__results-groups-wrapper" class="predictive-search__results-groups-wrapper{% unless predictive_search.resources.products.size > 0 %} predictive-search__results-groups-wrapper--no-products{% endunless %}{% unless predictive_search.resources.queries.size > 0 or predictive_search.resources.collections.size > 0 %} predictive-search__results-groups-wrapper--no-suggestions{% endunless %}">
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see why you'd have the __ underscores there. But since we didn't use that same pattern for predictive-search-option-search-keywords I think we should approach it the same way. With a - instead.

@FCalabria FCalabria requested a review from NathanPJF January 26, 2023 11:27
const newSearchTerm = this.getQuery();
if (!this.searchTerm || !newSearchTerm.startsWith(this.searchTerm)) {
// Hide the results when they are no longer relevant for the new search term
this.closeResults(false);
Copy link
Contributor

@ludoboludo ludoboludo Jan 26, 2023

Choose a reason for hiding this comment

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

This doesn't do what the function you had before did 🤔
Here are two video comparisons: using clearResources() & using closeResults(false)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested and it seemed to be fine, but maybe my internet was too fast to appreciate the difference. Thanks for the thorough testing!

@FCalabria FCalabria force-pushed the fix-old-results-shown-on-predictive-search branch from b860ffc to 1acec68 Compare January 27, 2023 15:56
Copy link
Contributor

@ludoboludo ludoboludo left a comment

Choose a reason for hiding this comment

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

I think it's looking good 👍
I can see how the proposed change by Nathan would make sense too 👍 About wrapping the text in a span.
Not sure if we need the data attribute since we can select the span element itself.

NathanPJF
NathanPJF previously approved these changes Jan 27, 2023
Copy link
Contributor

@NathanPJF NathanPJF left a comment

Choose a reason for hiding this comment

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

Thanks for owning this @FCalabria

It doesn't seem like the failing theme check step will be a blocker to merge. I see that the recent PRs have merged without this passing (example). But don't forget to rebase this first 🔂 🚢

@FCalabria
Copy link
Contributor Author

I think it's looking good 👍 I can see how the proposed change by Nathan would make sense too 👍 About wrapping the text in a span. Not sure if we need the data attribute since we can select the span element itself.

I decided to use the data attribute because with the span i don't need the reference to the full button anymore, so i could delete that id.

It doesn't seem like the failing theme check step will be a blocker to merge. I see that the recent PRs have merged without this passing (#2253 (comment)). But don't forget to rebase this first 🔂 🚢

Sure! I was waiting for you to finish reviewing to not mess up anything

@FCalabria FCalabria force-pushed the fix-old-results-shown-on-predictive-search branch from 87ab664 to 52f37d1 Compare January 27, 2023 16:49
Copy link
Contributor

@NathanPJF NathanPJF left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor

@ludoboludo ludoboludo left a comment

Choose a reason for hiding this comment

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

🎉

@FCalabria FCalabria merged commit d61d0b6 into main Jan 27, 2023
@FCalabria FCalabria deleted the fix-old-results-shown-on-predictive-search branch January 27, 2023 17:21
phapsidesGT pushed a commit to Gravytrain-UK/gt-shopify-dawn-theme that referenced this pull request Sep 3, 2024
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.

[Predictive search] Old results are shown when the query is cleared and rewriten
3 participants