-
Notifications
You must be signed in to change notification settings - Fork 386
feat(search-client): Add support for Custom Search Clients #1216
Changes from 5 commits
5053eb8
4587eb1
866267b
8d50734
6b70d2b
4ddc07b
2ff671a
ef6696b
497bfca
c95e0b8
0d37199
0806a6e
4b095b8
0493b8e
4e33880
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,6 +29,7 @@ function validateNextProps(props, nextProps) { | |||||||||||
* @propType {string} indexName - Main index in which to search. | ||||||||||||
* @propType {boolean} [refresh=false] - Flag to activate when the cache needs to be cleared so that the front-end is updated when a change occurs in the index. | ||||||||||||
* @propType {object} [algoliaClient] - Provide a custom Algolia client instead of the internal one. | ||||||||||||
* @propType {object} [searchClient] - Provide a custom search client. | ||||||||||||
* @propType {func} [onSearchStateChange] - Function to be called everytime a new search is done. Useful for [URL Routing](guide/Routing.html). | ||||||||||||
* @propType {object} [searchState] - Object to inject some search state. Switches the InstantSearch component in controlled mode. Useful for [URL Routing](guide/Routing.html). | ||||||||||||
* @propType {func} [createURL] - Function to call when creating links, useful for [URL Routing](guide/Routing.html). | ||||||||||||
|
@@ -59,7 +60,7 @@ class InstantSearch extends Component { | |||||||||||
|
||||||||||||
this.aisManager = createInstantSearchManager({ | ||||||||||||
indexName: props.indexName, | ||||||||||||
algoliaClient: props.algoliaClient, | ||||||||||||
searchClient: props.searchClient || props.algoliaClient, | ||||||||||||
initialState, | ||||||||||||
resultsState: props.resultsState, | ||||||||||||
stalledSearchDelay: props.stalledSearchDelay, | ||||||||||||
|
@@ -83,6 +84,10 @@ class InstantSearch extends Component { | |||||||||||
this.aisManager.updateClient(nextProps.algoliaClient); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (this.props.searchClient !== nextProps.searchClient) { | ||||||||||||
this.aisManager.updateClient(nextProps.searchClient); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (this.isControlled) { | ||||||||||||
this.aisManager.onExternalStateUpdate(nextProps.searchState); | ||||||||||||
} | ||||||||||||
|
@@ -177,7 +182,9 @@ InstantSearch.propTypes = { | |||||||||||
// @TODO: These props are currently constant. | ||||||||||||
indexName: PropTypes.string.isRequired, | ||||||||||||
|
||||||||||||
algoliaClient: PropTypes.object.isRequired, | ||||||||||||
algoliaClient: PropTypes.object, | ||||||||||||
|
||||||||||||
searchClient: PropTypes.object.isRequired, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to duplicate the props, we already do the logic to chose the correct implementation on the previous layer. But we can still rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We somehow need to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but this component is not part of the public API. The part that is exposed is react-instantsearch/packages/react-instantsearch/dom.js Lines 5 to 9 in 388f777
|
||||||||||||
|
||||||||||||
createURL: PropTypes.func, | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,6 +17,15 @@ Object { | |||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"searchClient": Object { | ||||||||||||||||||||||||||
"addAlgoliaAgent": [MockFunction] { | ||||||||||||||||||||||||||
"calls": Array [ | ||||||||||||||||||||||||||
Array [ | ||||||||||||||||||||||||||
"react-instantsearch 5.0.3", | ||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the previous implementation we don't pass the react-instantsearch/packages/react-instantsearch/src/core/createInstantSearch.test.js Lines 22 to 33 in 388f777
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get it! |
||||||||||||||||||||||||||
"searchState": undefined, | ||||||||||||||||||||||||||
"stalledSearchDelay": 200, | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
@@ -34,6 +43,15 @@ Object { | |||||||||||||||||||||||||
"root": Object { | ||||||||||||||||||||||||||
"Root": "div", | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"searchClient": Object { | ||||||||||||||||||||||||||
"addAlgoliaAgent": [MockFunction] { | ||||||||||||||||||||||||||
"calls": Array [ | ||||||||||||||||||||||||||
Array [ | ||||||||||||||||||||||||||
"react-instantsearch 5.0.3", | ||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"searchState": undefined, | ||||||||||||||||||||||||||
"stalledSearchDelay": 200, | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ export default function createInstantSearch(defaultAlgoliaClient, root) { | |
return class CreateInstantSearch extends Component { | ||
static propTypes = { | ||
algoliaClient: PropTypes.object, | ||
searchClient: PropTypes.object, | ||
appId: PropTypes.string, | ||
apiKey: PropTypes.string, | ||
children: PropTypes.oneOfType([ | ||
|
@@ -42,24 +43,49 @@ export default function createInstantSearch(defaultAlgoliaClient, root) { | |
constructor(...args) { | ||
super(...args); | ||
|
||
if (this.props.searchClient) { | ||
if (this.props.appId || this.props.apiKey || this.props.algoliaClient) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cover those cases with tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I messed up with my rebase. Will add it back! |
||
// eslint-disable-next-line no-console | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this comment, we don't use the |
||
throw new Error( | ||
'react-instantsearch:: `searchClient` cannot be used with `appId`, `apiKey` or `algoliaClient`.' | ||
); | ||
} | ||
} | ||
|
||
if (this.props.algoliaClient) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'`algoliaClient` option was renamed `searchClient`. Please use this new option before the next major version.' | ||
); | ||
} | ||
|
||
this.client = | ||
this.props.searchClient || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cover this case with a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
this.props.algoliaClient || | ||
defaultAlgoliaClient(this.props.appId, this.props.apiKey); | ||
|
||
this.client.addAlgoliaAgent(`react-instantsearch ${version}`); | ||
if (typeof this.client.addAlgoliaAgent === 'function') { | ||
this.client.addAlgoliaAgent(`react-instantsearch ${version}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cover this case with a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
const props = this.props; | ||
if (nextProps.algoliaClient) { | ||
|
||
if (nextProps.searchClient) { | ||
this.client = nextProps.searchClient; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cover this case with a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! (I messed up with my rebase) |
||
} else if (nextProps.algoliaClient) { | ||
this.client = nextProps.algoliaClient; | ||
} else if ( | ||
props.appId !== nextProps.appId || | ||
props.apiKey !== nextProps.apiKey | ||
) { | ||
this.client = defaultAlgoliaClient(nextProps.appId, nextProps.apiKey); | ||
} | ||
this.client.addAlgoliaAgent(`react-instantsearch ${version}`); | ||
|
||
if (typeof this.client.addAlgoliaAgent === 'function') { | ||
this.client.addAlgoliaAgent(`react-instantsearch ${version}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cover this case with a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} | ||
|
||
render() { | ||
|
@@ -71,6 +97,7 @@ export default function createInstantSearch(defaultAlgoliaClient, root) { | |
onSearchStateChange={this.props.onSearchStateChange} | ||
onSearchParameters={this.props.onSearchParameters} | ||
root={this.props.root} | ||
searchClient={this.client} | ||
algoliaClient={this.client} | ||
refresh={this.props.refresh} | ||
resultsState={this.props.resultsState} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this prop from the documentation since we deprecate the option. What is the strategy in the others flavours?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe description can have “deprecated” in it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we deprecate
algoliaClient
before removing it in the next major version. I'll add a mention.