This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Conditional): add connectStateResults connector (#357)
* feat(Conditional): add connectStateResults connector * feat(Conditiona): add warning and examples
- Loading branch information
Showing
8 changed files
with
373 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/react-instantsearch/src/connectors/connectStateResults.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import createConnector from '../core/createConnector'; | ||
import { getResults } from '../core/indexUtils'; | ||
|
||
/** | ||
* The `connectStateResults` connector provides a way to access the `searchState` and the `searchResults` | ||
* of InstantSearch. | ||
* For instance this connector allows you to create results/noResults or query/noQuery pages. | ||
* @name connectStateResults | ||
* @kind connector | ||
* @providedPropType {object} searchState - The search state of the instant search component. <br/><br/> See: [Search state structure](https://community.algolia.com/react-instantsearch/guide/Search_state.html) | ||
* @providedPropType {object} searchResults - The search results. <br/><br/> In case of multiple indices: if used under `<Index>`, results will be those of the corresponding index otherwise it'll be those of the root index See: [Search results structure](https://community.algolia.com/algoliasearch-helper-js/reference.html#searchresults) | ||
* @providedPropType {object} allSearchResults - In case of multiple indices you can retrieve all the results | ||
* @providedPropType {string} error - If the search failed, the error will be logged here. | ||
* @providedPropType {boolean} searching - If there is a search in progress. | ||
* @providedPropType {boolean} searchingForFacetValues - If there is a search in a list in progress. | ||
* @example | ||
* import React from 'react'; | ||
* | ||
* import { InstantSearch, Hits } from 'react-instantsearch/dom'; | ||
* import { connectStateResults } from 'react-instantsearch/connectors'; | ||
* | ||
* const Content = connectStateResults( | ||
* ({ searchState, searchResults }) => | ||
* searchResults && searchResults.nbHits !== 0 | ||
* ? <Hits/> | ||
* : <div> | ||
* No results has been found for {searchState.query} | ||
* </div> | ||
* ); | ||
* return ( | ||
* <WrapWithHits linkedStoryGroup="Conditional"> | ||
* <Content /> | ||
* </WrapWithHits> | ||
* ); | ||
* | ||
* export default function App() { | ||
* return ( | ||
* <InstantSearch | ||
* appId="latency" | ||
* apiKey="6be0576ff61c053d5f9a3225e2a90f76" | ||
* indexName="ikea" | ||
* > | ||
* <Content /> | ||
* </InstantSearch> | ||
* ); | ||
* } | ||
*/ | ||
export default createConnector({ | ||
displayName: 'AlgoliaStateResults', | ||
|
||
getProvidedProps(props, searchState, searchResults) { | ||
const results = getResults(searchResults, this.context); | ||
return { | ||
searchState, | ||
searchResults: results, | ||
allSearchResults: searchResults.results, | ||
searching: searchResults.searching, | ||
error: searchResults.error, | ||
searchingForFacetValues: searchResults.searchingForFacetValues, | ||
}; | ||
}, | ||
}); |
69 changes: 69 additions & 0 deletions
69
packages/react-instantsearch/src/connectors/connectStateResults.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-env jest, jasmine */ | ||
|
||
import connect from './connectStateResults'; | ||
jest.mock('../core/createConnector'); | ||
|
||
let props; | ||
|
||
describe('connectStateResults', () => { | ||
describe('single index', () => { | ||
const context = { context: { ais: { mainTargetedIndex: 'index' } } }; | ||
const getProvidedProps = connect.getProvidedProps.bind(context); | ||
it('provides the correct props to the component', () => { | ||
const searchState = { state: 'state' }; | ||
const error = 'error'; | ||
const searching = true; | ||
const searchingForFacetValues = true; | ||
const searchResults = { | ||
results: { nbHits: 25, hits: [] }, | ||
error, | ||
searching, | ||
searchingForFacetValues, | ||
}; | ||
|
||
props = getProvidedProps({}, searchState, searchResults); | ||
expect(props).toEqual({ | ||
searchState, | ||
searchResults: searchResults.results, | ||
allSearchResults: searchResults.results, | ||
error, | ||
searching, | ||
searchingForFacetValues, | ||
}); | ||
}); | ||
}); | ||
describe('multi index', () => { | ||
const context = { | ||
context: { | ||
ais: { mainTargetedIndex: 'first' }, | ||
multiIndexContext: { targetedIndex: 'first' }, | ||
}, | ||
}; | ||
const getProvidedProps = connect.getProvidedProps.bind(context); | ||
it('provides the correct props to the component', () => { | ||
const searchState = { state: 'state' }; | ||
const error = 'error'; | ||
const searching = true; | ||
const searchingForFacetValues = true; | ||
const searchResults = { | ||
results: { | ||
first: { nbHits: 25, hits: [] }, | ||
second: { nbHits: 25, hits: [] }, | ||
}, | ||
error, | ||
searching, | ||
searchingForFacetValues, | ||
}; | ||
|
||
props = getProvidedProps({}, searchState, searchResults); | ||
expect(props).toEqual({ | ||
searchState, | ||
searchResults: searchResults.results.first, | ||
allSearchResults: searchResults.results, | ||
error, | ||
searching, | ||
searchingForFacetValues, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.