Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept multiple objectIDs #8

Merged
merged 6 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Then, use the `Recommendations` component:
model="related-products"
searchClient={searchClient}
indexName="YOUR_SOURCE_INDEX_NAME"
objectID={objectID}
objectIDs={[objectID]}
hitComponent={Hit}
/>

Expand All @@ -39,7 +39,7 @@ Then, use the `Recommendations` component:
model="bought-together"
searchClient={searchClient}
indexName="YOUR_SOURCE_INDEX_NAME"
objectID={objectID}
objectIDs={[objectID]}
hitComponent={Hit}
/>
```
Expand All @@ -64,11 +64,11 @@ The initialized Algolia search client.

The name of the products index.

### `objectID`
### `objectIDs`

> `string` | **required**
> `string[` | **required**
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved

The objectID of the product to get recommendations from
An array of object IDs of the products to get recommendations from.
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved

### `hitComponent`

Expand Down
6 changes: 3 additions & 3 deletions examples/demo/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function App() {
<FrequentlyBoughtTogether
searchClient={searchClient}
indexName={indexName}
objectID={selectedProduct.objectID}
objectIDs={[selectedProduct.objectID]}
hitComponent={({ hit }) => <Hit hit={hit} insights={insights} />}
maxRecommendations={3}
searchParameters={{
Expand All @@ -130,7 +130,7 @@ function App() {
<RelatedProductsSlider
searchClient={searchClient}
indexName={indexName}
objectID={selectedProduct.objectID}
objectIDs={[selectedProduct.objectID]}
hitComponent={({ hit }) => <Hit hit={hit} insights={insights} />}
maxRecommendations={10}
translations={{
Expand All @@ -151,7 +151,7 @@ function App() {
<RelatedProducts
searchClient={searchClient}
indexName={indexName}
objectID={selectedProduct.objectID}
objectIDs={[selectedProduct.objectID]}
hitComponent={({ hit }) => <Hit hit={hit} insights={insights} />}
maxRecommendations={10}
translations={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function FrequentlyBoughtTogether<TObject extends ProductBaseRecord>(
FrequentlyBoughtTogether.propTypes = {
searchClient: PropTypes.object.isRequired,
indexName: PropTypes.string.isRequired,
objectID: PropTypes.string.isRequired,
objectIDs: PropTypes.arrayOf(PropTypes.string).isRequired,
hitComponent: PropTypes.elementType.isRequired,

maxRecommendations: PropTypes.number,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-recommendations/src/Recommendations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Recommendations.propTypes = {
model: PropTypes.string.isRequired,
searchClient: PropTypes.object.isRequired,
indexName: PropTypes.string.isRequired,
objectID: PropTypes.string.isRequired,
objectIDs: PropTypes.arrayOf(PropTypes.string).isRequired,
hitComponent: PropTypes.elementType.isRequired,

fallbackFilters: PropTypes.arrayOf(
Expand Down
2 changes: 1 addition & 1 deletion packages/react-recommendations/src/RelatedProducts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function RelatedProducts<TObject extends ProductBaseRecord>(
RelatedProducts.propTypes = {
searchClient: PropTypes.object.isRequired,
indexName: PropTypes.string.isRequired,
objectID: PropTypes.string.isRequired,
objectIDs: PropTypes.arrayOf(PropTypes.string).isRequired,
hitComponent: PropTypes.elementType.isRequired,

fallbackFilters: PropTypes.arrayOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export function RelatedProductsSlider<TObject extends ProductBaseRecord>(
RelatedProductsSlider.propTypes = {
searchClient: PropTypes.object.isRequired,
indexName: PropTypes.string.isRequired,
objectID: PropTypes.string.isRequired,
objectIDs: PropTypes.arrayOf(PropTypes.string).isRequired,
hitComponent: PropTypes.elementType.isRequired,

fallbackFilters: PropTypes.arrayOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, render, waitFor } from '@testing-library/react';
import React from 'react';

import { createSingleSearchResponse } from '../../../../test-utils/createApiResponse';
import { createMultiSearchResponse } from '../../../../test-utils/createApiResponse';
import { createSearchClient } from '../../../../test-utils/createSearchClient';
import { Recommendations } from '../Recommendations';

Expand Down Expand Up @@ -34,17 +34,17 @@ function Hit({ hit }) {

function createRecommendationsClient() {
const index = {
getObject: jest.fn(() => Promise.resolve(hit)),
getObjects: jest.fn(() => Promise.resolve({ results: [hit] })),
};
const searchClient = createSearchClient({
initIndex: jest.fn(() => index),
search: jest.fn(() =>
Promise.resolve(
createSingleSearchResponse({
createMultiSearchResponse({
hits: [hit],
})
)
),
};
const searchClient = createSearchClient({
initIndex: jest.fn(() => index),
});

return {
Expand All @@ -63,7 +63,7 @@ describe('Recommendations', () => {
model="related-products"
searchClient={searchClient}
indexName="indexName"
objectID="objectID"
objectIDs={['objectID']}
hitComponent={Hit}
/>
);
Expand All @@ -73,22 +73,27 @@ describe('Recommendations', () => {
expect(searchClient.initIndex).toHaveBeenCalledWith(
'ai_recommend_related-products_indexName'
);
expect(index.getObject).toHaveBeenCalledTimes(1);
expect(index.getObject).toHaveBeenCalledWith('objectID');
expect(index.getObjects).toHaveBeenCalledTimes(1);
expect(index.getObjects).toHaveBeenCalledWith(['objectID']);

await waitFor(() => {
expect(index.search).toHaveBeenCalledTimes(1);
expect(index.search).toHaveBeenCalledWith('', {
analytics: false,
analyticsTags: ['alg-recommend_related-products'],
clickAnalytics: false,
enableABTest: false,
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
ruleContexts: ['alg-recommend_related-products_objectID'],
typoTolerance: false,
});
expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
{
indexName: 'indexName',
params: {
analytics: false,
analyticsTags: ['alg-recommend_related-products'],
clickAnalytics: false,
enableABTest: false,
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
ruleContexts: ['alg-recommend_related-products_objectID'],
typoTolerance: false,
},
},
]);
});
});

Expand All @@ -101,7 +106,7 @@ describe('Recommendations', () => {
model="bought-together"
searchClient={searchClient}
indexName="indexName"
objectID="objectID"
objectIDs={['objectID']}
hitComponent={Hit}
/>
);
Expand All @@ -111,22 +116,27 @@ describe('Recommendations', () => {
expect(searchClient.initIndex).toHaveBeenCalledWith(
'ai_recommend_bought-together_indexName'
);
expect(index.getObject).toHaveBeenCalledTimes(1);
expect(index.getObject).toHaveBeenCalledWith('objectID');
expect(index.getObjects).toHaveBeenCalledTimes(1);
expect(index.getObjects).toHaveBeenCalledWith(['objectID']);

await waitFor(() => {
expect(index.search).toHaveBeenCalledTimes(1);
expect(index.search).toHaveBeenCalledWith('', {
analytics: false,
analyticsTags: ['alg-recommend_bought-together'],
clickAnalytics: false,
enableABTest: false,
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
ruleContexts: ['alg-recommend_bought-together_objectID'],
typoTolerance: false,
});
expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
{
indexName: 'indexName',
params: {
analytics: false,
analyticsTags: ['alg-recommend_bought-together'],
clickAnalytics: false,
enableABTest: false,
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
ruleContexts: ['alg-recommend_bought-together_objectID'],
typoTolerance: false,
},
},
]);
});
});
});
6 changes: 6 additions & 0 deletions packages/react-recommendations/src/types/ProductRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ProductRecord<TObject> = TObject & {
__indexName: string;
__queryID: string | undefined;
__position: number;
__recommendScore: number | null;
};
3 changes: 2 additions & 1 deletion packages/react-recommendations/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './UseRecommendationsInternalProps';
export * from './ProductBaseRecord';
export * from './ProductRecord';
export * from './RecommendationModel';
export * from './RecommendationRecord';
export * from './RecommendationTranslations';
export * from './UseRecommendationsInternalProps';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

export type UseFrequentlyBoughtTogetherProps = {
indexName: string;
objectID: string;
objectIDs: string[];
searchClient: SearchClient;

maxRecommendations?: number;
Expand Down
Loading