Skip to content

Commit

Permalink
feat: allow strings as propagate index
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Apr 4, 2022
1 parent 95a91ab commit 71742dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/app/core/directives/product-context.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ProductCompletenessLevel, SkuQuantityType } from 'ish-core/models/produ
})
export class ProductContextDirective implements OnInit, OnChanges, OnDestroy {
@Input() completeness: 'List' | 'Detail' = 'List';
@Input() propagateIndex: number;
@Input() propagateIndex: number | string;

@Output() skuChange = this.context.select('sku');
@Output() quantityChange = this.context.select('quantity');
Expand Down
5 changes: 3 additions & 2 deletions src/app/core/facades/product-context.facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Product Context Facade', () => {
Object {
"allowZeroQuantity": false,
"categoryId": null,
"children": Object {},
"displayProperties": Object {},
"propagateActive": true,
"requiredCompletenessLevel": 2,
Expand Down Expand Up @@ -104,7 +105,7 @@ describe('Product Context Facade', () => {
Object {
"allowZeroQuantity": false,
"categoryId": null,
"children": undefined,
"children": Object {},
"hasProductError": true,
"hasQuantityError": false,
"label": null,
Expand Down Expand Up @@ -175,7 +176,7 @@ describe('Product Context Facade', () => {
Object {
"allowZeroQuantity": false,
"categoryId": null,
"children": undefined,
"children": Object {},
"hasProductError": false,
"hasQuantityError": false,
"label": null,
Expand Down
30 changes: 22 additions & 8 deletions src/app/core/facades/product-context.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { TranslateService } from '@ngx-translate/core';
import { RxState } from '@rx-angular/state';
import { isEqual } from 'lodash-es';
import { BehaviorSubject, Observable, combineLatest } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, first, map, skip, startWith, switchMap } from 'rxjs/operators';
import {
debounceTime,
distinctUntilChanged,
filter,
first,
map,
skip,
skipWhile,
startWith,
switchMap,
} from 'rxjs/operators';

import { AttributeGroupTypes } from 'ish-core/models/attribute-group/attribute-group.types';
import { Image } from 'ish-core/models/image/image.model';
Expand Down Expand Up @@ -101,7 +111,7 @@ export interface ProductContext {

// child contexts
propagateActive: boolean;
children: ProductContext[];
children: Record<string | number, ProductContext>;
}

@Injectable()
Expand Down Expand Up @@ -131,6 +141,7 @@ export class ProductContextFacade extends RxState<ProductContext> {
requiredCompletenessLevel: ProductCompletenessLevel.List,
propagateActive: true,
allowZeroQuantity: false,
children: {},
// eslint-disable-next-line unicorn/no-null
categoryId: null,
});
Expand All @@ -146,7 +157,6 @@ export class ProductContextFacade extends RxState<ProductContext> {
map(product => ({
product,
loading: false,
children: undefined,
})),
startWith({ loading: true })
)
Expand Down Expand Up @@ -231,9 +241,11 @@ export class ProductContextFacade extends RxState<ProductContext> {
this.connect(
'hasQuantityError',
this.select('children').pipe(
map(children => Object.values(children)),
skipWhile(children => !children?.length),
map(
children =>
!children?.filter(x => !!x).length || children.filter(x => !!x).some(child => child.hasQuantityError)
!children.filter(x => !!x).length || children.filter(x => !!x).some(child => child.hasQuantityError)
),
distinctUntilChanged()
)
Expand All @@ -260,9 +272,11 @@ export class ProductContextFacade extends RxState<ProductContext> {
this.connect(
'hasProductError',
this.select('children').pipe(
map(children => Object.values(children)),
skipWhile(children => !children?.length),
map(
children =>
!children?.filter(x => !!x).length || children.filter(x => !!x).some(child => child.hasProductError)
!children.filter(x => !!x).length || children.filter(x => !!x).some(child => child.hasProductError)
),
distinctUntilChanged()
)
Expand Down Expand Up @@ -393,7 +407,7 @@ export class ProductContextFacade extends RxState<ProductContext> {
}

addToBasket() {
const items: SkuQuantityType[] = this.get('children') || this.get('parts');
const items: SkuQuantityType[] = Object.values(this.get('children')) || this.get('parts');
if (items && !ProductHelper.isProductBundle(this.get('product'))) {
items
.filter(x => !!x && !!x.quantity)
Expand All @@ -405,9 +419,9 @@ export class ProductContextFacade extends RxState<ProductContext> {
}
}

propagate(index: number, childState: ProductContext) {
propagate(index: number | string, childState: ProductContext) {
this.set('children', state => {
const current = [...(state.children || [])];
const current = { ...state.children };
current[index] = childState;
return current;
});
Expand Down

0 comments on commit 71742dd

Please sign in to comment.