Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(refreshcache): add prop refresh to InstantSearch instance #619

Merged
merged 4 commits into from
Nov 21, 2017

Conversation

marielaures
Copy link
Member

@marielaures marielaures commented Nov 17, 2017

Summary

The goal of this PR is to make it possible to refresh an InstantSearch instance.
It is allows the user to make a new request to Algolia in case a change has happened on the index for instance instead of getting the results from the cache.

Fixes #252.

Result

Adding a prop refresh to the InstantSearch component will trigger the refresh.
It takes a Boolean as an argument.

  <InstantSearch
        appId="xyz"
        apiKey="xyz"
        indexName="indexName"
        refresh={this.state.refresh}
  >

You can see how it works on the refreshCache story in Storybook.

demo

@algobot
Copy link
Contributor

algobot commented Nov 17, 2017

Deploy preview ready!

Built with commit 9b43b48

https://deploy-preview-619--react-instantsearch.netlify.com

@algolia algolia deleted a comment from algobot Nov 17, 2017
@marielaures marielaures requested review from vvo and samouss November 17, 2017 13:35
Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

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

Super nice feature, code wise nothing to remark. Some smaller stylistic questions/suggestions though

refresh: true,
});

expect(ism.clearCache.mock.calls).toHaveLength(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

toHaveBeenCalledTimes(1)

refresh: false,
});

expect(ism.clearCache.mock.calls).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

.not. toHaveBeenCalled()

</InstantSearch>
);

expect(ism.clearCache.mock.calls).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

.not. toHaveBeenCalled()

</InstantSearch>
);

expect(ism.updateIndex.mock.calls).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

.not. toHaveBeenCalled()

indexName: 'foobar',
});

expect(ism.updateIndex.mock.calls).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

.not.toHaveBeenCalled()

indexName: 'newindexname',
});

expect(ism.updateIndex.mock.calls).toHaveLength(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

toHaveBeenCalledTimes(1)

<button onClick={this.refresh} style={buttonStyle}>
Refresh cache
</button>
<button style={divStyle} disabled>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a button?

}
}

stories.add('with a refresh button', () => <AppWithRefresh />);
Copy link
Contributor

@Haroenv Haroenv Nov 17, 2017

Choose a reason for hiding this comment

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

Can you add the minimal jsx:

<InstantSearch
  appId="latency"
  apiKey="6be0576ff61c053d5f9a3225e2a90f76"
  indexName="ikea"
  refresh={this.state.refresh}
  onSearchStateChange={() => this.setState({ refresh: false })}
>
  <SearchBox />
  <button
    onClick={() => this.setState(({ refresh }) => ({ refresh: !refresh }))}
  >
    Refresh cache
  </button>
  <p>
    Refresh is set to: <em>{displayRefresh}</em>
  </p>
  <Hits />
</InstantSearch>

Copy link
Member Author

Choose a reason for hiding this comment

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

This story contains a customHit component, that's why I didn't add the JSX story (see thread on the PR to add the JSX addon)

import { InstantSearch, SearchBox } from '../packages/react-instantsearch/dom';
import { CustomHits } from './util';

const stories = storiesOf('RefreshCache', module);
Copy link
Contributor

Choose a reason for hiding this comment

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

is that normal syntax? What is module?

Copy link
Member Author

@marielaures marielaures Nov 17, 2017

Choose a reason for hiding this comment

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

Yes, normal syntax, see https://storybook.js.org/basics/guide-react/#create-the-config-file (then section about writing stories)

@mthuret
Copy link
Contributor

mthuret commented Nov 17, 2017

Awesome @marielaures ❤️ . You should add a guide describing how to use this feature :)

Copy link
Collaborator

@samouss samouss left a comment

Choose a reason for hiding this comment

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

Great work @marielaures! 🎉

Just few comments.

