|
@@ -3,11 +3,13 @@ import {
|
|
|
ChangeDetectorRef,
|
|
ChangeDetectorRef,
|
|
|
Component,
|
|
Component,
|
|
|
Input,
|
|
Input,
|
|
|
|
|
+ OnChanges,
|
|
|
OnDestroy,
|
|
OnDestroy,
|
|
|
OnInit,
|
|
OnInit,
|
|
|
|
|
+ SimpleChanges,
|
|
|
} from '@angular/core';
|
|
} from '@angular/core';
|
|
|
import { FormArray, FormGroup } from '@angular/forms';
|
|
import { FormArray, FormGroup } from '@angular/forms';
|
|
|
-import { ProductWithVariants } from '@vendure/admin-ui/core';
|
|
|
|
|
|
|
+import { flattenFacetValues, ProductWithVariants } from '@vendure/admin-ui/core';
|
|
|
import { Subscription } from 'rxjs';
|
|
import { Subscription } from 'rxjs';
|
|
|
import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
|
|
import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
|
|
|
|
|
|
|
@@ -17,11 +19,13 @@ import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
|
|
|
styleUrls: ['./product-variants-table.component.scss'],
|
|
styleUrls: ['./product-variants-table.component.scss'],
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
})
|
|
})
|
|
|
-export class ProductVariantsTableComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
+export class ProductVariantsTableComponent implements OnInit, OnChanges, OnDestroy {
|
|
|
@Input('productVariantsFormArray') formArray: FormArray;
|
|
@Input('productVariantsFormArray') formArray: FormArray;
|
|
|
@Input() variants: ProductWithVariants.Variants[];
|
|
@Input() variants: ProductWithVariants.Variants[];
|
|
|
|
|
+ @Input() channelPriceIncludesTax: boolean;
|
|
|
@Input() optionGroups: ProductWithVariants.OptionGroups[];
|
|
@Input() optionGroups: ProductWithVariants.OptionGroups[];
|
|
|
formGroupMap = new Map<string, FormGroup>();
|
|
formGroupMap = new Map<string, FormGroup>();
|
|
|
|
|
+ variantListPrice: { [variantId: string]: number } = {};
|
|
|
private subscription: Subscription;
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
constructor(private changeDetector: ChangeDetectorRef) {}
|
|
constructor(private changeDetector: ChangeDetectorRef) {}
|
|
@@ -40,6 +44,17 @@ export class ProductVariantsTableComponent implements OnInit, OnDestroy {
|
|
|
this.buildFormGroupMap();
|
|
this.buildFormGroupMap();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ ngOnChanges(changes: SimpleChanges) {
|
|
|
|
|
+ if ('variants' in changes) {
|
|
|
|
|
+ if (this.channelPriceIncludesTax != null && Object.keys(this.variantListPrice).length === 0) {
|
|
|
|
|
+ this.buildVariantListPrices(this.variants);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if ('channelPriceIncludesTax' in changes) {
|
|
|
|
|
+ this.buildVariantListPrices(this.variants);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
ngOnDestroy() {
|
|
ngOnDestroy() {
|
|
|
if (this.subscription) {
|
|
if (this.subscription) {
|
|
|
this.subscription.unsubscribe();
|
|
this.subscription.unsubscribe();
|
|
@@ -51,6 +66,21 @@ export class ProductVariantsTableComponent implements OnInit, OnDestroy {
|
|
|
return group && group.name;
|
|
return group && group.name;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ updateVariantListPrice(price, variantId: string, group: FormGroup) {
|
|
|
|
|
+ // Why do this and not just use a conditional `formControlName` or `formControl`
|
|
|
|
|
+ // binding in the template? It breaks down when switching between Channels and
|
|
|
|
|
+ // the values no longer get updated. There seem to some lifecycle/memory-clean-up
|
|
|
|
|
+ // issues with Angular forms at the moment, which will hopefully be fixed soon.
|
|
|
|
|
+ // See https://github.com/angular/angular/issues/20007
|
|
|
|
|
+ this.variantListPrice[variantId] = price;
|
|
|
|
|
+ const controlName = this.channelPriceIncludesTax ? 'priceWithTax' : 'price';
|
|
|
|
|
+ const control = group.get(controlName);
|
|
|
|
|
+ if (control) {
|
|
|
|
|
+ control.setValue(price);
|
|
|
|
|
+ control.markAsDirty();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private buildFormGroupMap() {
|
|
private buildFormGroupMap() {
|
|
|
this.formGroupMap.clear();
|
|
this.formGroupMap.clear();
|
|
|
for (const controlGroup of this.formArray.controls) {
|
|
for (const controlGroup of this.formArray.controls) {
|
|
@@ -58,4 +88,12 @@ export class ProductVariantsTableComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
}
|
|
|
this.changeDetector.markForCheck();
|
|
this.changeDetector.markForCheck();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private buildVariantListPrices(variants?: ProductWithVariants.Variants[]) {
|
|
|
|
|
+ if (variants) {
|
|
|
|
|
+ this.variantListPrice = variants.reduce((prices, v) => {
|
|
|
|
|
+ return { ...prices, [v.id]: this.channelPriceIncludesTax ? v.priceWithTax : v.price };
|
|
|
|
|
+ }, {});
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|