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: fetchSearch #4008

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

fix: fetchSearch #4008

wants to merge 4 commits into from

Conversation

fstoe
Copy link

@fstoe fstoe commented Nov 25, 2024

Hey,
i tried to fix this issue.
It seems like the onSubmit sends two events. The first one being undefined and the second being the actual onSubmit one. This results in the dispatching of the action fetchSearch two times

Copy link
Member

@marlo-longley marlo-longley left a comment

Choose a reason for hiding this comment

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

Thank you for looking at this! I looked closer and noticed how the tests were failing. Here is what I came up with:

  submitSearch(event) {
    const {
      companionWindowId, fetchSearch, searchService, windowId,
    } = this.props;
    const { search } = this.state;
    if (!search) return;

    if (event && event.type === 'submit' && this.state.isSuggestionSelected) {
      event.preventDefault();
      return; // Do not trigger search again if a suggestion was selected
    }

    if (event) {
      event.preventDefault();
    }
    fetchSearch(windowId, companionWindowId, `${searchService.id}?${new URLSearchParams({ q: search })}`, search);
  }

This approach requires adding something new to the component state (isSuggestionSelected), and also handling that elsewhere in the file (reset to false at the top of handleChange) and also in selectItem:

  selectItem(_event, selectedItem, _reason) {
  if (selectedItem && getMatch(selectedItem)) {
    this.setState({ search: getMatch(selectedItem), isSuggestionSelected: true }, this.submitSearch);
  }
}

That is just some ideas, it may not make sense overall but the tests are passing with that, and there are no double searches.

@fstoe
Copy link
Author

fstoe commented Nov 26, 2024

Ah thanks for the input for fixing the tests and updated the SearchPanelControls with you're suggestions

Copy link

codecov bot commented Nov 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.18%. Comparing base (3c87395) to head (76f18b8).
Report is 97 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4008      +/-   ##
==========================================
- Coverage   92.43%   92.18%   -0.25%     
==========================================
  Files         203      204       +1     
  Lines        3581     3354     -227     
==========================================
- Hits         3310     3092     -218     
+ Misses        271      262       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@marlo-longley
Copy link
Member

@fstoe nice! This seems to fix the double request bug for me and not break anything else, but I would be curious for someone else to test it out.

marlo-longley
marlo-longley previously approved these changes Nov 26, 2024
@cbeer
Copy link
Collaborator

cbeer commented Nov 26, 2024

I'm curious if #3999 (rewriting these components to functions) fixes it or has the same problems.

@fstoe
Copy link
Author

fstoe commented Nov 28, 2024

I'm curious if #3999 (rewriting these components to functions) fixes it or has the same problems.

I just tested but unfortunately the bug persists when using the functional SearchPanelControl component.

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.

3 participants