-
Notifications
You must be signed in to change notification settings - Fork 386
feat(search-client): Add support for Custom Search Clients #1216
Changes from all 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 |
---|---|---|
|
@@ -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,48 @@ export default function createInstantSearch(defaultAlgoliaClient, root) { | |
constructor(...args) { | ||
super(...args); | ||
|
||
if (this.props.searchClient) { | ||
if (this.props.appId || this.props.apiKey || this.props.algoliaClient) { | ||
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 +96,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 cover those cases with tests.
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.
I messed up with my rebase. Will add it back!