Skip to content

Commit

Permalink
feat(category): combine all metadata effects (#3294)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 18, 2024
1 parent cf4d2f7 commit 887c839
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 163 deletions.
4 changes: 2 additions & 2 deletions libs/category/state/src/category-state.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import {
DaffCategoryPageApplyFilters,
DaffCategoryPageClearFilters,
DaffCategoryPageRemoveFilters,
DaffCategoryPageChangePageSize,
DaffCategoryPageChangeCurrentPage,
DaffCategoryPageChangeSortingOption,
} from '@daffodil/category/state';
import {
DaffCategoryStateTestingModule,
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -64,9 +68,9 @@ class MockError extends DaffInheritableError implements DaffError {
}
}

describe('DaffCategoryPageFilterEffects', () => {
describe('DaffCategoryPageMetadataEffects', () => {
let actions$: Observable<any>;
let effects: DaffCategoryPageFilterEffects<DaffCategory, DaffProduct>;
let effects: DaffCategoryPageMetadataEffects<DaffCategory, DaffProduct>;
let daffCategoryDriver: DaffCategoryServiceInterface;
let facade: MockDaffCollectionFacade;

Expand Down Expand Up @@ -112,7 +116,7 @@ describe('DaffCategoryPageFilterEffects', () => {
};

expectObservable(
effects.updateFilters$(300, testScheduler),
effects.update$(300, testScheduler),
).toBe(expectedMarble, expectedValue);
});

Expand Down Expand Up @@ -162,7 +166,7 @@ describe('DaffCategoryPageFilterEffects', () => {
};

expectObservable(
effects.updateFilters$(0, testScheduler),
effects.update$(0, testScheduler),
).toBe(expectedMarble, expectedValue);
});

Expand Down Expand Up @@ -205,7 +209,7 @@ describe('DaffCategoryPageFilterEffects', () => {
};

expectObservable(
effects.updateFilters$(0, testScheduler),
effects.update$(0, testScheduler),
).toBe(expectedMarble, expectedValue);
});

Expand All @@ -229,14 +233,14 @@ describe('DaffCategoryPageFilterEffects', () => {
DaffCategoryStateTestingModule,
],
providers: [
DaffCategoryPageFilterEffects,
DaffCategoryPageMetadataEffects,
provideMockActions(() => actions$),
],
});

facade = TestBed.inject(MockDaffCollectionFacade);
daffCategoryDriver = TestBed.inject<DaffCategoryServiceInterface>(DaffCategoryDriver);
effects = TestBed.inject(DaffCategoryPageFilterEffects);
effects = TestBed.inject(DaffCategoryPageMetadataEffects);

categoryFactory = TestBed.inject(DaffCategoryFactory);
categoryPageMetadataFactory = TestBed.inject(DaffCategoryPageMetadataFactory);
Expand Down Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<V>,
W extends DaffProduct
> {
Expand All @@ -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
Expand All @@ -85,6 +85,9 @@ export class DaffCategoryPageFilterEffects<
DaffCategoryPageProductCollectionActionTypes.CategoryPageClearFiltersAction,
DaffCategoryPageProductCollectionActionTypes.CategoryPageRemoveFiltersAction,
DaffCategoryPageProductCollectionActionTypes.CategoryPageToggleFilterAction,
DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSizeAction,
DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeCurrentPageAction,
DaffCategoryPageProductCollectionActionTypes.CategoryPageChangeSortingOptionAction,
),
withLatestFrom(this.facade.metadata$),
map((
Expand Down
76 changes: 0 additions & 76 deletions libs/category/state/src/effects/category-page.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
});
});
});
82 changes: 7 additions & 75 deletions libs/category/state/src/effects/category-page.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,28 @@ import {
createEffect,
ofType,
} from '@ngrx/effects';
import {
Store,
select,
} from '@ngrx/store';
import {
of,
Observable,
} from 'rxjs';
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,
Expand All @@ -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<
Expand All @@ -65,20 +47,11 @@ export class DaffCategoryPageEffects<
private actions$: Actions,
@Inject(DaffCategoryDriver) private driver: DaffCategoryServiceInterface<V, W>,
@Inject(DAFF_CATEGORY_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
private store: Store<any>,
) {}

private categorySelectors = getDaffCategorySelectors<V, W>();

loadCategoryPage$: Observable<any> = createEffect(() => this.actions$.pipe(
ofType(DaffCategoryPageActionTypes.CategoryPageLoadAction),
switchMap((action: DaffCategoryPageLoad) => this.processCategoryGetRequest(action.request)),
));


loadCategoryPageByUrl$: Observable<any> = 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<V, W>) => [
new DaffProductGridLoadSuccess(resp.products),
new DaffCategoryPageLoadSuccess(resp),
Expand All @@ -88,55 +61,14 @@ export class DaffCategoryPageEffects<
));


changeCategoryPageSize$: Observable<any> = 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<any> = 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<any> = 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<any> = createEffect(() => this.actions$.pipe(
ofType(DaffCategoryPageActionTypes.CategoryPageLoadByUrlAction),
switchMap((action: DaffCategoryPageLoadByUrl) => this.driver.getByUrl(action.request).pipe(
switchMap((resp: DaffGetCategoryResponse<V, W>) => [
new DaffProductGridLoadSuccess(resp.products),
new DaffCategoryPageLoadSuccess(resp),
]),
catchError((error: DaffError) => of(new DaffCategoryPageLoadFailure(this.errorMatcher(error)))),
);
}
)),
));
}

0 comments on commit 887c839

Please sign in to comment.