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(headless): add define functions for RecentQueriesList, FieldSuggestionsGenerator, Triggers in commerce SSR #4258

Merged
merged 10 commits into from
Aug 9, 2024
10 changes: 5 additions & 5 deletions packages/headless/src/commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ export type {
} from './controllers/commerce/search-box/headless-search-box';

export {buildRecentQueriesList} from './controllers/commerce/recent-queries-list/headless-recent-queries-list';
export type {RecentQueriesList} from './controllers/commerce/recent-queries-list/headless-recent-queries-list';
export type {
RecentQueriesList,
RecentQueriesListOptions,
RecentQueriesListProps,
RecentQueriesListInitialState,
RecentQueriesState,
} from './controllers/recent-queries-list/headless-recent-queries-list.ts';
} from './controllers/commerce/recent-queries-list/headless-recent-queries-list';

export {buildInstantProducts} from './controllers/commerce/instant-products/headless-instant-products';
export type {
Expand Down Expand Up @@ -275,20 +275,20 @@ export type {
NotifyTrigger,
NotifyTriggerState,
} from './controllers/core/triggers/headless-core-notify-trigger';
export {buildNotifyTrigger} from './controllers/commerce/triggers/headless-commerce-notify-trigger';
export {buildNotifyTrigger} from './controllers/commerce/triggers/headless-notify-trigger';

export type {
RedirectionTrigger,
RedirectionTriggerState,
} from './controllers/core/triggers/headless-core-redirection-trigger';

export {buildRedirectionTrigger} from './controllers/commerce/triggers/headless-commerce-redirection-trigger';
export {buildRedirectionTrigger} from './controllers/commerce/triggers/headless-redirection-trigger';

export type {
QueryTrigger,
QueryTriggerState,
} from './controllers/core/triggers/headless-core-query-trigger';
export {buildQueryTrigger} from './controllers/commerce/triggers/headless-commerce-query-trigger';
export {buildQueryTrigger} from './controllers/commerce/triggers/headless-query-trigger';

export type {
FieldSuggestions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {SharedControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common';
import {
FieldSuggestionsGenerator,
buildFieldSuggestionsGenerator,
} from './headless-field-suggestions-generator';

export type {
FieldSuggestions,
FieldSuggestionsState,
} from '../field-suggestions/headless-field-suggestions';
export type {
CategoryFieldSuggestions,
CategoryFieldSuggestionsState,
} from '../field-suggestions/headless-category-field-suggestions';
export type {FieldSuggestionsFacet} from '../../../features/commerce/facets/field-suggestions-order/field-suggestions-order-state';
export type {GeneratedFieldSuggestionsControllers} from './headless-field-suggestions-generator';
export type {FieldSuggestionsGenerator};

export interface FieldSuggestionsGeneratorDefinition
extends SharedControllerDefinitionWithoutProps<FieldSuggestionsGenerator> {}

/**
* Defines a `FieldSuggestionsGenerator` controller instance.
*
* @returns The `FieldSuggestionsGenerator` controller definition.
*
* @internal
*/
export function defineFieldSuggestionsGenerator(): FieldSuggestionsGeneratorDefinition {
return {
search: true,
listing: true,
build: (engine) => buildFieldSuggestionsGenerator(engine),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {SearchOnlyControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common';
import {
RecentQueriesList,
RecentQueriesListProps,
buildRecentQueriesList,
} from './headless-recent-queries-list';

export type {
RecentQueriesState,
RecentQueriesListInitialState,
RecentQueriesListOptions,
} from './headless-recent-queries-list';
export type {RecentQueriesList, RecentQueriesListProps};

export interface RecentQueriesListDefinition
extends SearchOnlyControllerDefinitionWithoutProps<RecentQueriesList> {}

/**
* Defines a `RecentQueriesList` controller instance.
*
* @param props - The configuration `RecentQueriesList` properties.
* @returns The `RecentQueriesList` controller definition.
*
* @internal
*/
export function defineRecentQueriesList(
props: RecentQueriesListProps = {}
): RecentQueriesListDefinition {
return {
search: true,
build: (engine) => buildRecentQueriesList(engine, props),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import type {
RecentQueriesListProps,
} from '../../recent-queries-list/headless-recent-queries-list';

export type {
RecentQueriesListProps,
RecentQueriesListInitialState,
RecentQueriesListOptions,
};
const defaultRecentQueriesState: Required<RecentQueriesListInitialState> = {
queries: [],
};
Expand Down Expand Up @@ -85,7 +90,7 @@ export interface RecentQueriesState {
analyticsEnabled: boolean;
}

export function validateRecentQueriesProps(
function validateRecentQueriesProps(
engine: CommerceEngine,
props?: RecentQueriesListProps
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {SharedControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common';
import {NotifyTrigger} from '../../core/triggers/headless-core-notify-trigger';
import {buildNotifyTrigger} from './headless-notify-trigger';

export type {NotifyTriggerState} from '../../core/triggers/headless-core-notify-trigger';
export type {NotifyTrigger};
export interface NotifyTriggerDefinition
extends SharedControllerDefinitionWithoutProps<NotifyTrigger> {}

/**
* Defines a `NotifyTrigger` controller instance.
*
* @returns The `NotifyTrigger` controller definition.
*
* @internal
*/
export function defineNotifyTrigger(): NotifyTriggerDefinition {
return {
search: true,
listing: true,
build: (engine) => buildNotifyTrigger(engine),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
buildMockCommerceEngine,
} from '../../../test/mock-engine-v2';
import {NotifyTrigger} from '../../core/triggers/headless-core-notify-trigger';
import {buildNotifyTrigger} from './headless-commerce-notify-trigger';
import {buildNotifyTrigger} from './headless-notify-trigger';

describe('commerce notify trigger', () => {
let engine: MockedCommerceEngine;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {SharedControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common';
import {QueryTrigger} from '../../core/triggers/headless-core-query-trigger';
import {buildQueryTrigger} from './headless-query-trigger';

export type {QueryTriggerState} from '../../core/triggers/headless-core-query-trigger';
export type {QueryTrigger};
export interface QueryTriggerDefinition
extends SharedControllerDefinitionWithoutProps<QueryTrigger> {}

/**
* Defines a `NotifyTrigger` controller instance.
*
* @returns The `NotifyTrigger` controller definition.
*
* @internal
*/
export function defineQueryTrigger(): QueryTriggerDefinition {
return {
search: true,
listing: true,
build: (engine) => buildQueryTrigger(engine),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
buildMockCommerceEngine,
} from '../../../test/mock-engine-v2';
import {QueryTrigger} from '../../core/triggers/headless-core-query-trigger';
import {buildQueryTrigger} from './headless-commerce-query-trigger';
import {buildQueryTrigger} from './headless-query-trigger';

jest.mock('../../../features/commerce/query/query-actions');
jest.mock('../../../features/commerce/search/search-actions');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {SharedControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common';
import {RedirectionTrigger} from '../../core/triggers/headless-core-redirection-trigger';
import {buildRedirectionTrigger} from './headless-redirection-trigger';

export type {RedirectionTriggerState} from '../../core/triggers/headless-core-redirection-trigger';
export type {RedirectionTrigger};
export interface RedirectionTriggerDefinition
extends SharedControllerDefinitionWithoutProps<RedirectionTrigger> {}

/**
* Defines a `NotifyTrigger` controller instance.
*
* @returns The `NotifyTrigger` controller definition.
*
* @internal
*/
export function defineRedirectionTrigger(): RedirectionTriggerDefinition {
return {
search: true,
listing: true,
build: (engine) => buildRedirectionTrigger(engine),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
buildMockCommerceEngine,
} from '../../../test/mock-engine-v2';
import {RedirectionTrigger} from '../../core/triggers/headless-core-redirection-trigger';
import {buildRedirectionTrigger} from './headless-commerce-redirection-trigger';
import {buildRedirectionTrigger} from './headless-redirection-trigger';

describe('RedirectionTrigger', () => {
let engine: MockedCommerceEngine;
Expand Down
45 changes: 45 additions & 0 deletions packages/headless/src/ssr-commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type {
CartItem,
CartProps,
CartState,
CartDefinition,
} from './controllers/commerce/context/cart/headless-cart.ssr';
export {defineCart} from './controllers/commerce/context/cart/headless-cart.ssr';

Expand All @@ -86,6 +87,7 @@ export type {
ContextProps,
ContextState,
View,
ContextDefinition,
} from './controllers/commerce/context/headless-context.ssr';
export {defineContext} from './controllers/commerce/context/headless-context.ssr';

Expand All @@ -95,6 +97,7 @@ export type {
SearchBoxProps,
SearchBoxState,
Suggestion,
SearchBoxDefinition,
} from './controllers/commerce/search-box/headless-search-box.ssr';
export {defineSearchBox} from './controllers/commerce/search-box/headless-search-box.ssr';

Expand All @@ -104,6 +107,48 @@ export type {
} from './controllers/commerce/core/sub-controller/headless-sub-controller.ssr';
export {defineQuerySummary} from './controllers/commerce/core/sub-controller/headless-sub-controller.ssr';

export type {
RecentQueriesList,
RecentQueriesListOptions,
RecentQueriesListProps,
RecentQueriesListInitialState,
RecentQueriesState,
RecentQueriesListDefinition,
} from './controllers/commerce/recent-queries-list/headless-recent-queries-list.ssr';
export {defineRecentQueriesList} from './controllers/commerce/recent-queries-list/headless-recent-queries-list.ssr';

export type {
FieldSuggestions,
FieldSuggestionsState,
CategoryFieldSuggestions,
CategoryFieldSuggestionsState,
FieldSuggestionsFacet,
GeneratedFieldSuggestionsControllers,
FieldSuggestionsGenerator,
FieldSuggestionsGeneratorDefinition,
} from './controllers/commerce/field-suggestions/headless-field-suggestions-generator.ssr';
export {defineFieldSuggestionsGenerator} from './controllers/commerce/field-suggestions/headless-field-suggestions-generator.ssr';

export type {
NotifyTrigger,
NotifyTriggerState,
NotifyTriggerDefinition,
} from './controllers/commerce/triggers/headless-notify-trigger.ssr';
export {defineNotifyTrigger} from './controllers/commerce/triggers/headless-notify-trigger.ssr';

export type {
QueryTrigger,
QueryTriggerState,
QueryTriggerDefinition,
} from './controllers/commerce/triggers/headless-query-trigger.ssr';
export {defineQueryTrigger} from './controllers/commerce/triggers/headless-query-trigger.ssr';

export type {
RedirectionTrigger,
RedirectionTriggerState,
RedirectionTriggerDefinition,
} from './controllers/commerce/triggers/headless-redirection-trigger.ssr';
export {defineRedirectionTrigger} from './controllers/commerce/triggers/headless-redirection-trigger.ssr';
// TODO: KIT-3391 - export other SSR commerce controllers

//#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
SearchStaticState,
searchEngineDefinition,
} from '../../_lib/commerce-engine';
import {NotifyTrigger} from '../notify-trigger';
import {ProductList} from '../product-list';
import {QueryTrigger} from '../query-trigger';
import {RedirectionTrigger} from '../redirection-trigger';
import {SearchBox} from '../search-box';
import {Summary} from '../summary';

Expand Down Expand Up @@ -37,9 +40,21 @@ export default function SearchPage({

return (
<>
<RedirectionTrigger
staticState={staticState.controllers.redirectionTrigger.state}
controller={hydratedState?.controllers.redirectionTrigger}
/>
<QueryTrigger
staticState={staticState.controllers.queryTrigger.state}
controller={hydratedState?.controllers.queryTrigger}
/>
<SearchBox
staticState={staticState.controllers.searchBox.state}
controller={hydratedState?.controllers.searchBox}
staticStateRecentQueries={
staticState.controllers.recentQueriesList.state
}
recentQueriesController={hydratedState?.controllers.recentQueriesList}
/>
<Summary
staticState={staticState.controllers.summarySearch.state}
Expand All @@ -51,6 +66,10 @@ export default function SearchPage({
staticState={staticState.controllers.productListSearch.state}
controller={hydratedState?.controllers.productListSearch}
/>
<NotifyTrigger
staticState={staticState.controllers.notifyTrigger.state}
controller={hydratedState?.controllers.notifyTrigger}
/>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
NotifyTrigger as NotifyTriggerController,
NotifyTriggerState,
} from '@coveo/headless/commerce';
import {FunctionComponent, useCallback, useEffect, useState} from 'react';

interface NotifyTriggerProps {
controller?: NotifyTriggerController;
staticState: NotifyTriggerState;
}

// The notify trigger query example in the searchuisamples org is 'notify me'.
export const NotifyTrigger: FunctionComponent<NotifyTriggerProps> = ({
controller,
staticState,
}) => {
const [state, setState] = useState(staticState);

useEffect(
() => controller?.subscribe(() => setState({...controller.state})),
[controller]
);

const notify = useCallback(() => {
state.notifications.forEach((notification) => {
alert(`Notification: ${notification}`);
});
}, [state.notifications]);

useEffect(() => {
notify();
}, [notify]);

return null;
};
Loading
Loading