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(status): send render event when loading starts #5127

Merged
merged 2 commits into from
Oct 3, 2022

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Sep 30, 2022

Without this it's impossible to use loading for anything. I opted to only emit the event, as calling render on the widgets would require changes in all the tests.

If we render (fully), you still don't get updated of loading initially because render only gets called when there are results:

https://github.com/algolia/instantsearch.js/blob/4d43bd8292446101742a10d1da9f042f780d8d45/src/widgets/index/index.ts#L573-L575

To fix all of these inconsistencies, we should join init and render in a single function (always calling render, even if it's the first render, also when there's no results), but this would be a significant breaking change, so shouldn't be done lightly.

Without this PR, useInstantSearch in React InstantSearch Hooks doesn't get updated when the status changes to loading, as that doesn't emit render (yet)

Without this it's impossible to use `loading` for anything. I opted to only emit the event, as calling render on the widgets would  require changes in all the tests.

If we render (fully), you still don't get updated of `loading` initially because `render` only gets called when there are results:

https://github.com/algolia/instantsearch.js/blob/4d43bd8292446101742a10d1da9f042f780d8d45/src/widgets/index/index.ts#L573-L575

To fix all of these inconsistencies, we should join `init` and `render` in a single function (always calling render, even if it's the first render, also when there's no results), but this would be a significant breaking change, so shouldn't be done lightly
@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 30, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9c4c8e6:

Sandbox Source
InstantSearch.js Configuration

Haroenv added a commit to algolia/react-instantsearch that referenced this pull request Sep 30, 2022
Comment on lines +1299 to +1301
expect(renderSpy).toHaveBeenCalledTimes(2);
expect(renderSpy).toHaveBeenNthCalledWith(1, { widgetRenderTimes: 0 });
expect(renderSpy).toHaveBeenNthCalledWith(2, { widgetRenderTimes: 1 });
Copy link
Member

Choose a reason for hiding this comment

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

This isn't obvious what the behavior is when just reading those lines, can you add comments?

@@ -185,7 +185,7 @@ describe('status', () => {
});

test('lets users render on error with the `render` event', async () => {
expect.assertions(4);
// expect.assertions(4);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this commented?

@Haroenv Haroenv enabled auto-merge (squash) October 3, 2022 08:20
@Haroenv Haroenv merged commit 8cc18af into master Oct 3, 2022
@Haroenv Haroenv deleted the fix/loading-event-detectable branch October 3, 2022 08:29
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request Oct 3, 2022
<!--
  Thanks for submitting a pull request!
Please provide enough information so that others can review your pull
request.
-->

**Summary**

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

Introduces `status` and `error` in the response of `useInstantSearch`,
as well as a new option on `useInstantSearch` to indicate errors in the
search lifecycle should be caught.

built on top of algolia/instantsearch#5127


**Result**

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.
-->


```jsx
function Status() {
  const { status, error } = useInstantSearch({ catchError: true });

  return (
    <>
      <span>Search status: {status}</span>
      {error && <span>a search error occurred: {error.message}</span>}
    </>
  );
}
```


FX-1769
Haroenv added a commit that referenced this pull request Jan 4, 2023
…earch#3645)

<!--
  Thanks for submitting a pull request!
Please provide enough information so that others can review your pull
request.
-->

**Summary**

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

Introduces `status` and `error` in the response of `useInstantSearch`,
as well as a new option on `useInstantSearch` to indicate errors in the
search lifecycle should be caught.

built on top of #5127


**Result**

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.
-->


```jsx
function Status() {
  const { status, error } = useInstantSearch({ catchError: true });

  return (
    <>
      <span>Search status: {status}</span>
      {error && <span>a search error occurred: {error.message}</span>}
    </>
  );
}
```


FX-1769
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.

2 participants