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

fix(recommend): fix sendEvent function passing to Recommend components #6533

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function createCarouselComponent({ createElement, Fragment }: Renderer) {
sendEvent('click:internal', item, 'Item Clicked');
}}
>
<ItemComponent item={item} />
<ItemComponent item={item} sendEvent={sendEvent} />
</li>
))}
</ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('FrequentlyBoughtTogether', () => {
item={item}
onClick={jest.fn()}
onAuxClick={jest.fn()}
sendEvent={jest.fn()}
/>
</li>
))}
Expand Down Expand Up @@ -237,6 +238,12 @@ describe('FrequentlyBoughtTogether', () => {
);

expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenNthCalledWith(
1,
'click:internal',
items[0],
'Item Clicked'
);
});

test('accepts custom title translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('LookingSimilar', () => {
item={item}
onClick={jest.fn()}
onAuxClick={jest.fn()}
sendEvent={jest.fn()}
/>
</li>
))}
Expand Down Expand Up @@ -233,6 +234,12 @@ describe('LookingSimilar', () => {
userEvent.click(container.querySelectorAll('.ais-LookingSimilar-item')[0]!);

expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenNthCalledWith(
1,
'click:internal',
items[0],
'Item Clicked'
);
});

test('accepts custom title translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('RelatedProducts', () => {
item={item}
onClick={jest.fn()}
onAuxClick={jest.fn()}
sendEvent={jest.fn()}
/>
</li>
))}
Expand Down Expand Up @@ -235,6 +236,12 @@ describe('RelatedProducts', () => {
);

expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenNthCalledWith(
1,
'click:internal',
items[0],
'Item Clicked'
);
});

test('accepts custom title translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('TrendingItems', () => {
item={item}
onClick={jest.fn()}
onAuxClick={jest.fn()}
sendEvent={jest.fn()}
/>
</li>
))}
Expand Down Expand Up @@ -233,6 +234,12 @@ describe('TrendingItems', () => {
userEvent.click(container.querySelectorAll('.ais-TrendingItems-item')[0]!);

expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenNthCalledWith(
1,
'click:internal',
items[0],
'Item Clicked'
);
});

test('accepts custom title translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createListComponent({ createElement }: Renderer) {
sendEvent('click:internal', item, 'Item Clicked');
}}
>
<ItemComponent item={item} />
<ItemComponent item={item} sendEvent={sendEvent} />
</li>
))}
</ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type RecordWithObjectID<TObject = Record<string, unknown>> = TObject & {

export type RecommendItemComponentProps<TObject> = {
item: TObject;
sendEvent: SendEventForHits;
onClick?: () => void;
onAuxClick?: () => void;
};
Expand Down
12 changes: 11 additions & 1 deletion packages/instantsearch-ui-components/src/types/shared.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
// Should be imported from a shared package in the future
export type SendEventForHits = (...props: unknown[]) => void;

import type { RecordWithObjectID } from './Recommend';

type BuiltInSendEventForHits = (
eventType: string,
hits: RecordWithObjectID<any> | Array<RecordWithObjectID<any>>,
eventName?: string,
additionalData?: Record<string, any>
) => void;
type CustomSendEventForHits = (customPayload: any) => void;
export type SendEventForHits = BuiltInSendEventForHits & CustomSendEventForHits;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
BaseHit,
RecommendResponse,
Hit,
TemplateWithBindEvent,
} from '../../types';
import type {
RecommendClassNames,
Expand Down Expand Up @@ -84,21 +85,21 @@ const renderer =
: undefined
) as FrequentlyBoughtTogetherUiProps<AlgoliaHit>['headerComponent'];

const itemComponent = (
const itemComponent: FrequentlyBoughtTogetherUiProps<AlgoliaHit>['itemComponent'] =
templates.item
? ({ item }) => {
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
rootTagName="fragment"
data={item}
sendEvent={sendEvent}
sendEvent={_sendEvent}
rootProps={{ ...rootProps }}
/>
);
}
: undefined
) as FrequentlyBoughtTogetherUiProps<AlgoliaHit>['itemComponent'];
: undefined;

