Skip to content

Commit

Permalink
feat(headless commerce): create use & export commerce-specific action…
Browse files Browse the repository at this point in the history
…s & loaders (#4124)

https://coveord.atlassian.net/browse/KIT-3207

This PR is starting to grow huge, so I'll put it in review.

I linked another Jira in KIT-3207. I'll open a few other PRs to do the
rest of the work.
  • Loading branch information
fbeaudoincoveo authored Jul 2, 2024
1 parent 052fac3 commit ce6fb7e
Show file tree
Hide file tree
Showing 74 changed files with 1,736 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Unsubscribe,
UrlManager,
buildSearch,
updateQuery,
loadQueryActions,
CommerceEngine,
CommerceEngineConfiguration,
buildCommerceEngine,
Expand Down Expand Up @@ -295,7 +295,9 @@ export class AtomicCommerceInterface
safeStorage.removeItem(StorageItems.STANDALONE_SEARCH_BOX_DATA);
const {value} = standaloneSearchBoxData;

this.engine.dispatch(updateQuery({query: value}));
this.engine.dispatch(
loadQueryActions(this.engine).updateQuery({query: value})
);
(this.searchOrListing as Search).executeFirstSearch();
} else {
(this.searchOrListing as ProductListing).executeFirstRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

await commerceInterface.initializeWithEngine(commerceEngine);

commerceInterface.executeFirstSearch();
commerceInterface.executeFirstRequest();
})();
</script>
</head>
Expand Down
35 changes: 22 additions & 13 deletions packages/headless/src/commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,27 @@ export type {
export type {PlatformEnvironment} from './utils/url-utils';

// Actions
export * from './features/commerce/context/context-actions-loader';
export * from './features/commerce/search/search-actions-loader';
export * from './features/commerce/product-listing/product-listing-actions-loader';
export * from './features/commerce/recommendations/recommendations-actions-loader';
export * from './features/commerce/pagination/pagination-actions-loader';
export * from './features/commerce/product/product-actions-loaders';
export * from './features/commerce/context/cart/cart-actions-loader';
export * from './features/commerce/sort/sort-actions-loader';
export * from './features/commerce/facets/core-facet/core-facet-actions-loader';
export * from './features/commerce/facets/category-facet/category-facet-actions-loader';
export * from './features/commerce/facets/regular-facet/regular-facet-actions-loader';
export * from './features/commerce/facets/date-facet/date-facet-actions-loader';
export * from './features/commerce/facets/numeric-facet/numeric-facet-actions-loader';
export * from './features/commerce/query-set/query-set-actions-loader';
export * from './features/commerce/query-suggest/query-suggest-actions-loader';
export * from './features/configuration/configuration-actions-loader';
export * from './features/commerce/query/query-actions-loader';
export * from './features/commerce/search-parameters/search-parameters-actions-loader';
export * from './features/commerce/product-listing-parameters/product-listing-parameters-actions-loader';
export * from './features/commerce/query-set/query-set-actions-loader';
export * from './features/commerce/triggers/triggers-actions-loader';
// TODO: KIT-3350: Create/use/export remaining commerce actions/loaders

// Selectors
export {Selectors};
Expand All @@ -64,6 +78,12 @@ export type {
} from './controllers/commerce/context/headless-context';
export {buildContext} from './controllers/commerce/context/headless-context';

export type {
Search,
SearchState,
} from './controllers/commerce/search/headless-search';
export {buildSearch} from './controllers/commerce/search/headless-search';

export type {
ProductListing,
ProductListingState,
Expand All @@ -89,7 +109,7 @@ export type {
InteractiveProduct,
InteractiveProductOptions,
InteractiveProductProps,
} from './controllers/commerce/core/product-list/headless-core-interactive-product';
} from './controllers/commerce/core/interactive-product/headless-core-interactive-product';

export type {InteractiveResultCore} from './controllers/core/interactive-result/headless-core-interactive-result';

Expand Down Expand Up @@ -175,15 +195,6 @@ export type {
} from './controllers/commerce/core/facets/category/headless-commerce-category-facet-search';
export type {CategoryFacetSearchResult} from './api/search/facet-search/category-facet-search/category-facet-search-response';

export type {
Search,
SearchState,
} from './controllers/commerce/search/headless-search';
export {buildSearch} from './controllers/commerce/search/headless-search';

export {updateQuery} from './features/commerce/query/query-actions';
export type {UpdateQueryActionCreatorPayload} from './features/commerce/query/query-actions';

export {buildSearchBox} from './controllers/commerce/search-box/headless-search-box';
export type {
SearchBox,
Expand Down Expand Up @@ -287,8 +298,6 @@ export type {FieldSuggestionsGenerator} from './controllers/commerce/field-sugge
export type {FieldSuggestionsFacet} from './features/commerce/facets/field-suggestions-order/field-suggestions-order-state.ts';
export {buildFieldSuggestionsGenerator} from './controllers/commerce/field-suggestions/headless-field-suggestions-generator';

export type {FetchQuerySuggestionsActionCreatorPayload} from './features/query-suggest/query-suggest-actions';

export type {
ParameterManager,
ParameterManagerState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import {Action} from '@reduxjs/toolkit';
import {stateKey} from '../../../../app/state-key';
import {deselectAllBreadcrumbs} from '../../../../features/breadcrumb/breadcrumb-actions';
import {
clearAllCoreFacets,
deselectAllValuesInCoreFacet,
updateCoreFacetFreezeCurrentValues,
} from '../../../../features/commerce/facets/core-facet/core-facet-actions';
import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
} from '../../../../features/commerce/facets/date-facet/date-facet-actions';
import {commerceFacetSetReducer as commerceFacetSet} from '../../../../features/commerce/facets/facet-set/facet-set-slice';
import {
AnyFacetResponse,
Expand All @@ -10,22 +18,16 @@ import {
NumericFacetValue,
RegularFacetValue,
} from '../../../../features/commerce/facets/facet-set/interfaces/response';
import {FacetValueState} from '../../../../features/facets/facet-api/value';
import {facetOrderReducer as facetOrder} from '../../../../features/facets/facet-order/facet-order-slice';
import {
deselectAllFacetValues,
toggleExcludeFacetValue,
toggleSelectFacetValue,
updateFreezeCurrentValues,
} from '../../../../features/facets/facet-set/facet-set-actions';
import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
} from '../../../../features/facets/range-facets/date-facet-set/date-facet-actions';
import {
toggleExcludeNumericFacetValue,
toggleSelectNumericFacetValue,
} from '../../../../features/facets/range-facets/numeric-facet-set/numeric-facet-actions';
} from '../../../../features/commerce/facets/numeric-facet/numeric-facet-actions';
import {
toggleExcludeFacetValue,
toggleSelectFacetValue,
} from '../../../../features/commerce/facets/regular-facet/regular-facet-actions';
import {FacetValueState} from '../../../../features/facets/facet-api/value';
import {facetOrderReducer as facetOrder} from '../../../../features/facets/facet-order/facet-order-slice';
import {buildMockCommerceFacetRequest} from '../../../../test/mock-commerce-facet-request';
import {
buildMockCategoryFacetResponse,
Expand All @@ -44,17 +46,15 @@ import {
CoreBreadcrumbManagerOptions,
} from './headless-core-breadcrumb-manager';

jest.mock('../../../../features/facets/facet-set/facet-set-actions');
jest.mock('../../../../features/commerce/facets/core-facet/core-facet-actions');
jest.mock(
'../../../../features/facets/range-facets/numeric-facet-set/numeric-facet-actions'
);
jest.mock(
'../../../../features/facets/range-facets/date-facet-set/date-facet-actions'
'../../../../features/commerce/facets/numeric-facet/numeric-facet-actions'
);
jest.mock('../../../../features/commerce/facets/date-facet/date-facet-actions');

jest.mock(
'../../../../features/facets/category-facet-set/category-facet-set-actions'
'../../../../features/commerce/facets/regular-facet/regular-facet-actions'
);
jest.mock('../../../../features/breadcrumb/breadcrumb-actions');

describe('core breadcrumb manager', () => {
let engine: MockedCommerceEngine;
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('core breadcrumb manager', () => {
describe('#deselectAll', () => {
it('deselects all breadcrumbs', () => {
breadcrumbManager.deselectAll();
expect(deselectAllBreadcrumbs).toHaveBeenCalled();
expect(clearAllCoreFacets).toHaveBeenCalled();
});

it('dispatches #fetchProductsActionCreator', () => {
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('core breadcrumb manager', () => {

it('when facet is selected, #deselect dispatches #toggleSelectActionCreator and #fetchProductsActionCreator', () => {
deselectBreadcrumb();
expect(deselectAllFacetValues).toHaveBeenCalledWith(facetId);
expect(deselectAllValuesInCoreFacet).toHaveBeenCalledWith(facetId);
expect(fetchProductsActionCreator).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('core breadcrumb manager', () => {
});

it('dispatches #updateFreezeCurrentValues', () => {
expect(updateFreezeCurrentValues).toHaveBeenCalledWith({
expect(updateCoreFacetFreezeCurrentValues).toHaveBeenCalledWith({
facetId,
freezeCurrentValues: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import {
CommerceEngineState,
} from '../../../../app/commerce-engine/commerce-engine';
import {stateKey} from '../../../../app/state-key';
import {deselectAllBreadcrumbs} from '../../../../features/breadcrumb/breadcrumb-actions';
import {
clearAllCoreFacets,
deselectAllValuesInCoreFacet,
updateCoreFacetFreezeCurrentValues,
} from '../../../../features/commerce/facets/core-facet/core-facet-actions';
import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
} from '../../../../features/commerce/facets/date-facet/date-facet-actions';
import {commerceFacetSetReducer as commerceFacetSet} from '../../../../features/commerce/facets/facet-set/facet-set-slice';
import {FacetType} from '../../../../features/commerce/facets/facet-set/interfaces/common';
import {
Expand All @@ -16,22 +24,16 @@ import {
NumericFacetResponse,
RegularFacetResponse,
} from '../../../../features/commerce/facets/facet-set/interfaces/response';
import {findActiveValueAncestry} from '../../../../features/facets/category-facet-set/category-facet-utils';
import {facetOrderReducer as facetOrder} from '../../../../features/facets/facet-order/facet-order-slice';
import {
deselectAllFacetValues,
toggleExcludeFacetValue,
toggleSelectFacetValue,
updateFreezeCurrentValues,
} from '../../../../features/facets/facet-set/facet-set-actions';
import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
} from '../../../../features/facets/range-facets/date-facet-set/date-facet-actions';
import {
toggleExcludeNumericFacetValue,
toggleSelectNumericFacetValue,
} from '../../../../features/facets/range-facets/numeric-facet-set/numeric-facet-actions';
} from '../../../../features/commerce/facets/numeric-facet/numeric-facet-actions';
import {
toggleExcludeFacetValue,
toggleSelectFacetValue,
} from '../../../../features/commerce/facets/regular-facet/regular-facet-actions';
import {findActiveValueAncestry} from '../../../../features/facets/category-facet-set/category-facet-utils';
import {facetOrderReducer as facetOrder} from '../../../../features/facets/facet-order/facet-order-slice';
import {
CommerceFacetSetSection,
FacetOrderSection,
Expand Down Expand Up @@ -128,7 +130,7 @@ const actions: Record<FacetType, ActionCreators> = {
toggleExcludeActionCreator: toggleExcludeDateFacetValue,
},
[facetTypeWithoutExcludeAction]: {
toggleSelectActionCreator: deselectAllFacetValues,
toggleSelectActionCreator: deselectAllValuesInCoreFacet,
},
};

Expand Down Expand Up @@ -178,7 +180,7 @@ export function buildCoreBreadcrumbManager(
})
);
dispatch(
updateFreezeCurrentValues({
updateCoreFacetFreezeCurrentValues({
facetId: facet.facetId,
freezeCurrentValues: false,
})
Expand All @@ -195,7 +197,7 @@ export function buildCoreBreadcrumbManager(
})
);
dispatch(
updateFreezeCurrentValues({
updateCoreFacetFreezeCurrentValues({
facetId: facet.facetId,
freezeCurrentValues: false,
})
Expand Down Expand Up @@ -249,7 +251,7 @@ export function buildCoreBreadcrumbManager(
...controller,

deselectAll: () => {
dispatch(deselectAllBreadcrumbs());
dispatch(clearAllCoreFacets());
dispatch(options.fetchProductsActionCreator());
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {CategoryFacetResponse} from '../../../../../features/commerce/facets/facet-set/interfaces/response';
import {
toggleSelectCategoryFacetValue,
updateCategoryFacetNumberOfValues,
} from '../../../../../features/facets/category-facet-set/category-facet-set-actions';
} from '../../../../../features/commerce/facets/category-facet/category-facet-actions';
import {CategoryFacetResponse} from '../../../../../features/commerce/facets/facet-set/interfaces/response';
import {CommerceAppState} from '../../../../../state/commerce-app-state';
import {buildMockCategoryFacetSearch} from '../../../../../test/mock-category-facet-search';
import {buildMockCommerceFacetRequest} from '../../../../../test/mock-commerce-facet-request';
Expand All @@ -21,7 +21,7 @@ import {
} from './headless-commerce-category-facet';

jest.mock(
'../../../../../features/facets/category-facet-set/category-facet-set-actions'
'../../../../../features/commerce/facets/category-facet/category-facet-actions'
);

describe('CategoryFacet', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
CommerceEngineState,
} from '../../../../../app/commerce-engine/commerce-engine';
import {stateKey} from '../../../../../app/state-key';
import {CategoryFacetValueRequest} from '../../../../../features/commerce/facets/facet-set/interfaces/request';
import {
defaultNumberOfValuesIncrement,
toggleSelectCategoryFacetValue,
updateCategoryFacetNumberOfValues,
} from '../../../../../features/facets/category-facet-set/category-facet-set-actions';
} from '../../../../../features/commerce/facets/category-facet/category-facet-actions';
import {CategoryFacetValueRequest} from '../../../../../features/commerce/facets/facet-set/interfaces/request';
import {defaultNumberOfValuesIncrement} from '../../../../../features/facets/category-facet-set/category-facet-set-actions';
import {findActiveValueAncestry} from '../../../../../features/facets/category-facet-set/category-facet-utils';
import {
CategoryFacetValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {FacetType} from '../../../../../features/commerce/facets/facet-set/interfaces/common';
import {DateFacetRequest} from '../../../../../features/commerce/facets/facet-set/interfaces/request';
import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
updateDateFacetValues,
} from '../../../../../features/facets/range-facets/date-facet-set/date-facet-actions';
} from '../../../../../features/commerce/facets/date-facet/date-facet-actions';
import {FacetType} from '../../../../../features/commerce/facets/facet-set/interfaces/common';
import {DateFacetRequest} from '../../../../../features/commerce/facets/facet-set/interfaces/request';
import {CommerceAppState} from '../../../../../state/commerce-app-state';
import {buildMockCommerceFacetRequest} from '../../../../../test/mock-commerce-facet-request';
import {buildMockCommerceDateFacetResponse} from '../../../../../test/mock-commerce-facet-response';
Expand All @@ -23,7 +23,7 @@ import {
} from './headless-commerce-date-facet';

jest.mock(
'../../../../../features/facets/range-facets/date-facet-set/date-facet-actions'
'../../../../../features/commerce/facets/date-facet/date-facet-actions'
);

describe('DateFacet', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
toggleExcludeDateFacetValue,
toggleSelectDateFacetValue,
updateDateFacetValues,
} from '../../../../../features/facets/range-facets/date-facet-set/date-facet-actions';
} from '../../../../../features/commerce/facets/date-facet/date-facet-actions';
import {buildDateRange} from '../../../../core/facets/range-facet/date-facet/date-range';
import {
CoreCommerceFacet,
Expand Down
Loading

0 comments on commit ce6fb7e

Please sign in to comment.