From 183b1ca2e8f7727ba397a1cb459975fd6ddfa9d9 Mon Sep 17 00:00:00 2001 From: eunjae-lee Date: Mon, 31 Aug 2020 14:02:23 +0200 Subject: [PATCH 1/4] chore: rename files for better alignment --- src/lib/InstantSearch.ts | 7 +++++-- .../{insights.ts => createInsightsMiddleware.ts} | 0 .../{createRouter.ts => createRouterMiddleware.ts} | 2 +- src/middlewares/index.ts | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) rename src/middlewares/{insights.ts => createInsightsMiddleware.ts} (100%) rename src/middlewares/{createRouter.ts => createRouterMiddleware.ts} (97%) diff --git a/src/lib/InstantSearch.ts b/src/lib/InstantSearch.ts index 27a2743783..c88022da09 100644 --- a/src/lib/InstantSearch.ts +++ b/src/lib/InstantSearch.ts @@ -21,7 +21,10 @@ import { MiddlewareDefinition, } from '../types'; import hasDetectedInsightsClient from './utils/detect-insights-client'; -import { createRouter, RouterProps } from '../middlewares/createRouter'; +import { + createRouterMiddleware, + RouterProps, +} from '../middlewares/createRouterMiddleware'; const withUsage = createDocumentationMessageGenerator({ name: 'instantsearch', @@ -242,7 +245,7 @@ See ${createDocumentationLink({ if (routing) { const routerOptions = typeof routing === 'boolean' ? undefined : routing; - this.EXPERIMENTAL_use(createRouter(routerOptions)); + this.EXPERIMENTAL_use(createRouterMiddleware(routerOptions)); } } diff --git a/src/middlewares/insights.ts b/src/middlewares/createInsightsMiddleware.ts similarity index 100% rename from src/middlewares/insights.ts rename to src/middlewares/createInsightsMiddleware.ts diff --git a/src/middlewares/createRouter.ts b/src/middlewares/createRouterMiddleware.ts similarity index 97% rename from src/middlewares/createRouter.ts rename to src/middlewares/createRouterMiddleware.ts index 3045b1595c..04d4515d82 100644 --- a/src/middlewares/createRouter.ts +++ b/src/middlewares/createRouterMiddleware.ts @@ -29,7 +29,7 @@ export type RouterProps = { export type RoutingManager = (props?: RouterProps) => Middleware; -export const createRouter: RoutingManager = (props = {}) => { +export const createRouterMiddleware: RoutingManager = (props = {}) => { const { router = historyRouter(), stateMapping = simpleStateMapping(), diff --git a/src/middlewares/index.ts b/src/middlewares/index.ts index d1ef9f339f..6a413e8d04 100644 --- a/src/middlewares/index.ts +++ b/src/middlewares/index.ts @@ -1,2 +1,2 @@ -export { createInsightsMiddleware } from './insights'; -export * from './createRouter'; +export { createInsightsMiddleware } from './createInsightsMiddleware'; +export * from './createRouterMiddleware'; From e7c7e5858aada1f4ee105764d524ca0bc54212ed Mon Sep 17 00:00:00 2001 From: eunjae-lee Date: Mon, 31 Aug 2020 14:03:00 +0200 Subject: [PATCH 2/4] chore: export all types related to insights middleware --- src/middlewares/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middlewares/index.ts b/src/middlewares/index.ts index 6a413e8d04..4228b30946 100644 --- a/src/middlewares/index.ts +++ b/src/middlewares/index.ts @@ -1,2 +1,2 @@ -export { createInsightsMiddleware } from './createInsightsMiddleware'; +export * from './createInsightsMiddleware'; export * from './createRouterMiddleware'; From 8c7298e25d4c3f5f6f5a0d971ce75ea6c8b1aa97 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 31 Aug 2020 14:05:07 +0200 Subject: [PATCH 3/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François Chalifour --- src/connectors/hits/connectHits.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connectors/hits/connectHits.ts b/src/connectors/hits/connectHits.ts index 44bf50aad2..c222d84725 100644 --- a/src/connectors/hits/connectHits.ts +++ b/src/connectors/hits/connectHits.ts @@ -30,12 +30,12 @@ export type HitsRendererOptions = { results?: SearchResults; /** - * Send event to insights middleware + * Sends an event to the Insights middleware. */ sendEvent: SendEventForHits; /** - * Returns a string of data-insights-event attribute for insights middleware + * Returns a string for the `data-insights-event` attribute for the Insights middleware */ bindEvent: BindEventForHits; }; From 61ca509ced8082c6efd007d8ab04f0c65645bba6 Mon Sep 17 00:00:00 2001 From: eunjae-lee Date: Mon, 31 Aug 2020 14:09:33 +0200 Subject: [PATCH 4/4] fix: allow only one way for sending events --- src/lib/insights/listener.tsx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lib/insights/listener.tsx b/src/lib/insights/listener.tsx index 194abe571d..6a1361eb20 100644 --- a/src/lib/insights/listener.tsx +++ b/src/lib/insights/listener.tsx @@ -47,19 +47,8 @@ const parseInsightsEvent = element => { const insightsListener = (BaseComponent: any) => { function WithInsightsListener(props: WithInsightsListenerProps) { const handleClick = (event: MouseEvent): void => { - // old way, e.g. instantsearch.insights("clickedObjectIDsAfterSearch", { .. }) - const insightsTarget = findInsightsTarget( - event.target as HTMLElement | null, - event.currentTarget as HTMLElement | null, - element => hasDataAttributes(element) - ); - if (insightsTarget) { - const { method, payload } = readDataAttributes(insightsTarget); - props.insights(method, payload); - } - - // new way with insights middleware if (props.sendEvent) { + // new way with insights middleware const targetWithEvent = findInsightsTarget( event.target as HTMLElement | null, event.currentTarget as HTMLElement | null, @@ -69,6 +58,17 @@ const insightsListener = (BaseComponent: any) => { const payload = parseInsightsEvent(targetWithEvent); props.sendEvent(payload); } + } else { + // old way, e.g. instantsearch.insights("clickedObjectIDsAfterSearch", { .. }) + const insightsTarget = findInsightsTarget( + event.target as HTMLElement | null, + event.currentTarget as HTMLElement | null, + element => hasDataAttributes(element) + ); + if (insightsTarget) { + const { method, payload } = readDataAttributes(insightsTarget); + props.insights(method, payload); + } } };