const emptyComponent = (
templates.empty
Expand Down Expand Up @@ -189,7 +190,7 @@ export type FrequentlyBoughtTogetherTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: Template<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<AlgoliaHit<THit>>;

/**
* Template to use to wrap all items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
BaseHit,
RecommendResponse,
Hit,
TemplateWithBindEvent,
} from '../../types';
import type {
RecommendClassNames,
Expand Down Expand Up @@ -83,21 +84,21 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
: undefined
) as LookingSimilarUiProps<AlgoliaHit>['headerComponent'];

const itemComponent = (
const itemComponent: LookingSimilarUiProps<AlgoliaHit>['itemComponent'] =
templates.item
? ({ item }) => {
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
rootTagName="fragment"
data={item}
sendEvent={sendEvent}
sendEvent={_sendEvent}
rootProps={{ ...rootProps }}
/>
);
}
: undefined
) as LookingSimilarUiProps<AlgoliaHit>['itemComponent'];
: undefined;

const emptyComponent = (
templates.empty
Expand Down Expand Up @@ -187,7 +188,7 @@ export type LookingSimilarTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: Template<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<AlgoliaHit<THit>>;

/**
* Template to use to wrap all items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
BaseHit,
RecommendResponse,
Hit,
TemplateWithBindEvent,
} from '../../types';
import type {
RecommendClassNames,
Expand Down Expand Up @@ -90,21 +91,21 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
: undefined
) as RelatedProductsUiProps<AlgoliaHit>['headerComponent'];

const itemComponent = (
const itemComponent: RelatedProductsUiProps<AlgoliaHit>['itemComponent'] =
templates.item
? ({ item }) => {
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
rootTagName="fragment"
data={item}
sendEvent={sendEvent}
sendEvent={_sendEvent}
rootProps={{ ...rootProps }}
/>
);
}
: undefined
) as RelatedProductsUiProps<AlgoliaHit>['itemComponent'];
: undefined;

const emptyComponent = (
templates.empty
Expand Down Expand Up @@ -194,7 +195,7 @@ export type RelatedProductsTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: Template<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<AlgoliaHit<THit>>;

/**
* Template to use to wrap all items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
BaseHit,
RecommendResponse,
Hit,
TemplateWithBindEvent,
} from '../../types';
import type {
RecommendClassNames,
Expand Down Expand Up @@ -90,21 +91,21 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
: undefined
) as TrendingItemsUiProps<AlgoliaHit>['headerComponent'];

const itemComponent = (
const itemComponent: TrendingItemsUiProps<AlgoliaHit>['itemComponent'] =
templates.item
? ({ item }) => {
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
rootTagName="fragment"
data={item}
sendEvent={sendEvent}
sendEvent={_sendEvent}
rootProps={{ ...rootProps }}
/>
);
}
: undefined
) as TrendingItemsUiProps<AlgoliaHit>['itemComponent'];
: undefined;

const emptyComponent = (
templates.empty
Expand Down Expand Up @@ -193,7 +194,7 @@ export type TrendingItemsTemplates<THit extends NonNullable<object> = BaseHit> =
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: Template<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<AlgoliaHit<THit>>;

/**
* Template to use to wrap all items.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createFrequentlyBoughtTogetherComponent } from 'instantsearch-ui-components';
import React, { createElement, Fragment } from 'react';
import React, { createElement, Fragment, useMemo } from 'react';
import {
useFrequentlyBoughtTogether,
useInstantSearch,
Expand Down Expand Up @@ -77,9 +77,17 @@ export function FrequentlyBoughtTogether<THit extends BaseHit = BaseHit>({
})
: undefined;

const _itemComponent: typeof itemComponent = useMemo(
() =>
itemComponent
? (itemProps) => itemComponent({ ...itemProps, sendEvent })
: undefined,
[itemComponent, sendEvent]
);

const uiProps: UiProps<THit> = {
items: items as Array<Hit<THit>>,
itemComponent,
itemComponent: _itemComponent,
headerComponent,
emptyComponent,
layout,
Expand Down
12 changes: 10 additions & 2 deletions packages/react-instantsearch/src/widgets/LookingSimilar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLookingSimilarComponent } from 'instantsearch-ui-components';
import React, { createElement, Fragment } from 'react';
import React, { createElement, Fragment, useMemo } from 'react';
import { useLookingSimilar, useInstantSearch } from 'react-instantsearch-core';

import type {
Expand Down Expand Up @@ -75,9 +75,17 @@ export function LookingSimilar<THit extends BaseHit = BaseHit>({
})
: undefined;

const _itemComponent: typeof itemComponent = useMemo(
() =>
itemComponent
? (itemProps) => itemComponent({ ...itemProps, sendEvent })
: undefined,
[itemComponent, sendEvent]
);

const uiProps: UiProps<THit> = {
items: items as Array<Hit<THit>>,
itemComponent,
itemComponent: _itemComponent,
headerComponent,
emptyComponent,
layout,
Expand Down
12 changes: 10 additions & 2 deletions packages/react-instantsearch/src/widgets/RelatedProducts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRelatedProductsComponent } from 'instantsearch-ui-components';
import React, { createElement, Fragment } from 'react';
import React, { createElement, Fragment, useMemo } from 'react';
import { useInstantSearch, useRelatedProducts } from 'react-instantsearch-core';

import type {
Expand Down Expand Up @@ -75,9 +75,17 @@ export function RelatedProducts<TItem extends BaseHit = BaseHit>({
})
: undefined;

const _itemComponent: typeof itemComponent = useMemo(
() =>
itemComponent
? (itemProps) => itemComponent({ ...itemProps, sendEvent })
: undefined,
[itemComponent, sendEvent]
);

const uiProps: UiProps<TItem> = {
items: items as Array<Hit<TItem>>,
itemComponent,
itemComponent: _itemComponent,
headerComponent,
emptyComponent,
layout,
Expand Down
Loading