|
|
@@ -1,11 +1,11 @@
|
|
|
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
|
|
-import { UntypedFormControl } from '@angular/forms';
|
|
|
-import { DefaultFormComponentId } from '@vendure/common/lib/shared-types';
|
|
|
+import { FormControl } from '@angular/forms';
|
|
|
+import { DefaultFormComponentId, DefaultFormComponentUiConfig } from '@vendure/common/lib/shared-types';
|
|
|
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
|
|
|
import { forkJoin, Observable, of } from 'rxjs';
|
|
|
import { map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
|
|
-import { FormInputComponent, InputComponentConfig } from '../../../common/component-registry-types';
|
|
|
+import { FormInputComponent } from '../../../common/component-registry-types';
|
|
|
import { GetProductVariantQuery, ProductSelectorSearchQuery } from '../../../common/generated-types';
|
|
|
import { DataService } from '../../../data/providers/data.service';
|
|
|
|
|
|
@@ -27,8 +27,8 @@ export class ProductSelectorFormInputComponent implements FormInputComponent, On
|
|
|
static readonly id: DefaultFormComponentId = 'product-selector-form-input';
|
|
|
readonly isListInput = true;
|
|
|
readonly: boolean;
|
|
|
- formControl: UntypedFormControl;
|
|
|
- config: InputComponentConfig;
|
|
|
+ formControl: FormControl<Array<string | { id: string }>>;
|
|
|
+ config: DefaultFormComponentUiConfig<'product-selector-form-input'>;
|
|
|
selection$: Observable<Array<GetProductVariantQuery['productVariant']>>;
|
|
|
|
|
|
constructor(private dataService: DataService) {}
|
|
|
@@ -52,7 +52,7 @@ export class ProductSelectorFormInputComponent implements FormInputComponent, On
|
|
|
return forkJoin(
|
|
|
value.map(id =>
|
|
|
this.dataService.product
|
|
|
- .getProductVariant(id)
|
|
|
+ .getProductVariant(this.getId(id))
|
|
|
.mapSingle(data => data.productVariant),
|
|
|
),
|
|
|
);
|
|
|
@@ -66,10 +66,16 @@ export class ProductSelectorFormInputComponent implements FormInputComponent, On
|
|
|
addProductVariant(product: ProductSelectorSearchQuery['search']['items'][number]) {
|
|
|
const value = this.formControl.value as string[];
|
|
|
this.formControl.setValue([...new Set([...value, product.productVariantId])]);
|
|
|
+ this.formControl.markAsDirty();
|
|
|
}
|
|
|
|
|
|
removeProductVariant(id: string) {
|
|
|
- const value = this.formControl.value as string[];
|
|
|
- this.formControl.setValue(value.filter(_id => _id !== id));
|
|
|
+ const value = this.formControl.value;
|
|
|
+ this.formControl.setValue(value.map(this.getId).filter(_id => _id !== id));
|
|
|
+ this.formControl.markAsDirty();
|
|
|
+ }
|
|
|
+
|
|
|
+ private getId(value: (typeof this.formControl.value)[number]) {
|
|
|
+ return typeof value === 'string' ? value : value.id;
|
|
|
}
|
|
|
}
|