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(category): make category page configuration model generic #783

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions libs/category/src/actions/category.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export class DaffCategoryLoad<T extends DaffCategoryRequest> implements Action {
*
* @param response - DaffGetCategoryResponse object
*/
export class DaffCategoryLoadSuccess<T extends DaffGenericCategory<T>, U extends DaffCategoryPageConfigurationState> implements Action {
export class DaffCategoryLoadSuccess<T extends DaffCategoryRequest, V extends DaffGenericCategory<V>, U extends DaffCategoryPageConfigurationState<T>> implements Action {
readonly type = DaffCategoryActionTypes.CategoryLoadSuccessAction;

constructor(public response: DaffGetCategoryResponse<T, U>) { }
constructor(public response: DaffGetCategoryResponse<T, V, U>) { }
}

/**
Expand Down Expand Up @@ -113,10 +113,10 @@ export class DaffToggleCategoryFilter implements Action {
export type DaffCategoryActions<
T extends DaffCategoryRequest,
U extends DaffGenericCategory<U>,
V extends DaffCategoryPageConfigurationState
V extends DaffCategoryPageConfigurationState<T>
> =
| DaffCategoryLoad<T>
| DaffCategoryLoadSuccess<U, V>
| DaffCategoryLoadSuccess<T, U, V>
| DaffCategoryLoadFailure
| DaffChangeCategoryPageSize
| DaffChangeCategoryCurrentPage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { DaffGenericCategory } from '../../models/generic-category';

export interface DaffCategoryServiceInterface<
T extends DaffCategoryRequest,
U extends DaffGenericCategory<U>,
V extends DaffCategoryPageConfigurationState
V extends DaffGenericCategory<V>,
U extends DaffCategoryPageConfigurationState<T>
> {
get(categoryRequest: T): Observable<DaffGetCategoryResponse<U, V>>;
get(categoryRequest: T): Observable<DaffGetCategoryResponse<T, V, U>>;
}
4 changes: 2 additions & 2 deletions libs/category/src/drivers/magento/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { DaffCategoryPageConfigurationState } from '../../models/category-page-c
@Injectable({
providedIn: 'root'
})
export class DaffMagentoCategoryService implements DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState> {
export class DaffMagentoCategoryService implements DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>> {

constructor(
private apollo: Apollo,
Expand All @@ -40,7 +40,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface<
* Gets a category based on parameters. Default current_page is 1, and default page_size is 20.
* @param categoryRequest A DaffCategoryRequest object.
*/
get(categoryRequest: DaffCategoryRequest): Observable<DaffGetCategoryResponse<DaffCategory, DaffCategoryPageConfigurationState>> {
get(categoryRequest: DaffCategoryRequest): Observable<DaffGetCategoryResponse<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>>> {
return combineLatest([
this.apollo.query<MagentoGetACategoryResponse>({
query: MagentoGetCategoryQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MagentoSortFields } from '../models/sort-fields';
import { MagentoCompleteCategoryResponse } from '../models/complete-category-response';
import { DaffCategoryFromToFilterSeparator } from 'libs/category/src/models/requests/filter-request';
import { DaffCategoryFilterType } from '../../../models/category-filter-base';
import { DaffCategoryRequest } from '../../../models/requests/category-request';

describe('DaffMagentoCategoryPageConfigTransformerService', () => {

Expand All @@ -21,7 +22,7 @@ describe('DaffMagentoCategoryPageConfigTransformerService', () => {
const stubCategory: DaffCategory = categoryFactory.create();

const categoryPageConfigurationStateFactory: DaffCategoryPageConfigurationStateFactory = new DaffCategoryPageConfigurationStateFactory();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState = categoryPageConfigurationStateFactory.create();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState<DaffCategoryRequest> = categoryPageConfigurationStateFactory.create();
delete stubCategoryPageConfigurationState.filter_requests;
delete stubCategoryPageConfigurationState.applied_sort_direction;
delete stubCategoryPageConfigurationState.applied_sort_option;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { MagentoSortFields } from '../models/sort-fields';
import { MagentoCompleteCategoryResponse } from '../models/complete-category-response';
import { DaffCategoryFromToFilterSeparator } from '../../../models/requests/filter-request';
import { DaffCategoryFilterType } from '../../../models/category-filter-base';
import { DaffCategoryRequest } from '../../../models/requests/category-request';

@Injectable({
providedIn: 'root'
})
export class DaffMagentoCategoryPageConfigTransformerService {

transform(categoryResponse: MagentoCompleteCategoryResponse): DaffCategoryPageConfigurationState {
transform(categoryResponse: MagentoCompleteCategoryResponse): DaffCategoryPageConfigurationState<DaffCategoryRequest> {
return {
id: categoryResponse.category.id,
page_size: categoryResponse.page_info.page_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import { MagentoPageInfo } from '../models/page-info';
import { MagentoSortFields } from '../models/sort-fields';
import { DaffMagentoCategoryTransformerService } from './category-transformer.service';
import { MagentoCompleteCategoryResponse } from '../models/complete-category-response';
import { DaffCategoryRequest } from '../../../models/requests/category-request';

describe('DaffMagentoCategoryResponseTransformService', () => {

let service: DaffMagentoCategoryResponseTransformService;
const categoryFactory: DaffCategoryFactory = new DaffCategoryFactory();
const stubCategory: DaffCategory = categoryFactory.create();
const categoryPageConfigurationStateFactory: DaffCategoryPageConfigurationStateFactory = new DaffCategoryPageConfigurationStateFactory();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState = categoryPageConfigurationStateFactory.create();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState<DaffCategoryRequest> = categoryPageConfigurationStateFactory.create();
const productFactory: DaffProductFactory = new DaffProductFactory();
const stubProducts: DaffProduct[] = productFactory.createMany(4);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DaffGetCategoryResponse } from '../../../models/get-category-response';
import { DaffMagentoCategoryTransformerService } from './category-transformer.service';
import { DaffCategory } from '../../../models/category';
import { DaffCategoryPageConfigurationState } from '../../../models/category-page-configuration-state';
import { DaffCategoryRequest } from '../../../models/requests/category-request';

@Injectable({
providedIn: 'root'
Expand All @@ -20,7 +21,7 @@ export class DaffMagentoCategoryResponseTransformService {
private magentoProductTransformerService: DaffMagentoProductTransformerService
) {}

transform(completeCategory: MagentoCompleteCategoryResponse): DaffGetCategoryResponse<DaffCategory, DaffCategoryPageConfigurationState> {
transform(completeCategory: MagentoCompleteCategoryResponse): DaffGetCategoryResponse<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>> {
return {
...{ magentoCompleteCategoryResponse: completeCategory },
category: this.magentoCategoryTransformerService.transform(completeCategory.category),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { DaffCategoryPageConfigurationState } from '../../../models/category-pag
import { MagentoCustomAttributeMetadataResponse } from '../models/custom-attribute-metadata-response';
import { MagentoAggregation } from '../models/aggregation';
import { MagentoGetCategoryAggregationsResponse } from '../models/get-category-aggregations-response';
import { DaffCategoryRequest } from '../../../models/requests/category-request';

describe('Custom Metadata Attributes Methods', () => {

const categoryPageConfigurationStateFactory: DaffCategoryPageConfigurationStateFactory = new DaffCategoryPageConfigurationStateFactory();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState = categoryPageConfigurationStateFactory.create();
const stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState<DaffCategoryRequest> = categoryPageConfigurationStateFactory.create();
delete stubCategoryPageConfigurationState.filter_requests;
delete stubCategoryPageConfigurationState.applied_sort_direction;
delete stubCategoryPageConfigurationState.applied_sort_option;
Expand Down
10 changes: 5 additions & 5 deletions libs/category/src/effects/category.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ import { DaffCategoryFilterEqualRequest } from '../models/requests/filter-reques
import { DaffCategoryFilterType } from '../models/category-filter-base';
import { daffCategoryReducers } from '../reducers/category-reducers';

class MockCategoryDriver implements DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState> {
class MockCategoryDriver implements DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>> {
get(categoryRequest: any): Observable<any> {
return null;
}
}

describe('DaffCategoryEffects', () => {
let actions$: Observable<any>;
let effects: DaffCategoryEffects<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState>;
let effects: DaffCategoryEffects<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>>;
let stubCategory: DaffCategory;
let stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState;
let stubCategoryPageConfigurationState: DaffCategoryPageConfigurationState<DaffCategoryRequest>;
let stubProducts: DaffProduct[];
let daffCategoryDriver: DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState>;
let daffCategoryDriver: DaffCategoryServiceInterface<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>>;
const daffCategoryDriverSpy = jasmine.createSpyObj('DaffCategoryDriver', ['get']);
let store: MockStore<any>;

let categoryFactory: DaffCategoryFactory;
let categoryPageConfigurationStateFactory: DaffCategoryPageConfigurationStateFactory;
let productFactory: DaffProductFactory;
let categoryLoadSuccessAction: DaffCategoryLoadSuccess<DaffCategory, DaffCategoryPageConfigurationState>;
let categoryLoadSuccessAction: DaffCategoryLoadSuccess<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>>;
let productGridLoadSuccessAction: DaffProductGridLoadSuccess;

beforeEach(() => {
Expand Down
10 changes: 5 additions & 5 deletions libs/category/src/effects/category.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ import { DaffGenericCategory } from '../models/generic-category';
@Injectable()
export class DaffCategoryEffects<
T extends DaffCategoryRequest,
U extends DaffGenericCategory<U>,
V extends DaffCategoryPageConfigurationState
V extends DaffGenericCategory<V>,
U extends DaffCategoryPageConfigurationState<T>
> {

constructor(
private actions$: Actions,
@Inject(DaffCategoryDriver) private driver: DaffCategoryServiceInterface<T, U, V>,
@Inject(DaffCategoryDriver) private driver: DaffCategoryServiceInterface<T, V, U>,
private store: Store<any>
){}

private categorySelectors = getDaffCategorySelectors<U, V>();
private categorySelectors = getDaffCategorySelectors<T, V, U>();

@Effect()
loadCategory$ : Observable<any> = this.actions$.pipe(
Expand Down Expand Up @@ -183,7 +183,7 @@ export class DaffCategoryEffects<

private processCategoryGetRequest(payload: T) {
return this.driver.get(payload).pipe(
switchMap((resp: DaffGetCategoryResponse<U, V>) => [
switchMap((resp: DaffGetCategoryResponse<T, V, U>) => [
new DaffProductGridLoadSuccess(resp.products),
new DaffCategoryLoadSuccess(resp)
]),
Expand Down
5 changes: 3 additions & 2 deletions libs/category/src/facades/category.facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import { daffCategoryReducers } from '../reducers/category-reducers';
import { DaffCategory } from '../models/category';
import { DaffCategoryPageConfigurationState } from '../models/category-page-configuration-state';
import { DaffCategoryFilterType } from '../models/category-filter-base';
import { DaffCategoryRequest } from '../models/requests/category-request';

describe('DaffCategoryFacade', () => {
let store: MockStore<any>;
let facade: DaffCategoryFacade<DaffCategory, DaffCategoryPageConfigurationState>;
let facade: DaffCategoryFacade<DaffCategoryRequest, DaffCategory, DaffCategoryPageConfigurationState<DaffCategoryRequest>>;
const categoryFactory: DaffCategoryFactory = new DaffCategoryFactory();
const categoryPageConfigurationFactory: DaffCategoryPageConfigurationStateFactory = new DaffCategoryPageConfigurationStateFactory();
const productFactory: DaffProductFactory = new DaffProductFactory();
let category: DaffCategory;
let categoryPageConfigurationState: DaffCategoryPageConfigurationState;
let categoryPageConfigurationState: DaffCategoryPageConfigurationState<DaffCategoryRequest>;
let product: DaffProductUnion;

beforeEach(() => {
Expand Down
14 changes: 7 additions & 7 deletions libs/category/src/facades/category.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DaffCategoryReducersState } from '../reducers/category-reducers.interfa
import { DaffCategoryPageConfigurationState } from '../models/category-page-configuration-state';
import { DaffCategoryFilter } from '../models/category-filter';
import { DaffCategorySortOption } from '../models/category-sort-option';
import { DaffSortDirectionEnum } from '../models/requests/category-request';
import { DaffSortDirectionEnum, DaffCategoryRequest } from '../models/requests/category-request';
import { DaffCategoryAppliedFilter } from '../models/category-applied-filter';
import { DaffGenericCategory } from '../models/generic-category';

Expand All @@ -21,17 +21,17 @@ import { DaffGenericCategory } from '../models/generic-category';
@Injectable({
providedIn: DaffCategoryModule
})
export class DaffCategoryFacade<T extends DaffGenericCategory<T>, V extends DaffCategoryPageConfigurationState> implements DaffStoreFacade<Action> {
private categorySelectors = getDaffCategorySelectors<T, V>();
export class DaffCategoryFacade<T extends DaffCategoryRequest, V extends DaffGenericCategory<V>, U extends DaffCategoryPageConfigurationState<T>> implements DaffStoreFacade<Action> {
private categorySelectors = getDaffCategorySelectors<T, V, U>();

/**
* The currently selected category.
*/
category$: Observable<T>;
category$: Observable<V>;
/**
* The page configuration state for the selected category.
*/
pageConfigurationState$: Observable<V>;
pageConfigurationState$: Observable<U>;
/**
* The current page of products for the selected category.
*/
Expand Down Expand Up @@ -93,7 +93,7 @@ export class DaffCategoryFacade<T extends DaffGenericCategory<T>, V extends Daff
* Get a category by the provided Id.
* @param id
*/
getCategoryById(id: string): Observable<T> {
getCategoryById(id: string): Observable<V> {
return this.store.pipe(select(this.categorySelectors.selectCategory, {id: id}));
}

Expand All @@ -113,7 +113,7 @@ export class DaffCategoryFacade<T extends DaffGenericCategory<T>, V extends Daff
return this.store.pipe(select(this.categorySelectors.selectTotalProductsByCategory, {id: categoryId}))
}

constructor(private store: Store<DaffCategoryReducersState<T, V>>) {
constructor(private store: Store<DaffCategoryReducersState<T, V, U>>) {
this.category$ = this.store.pipe(select(this.categorySelectors.selectSelectedCategory));
this.products$ = this.store.pipe(select(this.categorySelectors.selectCategoryPageProducts));
this.totalProducts$ = this.store.pipe(select(this.categorySelectors.selectCategoryPageTotalProducts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DaffCategoryFilter } from './category-filter';
import { DaffCategorySortOption } from './category-sort-option';
import { DaffCategoryRequest } from './requests/category-request';

export interface DaffCategoryPageConfigurationState extends DaffCategoryRequest {
export type DaffCategoryPageConfigurationState<T extends DaffCategoryRequest> = T & {
Copy link
Contributor Author

@lderrickable lderrickable Apr 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change. In typescript you can't do DaffCategoryPageConfigurationState<T extends DaffCategoyRequest> extends T (microsoft/TypeScript#2225)

filters: DaffCategoryFilter[];
sort_options: DaffCategorySortOption[];
total_pages: number;
Expand Down
8 changes: 5 additions & 3 deletions libs/category/src/models/get-category-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { DaffProductUnion } from '@daffodil/product';

import { DaffCategoryPageConfigurationState } from './category-page-configuration-state';
import { DaffGenericCategory } from './generic-category';
import { DaffCategoryRequest } from './requests/category-request';

export interface DaffGetCategoryResponse<
T extends DaffGenericCategory<T>,
U extends DaffCategoryPageConfigurationState
T extends DaffCategoryRequest,
V extends DaffGenericCategory<V>,
U extends DaffCategoryPageConfigurationState<T>
> {
products: DaffProductUnion[];
category: T;
category: V;
categoryPageConfigurationState: U;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { DaffCategoryRequest } from '../../models/requests/category-request';
import { DaffCategoryPageConfigurationState } from '../../models/category-page-configuration-state';
import { DaffGenericCategory } from '../../models/generic-category';

export function daffCategoryEntitiesReducer<T extends DaffCategoryRequest, U extends DaffGenericCategory<U>, V extends DaffCategoryPageConfigurationState>(
state = daffCategoryEntitiesAdapter<U>().getInitialState(),
action: DaffCategoryActions<T, U, V>
): EntityState<U> {
export function daffCategoryEntitiesReducer<T extends DaffCategoryRequest, V extends DaffGenericCategory<V>, U extends DaffCategoryPageConfigurationState<T>>(
state = daffCategoryEntitiesAdapter<V>().getInitialState(),
action: DaffCategoryActions<T, V, U>
): EntityState<V> {
switch (action.type) {
case DaffCategoryActionTypes.CategoryLoadSuccessAction:
return daffCategoryEntitiesAdapter<U>().upsertOne(
return daffCategoryEntitiesAdapter<V>().upsertOne(
{
id: action.response.category.id,
...action.response.category
Expand Down
7 changes: 4 additions & 3 deletions libs/category/src/reducers/category-reducers.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { EntityState } from '@ngrx/entity';
import { DaffCategoryReducerState } from '../reducers/category/category-reducer-state.interface';
import { DaffCategoryPageConfigurationState } from '../models/category-page-configuration-state';
import { DaffGenericCategory } from '../models/generic-category';
import { DaffCategoryRequest } from '../models/requests/category-request';

export interface DaffCategoryReducersState<T extends DaffGenericCategory<T>, U extends DaffCategoryPageConfigurationState> {
category: DaffCategoryReducerState<U>;
categoryEntities: EntityState<T>;
export interface DaffCategoryReducersState<T extends DaffCategoryRequest, V extends DaffGenericCategory<V>, U extends DaffCategoryPageConfigurationState<T>> {
category: DaffCategoryReducerState<T, U>;
categoryEntities: EntityState<V>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DaffCategoryPageConfigurationState } from '../../models/category-page-configuration-state';
import { DaffCategoryRequest } from '../../models/requests/category-request';

export interface DaffCategoryReducerState<T extends DaffCategoryPageConfigurationState> {
categoryPageConfigurationState: T,
export interface DaffCategoryReducerState<T extends DaffCategoryRequest, V extends DaffCategoryPageConfigurationState<T>> {
categoryPageConfigurationState: V,
categoryLoading: boolean,
productsLoading: boolean,
errors: string[]
Expand Down
Loading