-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recommend): introduce
connectRelatedProducts
connector (#6142)
Co-authored-by: Dhaya <154633+dhayab@users.noreply.github.com> Co-authored-by: Raed <raed.chammam@algolia.com> Co-authored-by: Aymeric Giraudet <aymeric.giraudet@algolia.com>
- Loading branch information
1 parent
75a4b49
commit 89c4409
Showing
13 changed files
with
510 additions
and
11 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
156 changes: 156 additions & 0 deletions
156
packages/instantsearch.js/src/connectors/related-products/connectRelatedProducts.ts
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,156 @@ | ||
import { | ||
createDocumentationMessageGenerator, | ||
checkRendering, | ||
noop, | ||
} from '../../lib/utils'; | ||
|
||
import type { Connector, TransformItems, Hit, BaseHit } from '../../types'; | ||
import type { | ||
PlainSearchParameters, | ||
RecommendResultItem, | ||
} from 'algoliasearch-helper'; | ||
|
||
const withUsage = createDocumentationMessageGenerator({ | ||
name: 'related-products', | ||
connector: true, | ||
}); | ||
|
||
export type RelatedProductsRenderState<THit extends BaseHit = BaseHit> = { | ||
/** | ||
* The matched recommendations from the Algolia API. | ||
*/ | ||
recommendations: Array<Hit<THit>>; | ||
}; | ||
|
||
export type RelatedProductsConnectorParams<THit extends BaseHit = BaseHit> = { | ||
/** | ||
* The `objectIDs` of the items to get related products from. | ||
*/ | ||
objectIDs: string[]; | ||
/** | ||
* The number of recommendations to retrieve. | ||
*/ | ||
maxRecommendations?: number; | ||
/** | ||
* The threshold for the recommendations confidence score (between 0 and 100). | ||
*/ | ||
threshold?: number; | ||
/** | ||
* List of search parameters to send. | ||
*/ | ||
fallbackParameters?: Omit< | ||
PlainSearchParameters, | ||
'page' | 'hitsPerPage' | 'offset' | 'length' | ||
>; | ||
/** | ||
* List of search parameters to send. | ||
*/ | ||
queryParameters?: Omit< | ||
PlainSearchParameters, | ||
'page' | 'hitsPerPage' | 'offset' | 'length' | ||
>; | ||
/** | ||
* Function to transform the items passed to the templates. | ||
*/ | ||
transformItems?: TransformItems<Hit<THit>, { results: RecommendResultItem }>; | ||
}; | ||
|
||
export type RelatedProductsWidgetDescription<THit extends BaseHit = BaseHit> = { | ||
$$type: 'ais.relatedProducts'; | ||
renderState: RelatedProductsRenderState<THit>; | ||
}; | ||
|
||
export type RelatedProductsConnector<THit extends BaseHit = BaseHit> = | ||
Connector< | ||
RelatedProductsWidgetDescription<THit>, | ||
RelatedProductsConnectorParams<THit> | ||
>; | ||
|
||
const connectRelatedProducts: RelatedProductsConnector = | ||
function connectRelatedProducts(renderFn, unmountFn = noop) { | ||
checkRendering(renderFn, withUsage()); | ||
|
||
return function relatedProducts(widgetParams) { | ||
const { | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems = ((items) => items) as NonNullable< | ||
RelatedProductsConnectorParams['transformItems'] | ||
>, | ||
} = widgetParams || {}; | ||
|
||
if (!objectIDs || objectIDs.length === 0) { | ||
throw new Error(withUsage('The `objectIDs` option is required.')); | ||
} | ||
|
||
return { | ||
dependsOn: 'recommend', | ||
$$type: 'ais.relatedProducts', | ||
|
||
init(initOptions) { | ||
renderFn( | ||
{ | ||
...this.getWidgetRenderState(initOptions), | ||
instantSearchInstance: initOptions.instantSearchInstance, | ||
}, | ||
true | ||
); | ||
}, | ||
|
||
render(renderOptions) { | ||
const renderState = this.getWidgetRenderState(renderOptions); | ||
|
||
renderFn( | ||
{ | ||
...renderState, | ||
instantSearchInstance: renderOptions.instantSearchInstance, | ||
}, | ||
false | ||
); | ||
}, | ||
|
||
getRenderState(renderState) { | ||
return renderState; | ||
}, | ||
|
||
getWidgetRenderState({ results }) { | ||
if (results === null || results === undefined) { | ||
return { recommendations: [], widgetParams }; | ||
} | ||
|
||
return { | ||
recommendations: transformItems(results.hits, { | ||
results: results as RecommendResultItem, | ||
}), | ||
widgetParams, | ||
}; | ||
}, | ||
|
||
dispose({ state }) { | ||
unmountFn(); | ||
|
||
return state; | ||
}, | ||
|
||
getWidgetParameters(state) { | ||
// We only use the first `objectID` to get the recommendations for | ||
// until we implement support for multiple `objectIDs` in the helper. | ||
const objectID = objectIDs[0]; | ||
|
||
return state.addRelatedProducts({ | ||
objectID, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
$$id: this.$$id!, | ||
}); | ||
}, | ||
}; | ||
}; | ||
}; | ||
|
||
export default connectRelatedProducts; |
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,11 @@ | ||
import type { Widget } from '../../types'; | ||
|
||
let id = 0; | ||
|
||
export function addWidgetId(widget: Widget) { | ||
if (widget.dependsOn !== 'recommend') { | ||
return; | ||
} | ||
|
||
widget.$$id = id++; | ||
} |
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
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
Oops, something went wrong.