Fix createConnector getProvidedProps 3rd param

This commit is contained in:
Gordon
2019-02-19 15:36:29 -06:00
parent 85bde30b86
commit 57aff49fde
2 changed files with 63 additions and 3 deletions

View File

@@ -32,6 +32,14 @@ export function createInstantSearch(
*/
export function createIndex(defaultRoot: object): React.ComponentClass<any>;
export interface ConnectorSearchResults<TDoc = BasicDoc> {
results: AllSearchResults<TDoc>;
searching: boolean;
searchingForFacetValues: boolean;
isSearchStalled: boolean;
error: any;
}
export interface ConnectorDescription<TProvided, TExposed> {
displayName: string;
propTypes?: any;
@@ -50,7 +58,7 @@ export interface ConnectorDescription<TProvided, TExposed> {
this: React.Component<TExposed>,
props: TExposed,
searchState: SearchState,
searchResults: SearchResults,
searchResults: ConnectorSearchResults<any>,
metadata: any,
resultsFacetValues: any,
): TProvided;
@@ -495,7 +503,7 @@ export interface StateResultsProvided<TDoc = BasicDoc> {
*/
searchResults: SearchResults<TDoc>;
/** In case of multiple indices you can retrieve all the results */
allSearchResults: { [index: string]: SearchResults<TDoc> };
allSearchResults: AllSearchResults<TDoc>;
/** If there is a search in progress. */
searching: boolean;
/** Flag that indicates if React InstantSearch has detected that searches are stalled. */
@@ -616,6 +624,14 @@ export interface SearchResults<TDoc = BasicDoc> {
automaticRadius?: string;
}
/**
* The shape of the searchResults object when used in a multi-index search
* https://community.algolia.com/react-instantsearch/connectors/connectStateResults.html#default-props-entry-connectStateResults-searchResults
*/
export type AllSearchResults<TDoc = BasicDoc> = {
[index: string]: SearchResults<TDoc>;
} & SearchResults<TDoc>;
/**
* All the records that match the search parameters.
* Each record is augmented with a new attribute `_highlightResult` which is an

View File

@@ -22,7 +22,10 @@ import {
TranslatableProvided,
translatable,
ConnectorProvided,
StateResultsProvided
StateResultsProvided,
ConnectorSearchResults,
BasicDoc,
AllSearchResults
} from 'react-instantsearch-core';
() => {
@@ -662,3 +665,44 @@ import * as Autosuggest from 'react-autosuggest';
onSubmit={(evt) => { console.log('submitted', evt); }}
/>;
};
// can we recreate connectStateResults from source using the createConnector typedef?
() => {
function getIndexId(context: any): string {
return context && context.multiIndexContext
? context.multiIndexContext.targetedIndex
: context.ais.mainTargetedIndex;
}
function getResults<TDoc>(searchResults: { results: AllSearchResults<TDoc> }, context: any): SearchResults<TDoc> | null | undefined {
const {results} = searchResults;
if (results && !results.hits) {
return results[getIndexId(context)]
? results[getIndexId(context)]
: null;
} else {
return results ? results : null;
}
}
const csr = createConnector({
displayName: 'AlgoliaStateResults',
getProvidedProps(props, searchState, searchResults) {
const results = getResults(searchResults, this.context);
return {
searchState,
searchResults: results,
allSearchResults: searchResults.results,
searching: searchResults.searching,
isSearchStalled: searchResults.isSearchStalled,
error: searchResults.error,
searchingForFacetValues: searchResults.searchingForFacetValues,
props,
};
},
});
const asConnectStateResults: typeof connectStateResults = csr;
};