@@ -170,6 +177,8 @@ InstantSearch.propTypes = {

createURL: PropTypes.func,

refresh: PropTypes.bool,
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can put this one required, since createInstantSearch will always provide the prop.

@@ -24,11 +24,16 @@ export default function createInstantSearch(defaultAlgoliaClient, root) {
searchParameters: PropTypes.object,
createURL: PropTypes.func,
searchState: PropTypes.object,
refresh: PropTypes.bool,
Copy link
Collaborator

@samouss samouss Nov 20, 2017

Choose a reason for hiding this comment

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

You can put this one required, because we will enforce the fact that the value will be true|false instead of true|false|undefined|null.


createInstantSearchManager.mockImplementation(() => ism);

const wrapper = mount(
Copy link
Collaborator

@samouss samouss Nov 20, 2017

Choose a reason for hiding this comment

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

You can use shallow instead of mount. The former will only create a tree for the root level (much better for more focused unit test), the latter will perform a full rendering of the DOM. In you test you don't need to perform the full rendering.


createInstantSearchManager.mockImplementation(() => ism);

const wrapper = mount(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as before, prefer shallow over mount.

this.state = {
refresh: false,
};
this.refresh = this.refresh.bind(this);
Copy link
Collaborator

@samouss samouss Nov 20, 2017

Choose a reason for hiding this comment

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

You can use the function property instead of bind, like you do with onSearchStateChange.

background: '#3369e7',
};

const divStyle = {
Copy link
Collaborator

@samouss samouss Nov 20, 2017

Choose a reason for hiding this comment

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

You can use inline styles like the rest of the example. Just for coherence 🙂

@vvo
Copy link
Contributor

vvo commented Nov 20, 2017

This looks very good and professional, GG @marielaures. Small comments to address from peers and that's it!

@samouss samouss merged commit 19f6de0 into master Nov 21, 2017
@samouss samouss deleted the feat/refreshcache branch November 21, 2017 21:54
@benbonnet
Copy link

benbonnet commented Nov 23, 2017

hello, sorry is it was anounced somewhere else; is this feature already available in the main package ? usable via refresh={this.state.refresh} ?

@Haroenv
Copy link
Contributor

Haroenv commented Nov 23, 2017

@deesx this isn't published yet. You could build the library yourself and use the relative version. @samouss could you publish a beta maybe?

@samouss
Copy link
Collaborator

samouss commented Nov 23, 2017

@Haroenv has right, it's not published yet. I will try to publish a beta tomorrow, if I have not time to do it ti will be next week!

@benbonnet
Copy link

@Haroenv I'll give it a try and build it. Correct me if i'm wrong, as of now it will be the only way to "trigger" a refresh of the displayed list, right ?

@Haroenv
Copy link
Contributor

Haroenv commented Nov 24, 2017

Yes, that’s the only way. It’s possible to clear the cache as described in the linked issue, but refreshing the results needs a setup like this @deesx

samouss added a commit that referenced this pull request Nov 27, 2017
<a name="4.3.0-beta.0"></a>
# [4.3.0-beta.0](v4.2.0...v4.3.0-beta.0) (2017-11-27)

### Bug Fixes

* **babelrc:** add a key for each env development, production, es ([#547](#547)) ([fa9528d](fa9528d))
* **localizecount:** allow localized string for count in MenuSelect ([#657](#657)) ([67ebd34](67ebd34))
* **react-router-example:** Properly update search query when using browser navigation ([#604](#604)) ([9ee6600](9ee6600))

### Features

* **refreshcache:** add prop refresh to InstantSearch instance ([#619](#619)) ([19f6de0](19f6de0))
@sakhmedbayev
Copy link

I do not understand, has this PR ever made into official react-intantsearch package???

@Haroenv
Copy link
Contributor

Haroenv commented Apr 4, 2018

yes @sakhmedbayev, this is supported

@sakhmedbayev
Copy link

@Haroenv thanks for the reply. In what version it is supported? v4 or v5?

@sakhmedbayev
Copy link

Also, I cannot find anywhere docs for this feature except in PR's commits.

@samouss
Copy link
Collaborator

samouss commented Apr 4, 2018

Hi @sakhmedbayev, you can find more information about this props in our documentation. We also have a guide explaining common use case that this feature solve. Here is the link of the guide. The feature is available from the version 4.3.0.

@GioLogist
Copy link

Is this working on React Native, specifically? Can't seem to get it working locally.

@samouss
Copy link
Collaborator

samouss commented Jan 10, 2019

The logic is not tied to any platform so it should work for React Native. If the issue persist please provide us a working example, it will help us to better understand the issue.

@algolia algolia locked as resolved and limited conversation to collaborators Jan 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants