|
@@ -1,8 +1,10 @@
|
|
|
-import { ChangeDetectionStrategy, Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';
|
|
|
|
|
-import { NgSelectComponent } from '@ng-select/ng-select';
|
|
|
|
|
-import { CurrencyCode, DataService, LocalStorageService, SearchForTestOrder } from '@vendure/admin-ui/core';
|
|
|
|
|
-import { concat, merge, Observable, of, Subject } from 'rxjs';
|
|
|
|
|
-import { debounceTime, distinctUntilChanged, mapTo, switchMap, tap } from 'rxjs/operators';
|
|
|
|
|
|
|
+import { ChangeDetectionStrategy, Component, EventEmitter, OnInit, Output } from '@angular/core';
|
|
|
|
|
+import {
|
|
|
|
|
+ CurrencyCode,
|
|
|
|
|
+ DataService,
|
|
|
|
|
+ LocalStorageService,
|
|
|
|
|
+ ProductSelectorSearch,
|
|
|
|
|
+} from '@vendure/admin-ui/core';
|
|
|
|
|
|
|
|
export interface TestOrderLine {
|
|
export interface TestOrderLine {
|
|
|
id: string;
|
|
id: string;
|
|
@@ -21,19 +23,12 @@ export interface TestOrderLine {
|
|
|
})
|
|
})
|
|
|
export class TestOrderBuilderComponent implements OnInit {
|
|
export class TestOrderBuilderComponent implements OnInit {
|
|
|
@Output() orderLinesChange = new EventEmitter<TestOrderLine[]>();
|
|
@Output() orderLinesChange = new EventEmitter<TestOrderLine[]>();
|
|
|
-
|
|
|
|
|
lines: TestOrderLine[] = [];
|
|
lines: TestOrderLine[] = [];
|
|
|
currencyCode: CurrencyCode;
|
|
currencyCode: CurrencyCode;
|
|
|
- searchInput$ = new Subject<string>();
|
|
|
|
|
- resultSelected$ = new Subject<void>();
|
|
|
|
|
- searchLoading = false;
|
|
|
|
|
- searchResults$: Observable<SearchForTestOrder.Items[]>;
|
|
|
|
|
- @ViewChild('autoComplete', { static: true })
|
|
|
|
|
- private ngSelect: NgSelectComponent;
|
|
|
|
|
-
|
|
|
|
|
get subTotal(): number {
|
|
get subTotal(): number {
|
|
|
return this.lines.reduce((sum, l) => sum + l.unitPriceWithTax * l.quantity, 0);
|
|
return this.lines.reduce((sum, l) => sum + l.unitPriceWithTax * l.quantity, 0);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
constructor(private dataService: DataService, private localStorageService: LocalStorageService) {}
|
|
constructor(private dataService: DataService, private localStorageService: LocalStorageService) {}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
ngOnInit() {
|
|
@@ -41,22 +36,19 @@ export class TestOrderBuilderComponent implements OnInit {
|
|
|
if (this.lines) {
|
|
if (this.lines) {
|
|
|
this.orderLinesChange.emit(this.lines);
|
|
this.orderLinesChange.emit(this.lines);
|
|
|
}
|
|
}
|
|
|
- this.initSearchResults();
|
|
|
|
|
- this.dataService.settings.getActiveChannel('cache-first').single$.subscribe((result) => {
|
|
|
|
|
|
|
+ this.dataService.settings.getActiveChannel('cache-first').single$.subscribe(result => {
|
|
|
this.currencyCode = result.activeChannel.currencyCode;
|
|
this.currencyCode = result.activeChannel.currencyCode;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- selectResult(result: SearchForTestOrder.Items) {
|
|
|
|
|
|
|
+ selectResult(result: ProductSelectorSearch.Items) {
|
|
|
if (result) {
|
|
if (result) {
|
|
|
- this.resultSelected$.next();
|
|
|
|
|
- this.ngSelect.clearModel();
|
|
|
|
|
this.addToLines(result);
|
|
this.addToLines(result);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private addToLines(result: SearchForTestOrder.Items) {
|
|
|
|
|
- if (!this.lines.find((l) => l.id === result.productVariantId)) {
|
|
|
|
|
|
|
+ private addToLines(result: ProductSelectorSearch.Items) {
|
|
|
|
|
+ if (!this.lines.find(l => l.id === result.productVariantId)) {
|
|
|
this.lines.push({
|
|
this.lines.push({
|
|
|
id: result.productVariantId,
|
|
id: result.productVariantId,
|
|
|
name: result.productVariantName,
|
|
name: result.productVariantName,
|
|
@@ -77,31 +69,11 @@ export class TestOrderBuilderComponent implements OnInit {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
removeLine(line: TestOrderLine) {
|
|
removeLine(line: TestOrderLine) {
|
|
|
- this.lines = this.lines.filter((l) => l.id !== line.id);
|
|
|
|
|
|
|
+ this.lines = this.lines.filter(l => l.id !== line.id);
|
|
|
this.persistToLocalStorage();
|
|
this.persistToLocalStorage();
|
|
|
this.orderLinesChange.emit(this.lines);
|
|
this.orderLinesChange.emit(this.lines);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private initSearchResults() {
|
|
|
|
|
- const searchItems$ = this.searchInput$.pipe(
|
|
|
|
|
- debounceTime(200),
|
|
|
|
|
- distinctUntilChanged(),
|
|
|
|
|
- tap(() => (this.searchLoading = true)),
|
|
|
|
|
- switchMap((term) => {
|
|
|
|
|
- if (!term) {
|
|
|
|
|
- return of([]);
|
|
|
|
|
- }
|
|
|
|
|
- return this.dataService.settings
|
|
|
|
|
- .searchForTestOrder(term, 10)
|
|
|
|
|
- .mapSingle((result) => result.search.items);
|
|
|
|
|
- }),
|
|
|
|
|
- tap(() => (this.searchLoading = false)),
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- const clear$ = this.resultSelected$.pipe(mapTo([]));
|
|
|
|
|
- this.searchResults$ = concat(of([]), merge(searchItems$, clear$));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private persistToLocalStorage() {
|
|
private persistToLocalStorage() {
|
|
|
this.localStorageService.setForCurrentLocation('shippingTestOrder', this.lines);
|
|
this.localStorageService.setForCurrentLocation('shippingTestOrder', this.lines);
|
|
|
}
|
|
}
|