-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commerce): create PLP v2 pager controller (#3130)
* wip commerce sub-package * Add commerce/ to files * Import polyfillCryptoNode * Basic boilerplate * Comment out commerce use case * Rename analytics * Adjust test descriptions * Remove version from ProductListingV2BaseParam * Remove done TODO * Move propertyId to base params * Eliminate superfluous interfaces * Use logProductListingV2Load analytics action when building mock product listing response * Remove superfluous spread * Add missing comma * Use v2 product listing API client * Update adding sub-package documentation * Add mock commerce engine * Add basic exports * Remove comment * Undo bad changes * Linting * Rename property to tracking * Update API req and response params * Adjust state + buildProductListingRequestV2 params * Simplify buildProductListingRequestV2 function * Reorder request params and remove unreachable fallback values * Remove superfluous actions * Remove version from state * Remove TODO's * Documentation updates * Renaming + docs + small fixes * Remove superfluous response properties * Remove search API client from listing v2 API client * Update imports * Remove useless jsdocs * Use Map instead of Record for labels * Un-document get sample config function and uniformize CommerceEngineConfiguration docs * Add missing jsdoc * feat(productlistings): create plpv2 interactive controller [CAPI-85] * feat(productlistings): move to proper folder [CAPI-85] * feat(commerce): export controller [CAPI-85] * feat(commerce): amend documentation [CAPI-85] * feat(productlistings): create plp v2 pager controller [CAPI-86] * feat(commerce): add controller doc [CAPI-86] * feat(commerce): cover slice case with unit test [CAPI-86] * feat(commerce): expose pager [CAPI-86] --------- Co-authored-by: fbeaudoincoveo <[email protected]>
- Loading branch information
1 parent
fe40054
commit 07bf79f
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ess/src/controllers/commerce/product-listing/pager/headless-product-listing-pager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {fetchProductListing} from '../../../../features/product-listing/v2/product-listing-v2-actions'; | ||
import {buildMockCommerceEngine, MockCommerceEngine} from '../../../../test'; | ||
import { | ||
Pager, | ||
PagerOptions, | ||
PagerInitialState, | ||
buildPager, | ||
} from './headless-product-listing-pager'; | ||
|
||
describe('Pager', () => { | ||
let engine: MockCommerceEngine; | ||
let options: PagerOptions; | ||
let initialState: PagerInitialState; | ||
let pager: Pager; | ||
|
||
function initPager() { | ||
pager = buildPager(engine, {options, initialState}); | ||
} | ||
|
||
beforeEach(() => { | ||
options = {}; | ||
initialState = {}; | ||
engine = buildMockCommerceEngine(); | ||
initPager(); | ||
}); | ||
|
||
it('initializes', () => { | ||
expect(pager).toBeTruthy(); | ||
}); | ||
|
||
it('#selectPage dispatches #fetchProductListing', () => { | ||
pager.selectPage(2); | ||
const action = engine.findAsyncAction(fetchProductListing.pending); | ||
expect(action).toBeTruthy(); | ||
}); | ||
|
||
it('#nextPage dispatches #fetchProductListing', () => { | ||
pager.nextPage(); | ||
const action = engine.findAsyncAction(fetchProductListing.pending); | ||
expect(action).toBeTruthy(); | ||
}); | ||
|
||
it('#previousPage dispatches #fetchProductListing', () => { | ||
pager.previousPage(); | ||
const action = engine.findAsyncAction(fetchProductListing.pending); | ||
expect(engine.actions).toContainEqual(action); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
...headless/src/controllers/commerce/product-listing/pager/headless-product-listing-pager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {CommerceEngine} from '../../../../app/commerce-engine/commerce-engine'; | ||
import {fetchProductListing} from '../../../../features/product-listing/v2/product-listing-v2-actions'; | ||
import { | ||
buildCorePager, | ||
PagerInitialState, | ||
PagerOptions, | ||
PagerProps, | ||
Pager, | ||
PagerState, | ||
} from '../../../core/pager/headless-core-pager'; | ||
|
||
export type {PagerInitialState, PagerOptions, PagerProps, Pager, PagerState}; | ||
|
||
/** | ||
* Creates a `Pager` controller instance for the product listing. | ||
* | ||
* @param engine - The headless commerce engine. | ||
* @param props - The configurable `Pager` properties. | ||
* @returns A `Pager` controller instance. | ||
* */ | ||
export function buildPager( | ||
engine: CommerceEngine, | ||
props: PagerProps = {} | ||
): Pager { | ||
const {dispatch} = engine; | ||
const pager = buildCorePager(engine, props); | ||
|
||
return { | ||
...pager, | ||
|
||
get state() { | ||
return pager.state; | ||
}, | ||
|
||
selectPage(page: number) { | ||
pager.selectPage(page); | ||
dispatch(fetchProductListing()); | ||
}, | ||
|
||
nextPage() { | ||
pager.nextPage(); | ||
dispatch(fetchProductListing()); | ||
}, | ||
|
||
previousPage() { | ||
pager.previousPage(); | ||
dispatch(fetchProductListing()); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters