From 887c839c715b4482c62277e00960396c2b66b779 Mon Sep 17 00:00:00 2001 From: griest024 Date: Fri, 18 Oct 2024 09:53:41 -0400 Subject: [PATCH] feat(category): combine all metadata effects (#3294) --- .../state/src/category-state.module.ts | 4 +- ...=> category-page-metadata.effects.spec.ts} | 66 +++++++++++++-- ...s.ts => category-page-metadata.effects.ts} | 7 +- .../src/effects/category-page.effects.spec.ts | 76 ----------------- .../src/effects/category-page.effects.ts | 82 ++----------------- 5 files changed, 72 insertions(+), 163 deletions(-) rename libs/category/state/src/effects/{category-page-filter.effects.spec.ts => category-page-metadata.effects.spec.ts} (85%) rename libs/category/state/src/effects/{category-page-filter.effects.ts => category-page-metadata.effects.ts} (90%) diff --git a/libs/category/state/src/category-state.module.ts b/libs/category/state/src/category-state.module.ts index 0b483b18c2..26f3fc3cb1 100644 --- a/libs/category/state/src/category-state.module.ts +++ b/libs/category/state/src/category-state.module.ts @@ -4,7 +4,7 @@ import { StoreModule } from '@ngrx/store'; import { DaffProductStateModule } from '@daffodil/product/state'; -import { DaffCategoryPageFilterEffects } from './effects/category-page-filter.effects'; +import { DaffCategoryPageMetadataEffects } from './effects/category-page-metadata.effects'; import { DaffCategoryPageEffects } from './effects/category-page.effects'; import { DaffCategoryEffects } from './effects/category.effects'; import { DAFF_CATEGORY_STORE_FEATURE_KEY } from './reducers/public_api'; @@ -16,7 +16,7 @@ import { DAFF_CATEGORY_REDUCERS } from './reducers/token/reducers.token'; @NgModule({ imports: [ StoreModule.forFeature(DAFF_CATEGORY_STORE_FEATURE_KEY, DAFF_CATEGORY_REDUCERS), - EffectsModule.forFeature([DaffCategoryEffects, DaffCategoryPageEffects, DaffCategoryPageFilterEffects]), + EffectsModule.forFeature([DaffCategoryEffects, DaffCategoryPageEffects, DaffCategoryPageMetadataEffects]), DaffProductStateModule, ], }) diff --git a/libs/category/state/src/effects/category-page-filter.effects.spec.ts b/libs/category/state/src/effects/category-page-metadata.effects.spec.ts similarity index 85% rename from libs/category/state/src/effects/category-page-filter.effects.spec.ts rename to libs/category/state/src/effects/category-page-metadata.effects.spec.ts index d9e0d09549..f4287bbb00 100644 --- a/libs/category/state/src/effects/category-page-filter.effects.spec.ts +++ b/libs/category/state/src/effects/category-page-metadata.effects.spec.ts @@ -26,6 +26,9 @@ import { DaffCategoryPageApplyFilters, DaffCategoryPageClearFilters, DaffCategoryPageRemoveFilters, + DaffCategoryPageChangePageSize, + DaffCategoryPageChangeCurrentPage, + DaffCategoryPageChangeSortingOption, } from '@daffodil/category/state'; import { DaffCategoryStateTestingModule, @@ -42,6 +45,7 @@ import { DaffFilterRequest, daffFiltersToRequests, DaffFilterToggleRequest, + DaffSortDirectionEnum, } from '@daffodil/core'; import { daffTransformErrorToStateError } from '@daffodil/core/state'; import { MockDaffCollectionFacade } from '@daffodil/core/state/testing'; @@ -54,7 +58,7 @@ import { DaffProduct } from '@daffodil/product'; import { DaffProductGridLoadSuccess } from '@daffodil/product/state'; import { DaffProductFactory } from '@daffodil/product/testing'; -import { DaffCategoryPageFilterEffects } from './category-page-filter.effects'; +import { DaffCategoryPageMetadataEffects } from './category-page-metadata.effects'; class MockError extends DaffInheritableError implements DaffError { code = 'code'; @@ -64,9 +68,9 @@ class MockError extends DaffInheritableError implements DaffError { } } -describe('DaffCategoryPageFilterEffects', () => { +describe('DaffCategoryPageMetadataEffects', () => { let actions$: Observable; - let effects: DaffCategoryPageFilterEffects; + let effects: DaffCategoryPageMetadataEffects; let daffCategoryDriver: DaffCategoryServiceInterface; let facade: MockDaffCollectionFacade; @@ -112,7 +116,7 @@ describe('DaffCategoryPageFilterEffects', () => { }; expectObservable( - effects.updateFilters$(300, testScheduler), + effects.update$(300, testScheduler), ).toBe(expectedMarble, expectedValue); }); @@ -162,7 +166,7 @@ describe('DaffCategoryPageFilterEffects', () => { }; expectObservable( - effects.updateFilters$(0, testScheduler), + effects.update$(0, testScheduler), ).toBe(expectedMarble, expectedValue); }); @@ -205,7 +209,7 @@ describe('DaffCategoryPageFilterEffects', () => { }; expectObservable( - effects.updateFilters$(0, testScheduler), + effects.update$(0, testScheduler), ).toBe(expectedMarble, expectedValue); }); @@ -229,14 +233,14 @@ describe('DaffCategoryPageFilterEffects', () => { DaffCategoryStateTestingModule, ], providers: [ - DaffCategoryPageFilterEffects, + DaffCategoryPageMetadataEffects, provideMockActions(() => actions$), ], }); facade = TestBed.inject(MockDaffCollectionFacade); daffCategoryDriver = TestBed.inject(DaffCategoryDriver); - effects = TestBed.inject(DaffCategoryPageFilterEffects); + effects = TestBed.inject(DaffCategoryPageMetadataEffects); categoryFactory = TestBed.inject(DaffCategoryFactory); categoryPageMetadataFactory = TestBed.inject(DaffCategoryPageMetadataFactory); @@ -332,4 +336,50 @@ describe('DaffCategoryPageFilterEffects', () => { testDriverFailure(() => action); }); }); + + describe('when CategoryPageChangePageSizeAction is triggered', () => { + let action: Action; + + beforeEach(() => { + action = new DaffCategoryPageChangePageSize(5); + }); + + describe('driver behavior', () => { + testDriverSuccess(() => action); + testDriverFailure(() => action); + }); + }); + + describe('when CategoryPageChangeCurrentPageAction is triggered', () => { + let toggleRequest: DaffFilterToggleRequest; + let action: Action; + + beforeEach(() => { + toggleRequest = filterToggleRequestFactory.create(); + action = new DaffCategoryPageChangeCurrentPage(2); + }); + + describe('driver behavior', () => { + testDriverSuccess(() => action); + testDriverFailure(() => action); + }); + }); + + describe('when CategoryPageChangeSortingOptionAction is triggered', () => { + let toggleRequest: DaffFilterToggleRequest; + let action: Action; + + beforeEach(() => { + toggleRequest = filterToggleRequestFactory.create(); + action = new DaffCategoryPageChangeSortingOption({ + option: 'option', + direction: DaffSortDirectionEnum.Ascending, + }); + }); + + describe('driver behavior', () => { + testDriverSuccess(() => action); + testDriverFailure(() => action); + }); + }); }); diff --git a/libs/category/state/src/effects/category-page-filter.effects.ts b/libs/category/state/src/effects/category-page-metadata.effects.ts similarity index 90% rename from libs/category/state/src/effects/category-page-filter.effects.ts rename to libs/category/state/src/effects/category-page-metadata.effects.ts index bf44aed521..8a7b78c07a 100644 --- a/libs/category/state/src/effects/category-page-filter.effects.ts +++ b/libs/category/state/src/effects/category-page-metadata.effects.ts @@ -52,7 +52,7 @@ import { DaffCategoryProductCollectionFacade } from '../facades/public_api'; import { DAFF_CATEGORY_ERROR_MATCHER } from '../injection-tokens/public_api'; @Injectable() -export class DaffCategoryPageFilterEffects< +export class DaffCategoryPageMetadataEffects< V extends DaffGenericCategory, W extends DaffProduct > { @@ -73,7 +73,7 @@ export class DaffCategoryPageFilterEffects< * in a sequence. */ - updateFilters$: (throttleWindow: number, scheduler: AsyncScheduler) => Observable< + update$: (throttleWindow: number, scheduler: AsyncScheduler) => Observable< DaffProductGridLoadSuccess | DaffCategoryPageLoadSuccess | DaffCategoryPageLoadFailure @@ -85,6 +85,9 @@ export class DaffCategoryPageFilterEffects< DaffCategoryPageProductCollectionActionTypes.CategoryPageClearFiltersAction, DaffCategoryPageProductCollectionActionTypes.CategoryPageRemoveFiltersAction, DaffCategoryPageProductCollectionActionTypes.CategoryPageToggleFilterAction, + DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSizeAction, + DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeCurrentPageAction, + DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSortingOptionAction, ), withLatestFrom(this.facade.metadata$), map(( diff --git a/libs/category/state/src/effects/category-page.effects.spec.ts b/libs/category/state/src/effects/category-page.effects.spec.ts index b093134f8c..2ba4bf6e3e 100644 --- a/libs/category/state/src/effects/category-page.effects.spec.ts +++ b/libs/category/state/src/effects/category-page.effects.spec.ts @@ -299,80 +299,4 @@ describe('DaffCategoryPageEffects', () => { expect(daffCategoryDriver.getByUrl).toHaveBeenCalledWith(categoryRequest); }); }); - - describe('when ChangeCategoryPageSize action is triggered', () => { - - let expected; - - it('should call get category with an id, page size, applied filters, and an applied sort option', () => { - driverGetSpy.and.returnValue(of({ - category: stubCategory, - categoryPageMetadata: stubcategoryPageMetadata, - products: stubProducts, - })); - const changeCategoryPageSizeAction = new DaffCategoryPageChangePageSize(3); - actions$ = hot('--a', { a: changeCategoryPageSizeAction }); - - expected = cold('--(ab)', { a: productGridLoadSuccessAction, b: categoryLoadSuccessAction }); - expect(effects.changeCategoryPageSize$).toBeObservable(expected); - expect(daffCategoryDriver.get).toHaveBeenCalledWith({ - ...stubcategoryPageMetadata, - kind: DaffCategoryRequestKind.ID, - pageSize: 3, - filterRequests: daffFiltersToRequests(stubcategoryPageMetadata.filters), - }); - }); - }); - - describe('when ChangeCategoryCurrentPage action is triggered', () => { - - let expected; - - it('should call get category with every available parameter', () => { - driverGetSpy.and.returnValue(of({ - category: stubCategory, - categoryPageMetadata: stubcategoryPageMetadata, - products: stubProducts, - })); - const changeCategoryCurrentPageAction = new DaffCategoryPageChangeCurrentPage(3); - actions$ = hot('--a', { a: changeCategoryCurrentPageAction }); - - expected = cold('--(ab)', { a: productGridLoadSuccessAction, b: categoryLoadSuccessAction }); - expect(effects.changeCategoryCurrentPage$).toBeObservable(expected); - expect(daffCategoryDriver.get).toHaveBeenCalledWith({ - ...stubcategoryPageMetadata, - kind: DaffCategoryRequestKind.ID, - currentPage: 3, - filterRequests: daffFiltersToRequests(stubcategoryPageMetadata.filters), - }); - }); - }); - - describe('when ChangeCategorySortAction is triggered', () => { - - let expected; - - it('should call get category with an id, page size, applied filters, and an applied sorting option', () => { - driverGetSpy.and.returnValue(of({ - category: stubCategory, - categoryPageMetadata: stubcategoryPageMetadata, - products: stubProducts, - })); - const changeCategorySortingOption = new DaffCategoryPageChangeSortingOption({ - option: 'option', - direction: DaffSortDirectionEnum.Ascending, - }); - actions$ = hot('--a', { a: changeCategorySortingOption }); - - expected = cold('--(ab)', { a: productGridLoadSuccessAction, b: categoryLoadSuccessAction }); - expect(effects.changeCategorySort$).toBeObservable(expected); - expect(daffCategoryDriver.get).toHaveBeenCalledWith({ - ...stubcategoryPageMetadata, - kind: DaffCategoryRequestKind.ID, - appliedSortDirection: DaffSortDirectionEnum.Ascending, - appliedSortOption: 'option', - filterRequests: daffFiltersToRequests(stubcategoryPageMetadata.filters), - }); - }); - }); }); diff --git a/libs/category/state/src/effects/category-page.effects.ts b/libs/category/state/src/effects/category-page.effects.ts index 6feda48fbc..fa38e79665 100644 --- a/libs/category/state/src/effects/category-page.effects.ts +++ b/libs/category/state/src/effects/category-page.effects.ts @@ -7,10 +7,6 @@ import { createEffect, ofType, } from '@ngrx/effects'; -import { - Store, - select, -} from '@ngrx/store'; import { of, Observable, @@ -18,34 +14,21 @@ import { import { switchMap, catchError, - withLatestFrom, } from 'rxjs/operators'; import { DaffGenericCategory, DaffGetCategoryResponse, - DaffCategoryRequestKind, - DaffCategoryIdRequest, - DaffCategoryPageMetadata, } from '@daffodil/category'; import { DaffCategoryDriver, DaffCategoryServiceInterface, } from '@daffodil/category/driver'; -import { - DaffError, - daffFiltersToRequests, -} from '@daffodil/core'; +import { DaffError } from '@daffodil/core'; import { ErrorTransformer } from '@daffodil/core/state'; import { DaffProduct } from '@daffodil/product'; import { DaffProductGridLoadSuccess } from '@daffodil/product/state'; -import { - DaffCategoryPageChangeCurrentPage, - DaffCategoryPageChangePageSize, - DaffCategoryPageChangeSortingOption, - DaffCategoryPageProductCollectionActionTypes, -} from '../actions/category-page-filter.actions'; import { DaffCategoryPageLoadSuccess, DaffCategoryPageLoad, @@ -54,7 +37,6 @@ import { DaffCategoryPageLoadByUrl, } from '../actions/category-page.actions'; import { DAFF_CATEGORY_ERROR_MATCHER } from '../injection-tokens/public_api'; -import { getDaffCategorySelectors } from '../selectors/category.selector'; @Injectable() export class DaffCategoryPageEffects< @@ -65,20 +47,11 @@ export class DaffCategoryPageEffects< private actions$: Actions, @Inject(DaffCategoryDriver) private driver: DaffCategoryServiceInterface, @Inject(DAFF_CATEGORY_ERROR_MATCHER) private errorMatcher: ErrorTransformer, - private store: Store, ) {} - private categorySelectors = getDaffCategorySelectors(); - loadCategoryPage$: Observable = createEffect(() => this.actions$.pipe( ofType(DaffCategoryPageActionTypes.CategoryPageLoadAction), - switchMap((action: DaffCategoryPageLoad) => this.processCategoryGetRequest(action.request)), - )); - - - loadCategoryPageByUrl$: Observable = createEffect(() => this.actions$.pipe( - ofType(DaffCategoryPageActionTypes.CategoryPageLoadByUrlAction), - switchMap((action: DaffCategoryPageLoadByUrl) => this.driver.getByUrl(action.request).pipe( + switchMap((action: DaffCategoryPageLoad) => this.driver.get(action.request).pipe( switchMap((resp: DaffGetCategoryResponse) => [ new DaffProductGridLoadSuccess(resp.products), new DaffCategoryPageLoadSuccess(resp), @@ -88,55 +61,14 @@ export class DaffCategoryPageEffects< )); - changeCategoryPageSize$: Observable = createEffect(() => this.actions$.pipe( - ofType(DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSizeAction), - withLatestFrom( - this.store.pipe(select(this.categorySelectors.selectCollectionMetadata)), - ), - switchMap(([action, metadata]: [DaffCategoryPageChangePageSize, DaffCategoryPageMetadata]) => this.processCategoryGetRequest({ - ...metadata, - kind: DaffCategoryRequestKind.ID, - filterRequests: daffFiltersToRequests(metadata.filters), - pageSize: action.pageSize, - })), - )); - - - changeCategoryCurrentPage$: Observable = createEffect(() => this.actions$.pipe( - ofType(DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeCurrentPageAction), - withLatestFrom( - this.store.pipe(select(this.categorySelectors.selectCollectionMetadata)), - ), - switchMap(([action, metadata]: [DaffCategoryPageChangeCurrentPage, DaffCategoryPageMetadata]) => this.processCategoryGetRequest({ - ...metadata, - kind: DaffCategoryRequestKind.ID, - filterRequests: daffFiltersToRequests(metadata.filters), - currentPage: action.currentPage, - })), - )); - - - changeCategorySort$: Observable = createEffect(() => this.actions$.pipe( - ofType(DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSortingOptionAction), - withLatestFrom( - this.store.pipe(select(this.categorySelectors.selectCollectionMetadata)), - ), - switchMap(([action, metadata]: [DaffCategoryPageChangeSortingOption, DaffCategoryPageMetadata]) => this.processCategoryGetRequest({ - ...metadata, - kind: DaffCategoryRequestKind.ID, - filterRequests: daffFiltersToRequests(metadata.filters), - appliedSortOption: action.sort.option, - appliedSortDirection: action.sort.direction, - })), - )); - - private processCategoryGetRequest(payload: DaffCategoryIdRequest) { - return this.driver.get(payload).pipe( + loadCategoryPageByUrl$: Observable = createEffect(() => this.actions$.pipe( + ofType(DaffCategoryPageActionTypes.CategoryPageLoadByUrlAction), + switchMap((action: DaffCategoryPageLoadByUrl) => this.driver.getByUrl(action.request).pipe( switchMap((resp: DaffGetCategoryResponse) => [ new DaffProductGridLoadSuccess(resp.products), new DaffCategoryPageLoadSuccess(resp), ]), catchError((error: DaffError) => of(new DaffCategoryPageLoadFailure(this.errorMatcher(error)))), - ); - } + )), + )); }