-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent double requests after inserting a SKU on direct order an…
…d quickorder form including validation
- Loading branch information
Showing
6 changed files
with
107 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 52 additions & 3 deletions
55
...p/extensions/quickorder/shared/quickorder-repeat-form/quickorder-repeat-form.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,64 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { | ||
AfterViewInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
QueryList, | ||
ViewChildren, | ||
} from '@angular/core'; | ||
import { FieldArrayType } from '@ngx-formly/core'; | ||
import { debounceTime, map } from 'rxjs/operators'; | ||
|
||
import { ProductContextDirective } from 'ish-core/directives/product-context.directive'; | ||
import { ProductContextFacade } from 'ish-core/facades/product-context.facade'; | ||
|
||
/** | ||
* The Quick Order Repeat Component provides a formly field element, which displays a field array of n elements depending on the according model. | ||
* Each line displays a form field for the sku and for the according quantity. | ||
* The component handles adding and removing of elements to the according formly model. | ||
* The component controls the product contexts for a proper behavior between entered product sku and its quantity field. | ||
*/ | ||
@Component({ | ||
selector: 'ish-quickorder-repeat-form', | ||
templateUrl: './quickorder-repeat-form.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class QuickorderRepeatFormComponent extends FieldArrayType { | ||
export class QuickorderRepeatFormComponent extends FieldArrayType implements AfterViewInit { | ||
@ViewChildren(ProductContextDirective) contexts: QueryList<{ context: ProductContextFacade }>; | ||
|
||
constructor(private cdRef: ChangeDetectorRef) { | ||
super(); | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.updateContexts(); | ||
} | ||
|
||
addMultipleRows(rows: number) { | ||
for (let i = 0; i < rows; i++) { | ||
this.add(this.model.length, { sku: '', quantity: undefined }); | ||
this.add(this.model.length, { sku: '', quantity: 1 }); | ||
} | ||
this.updateContexts(); | ||
} | ||
|
||
/** | ||
* Set the form control field to the according product context and handle its behavior. | ||
*/ | ||
updateContexts() { | ||
this.cdRef.detectChanges(); | ||
this.contexts.forEach((context: { context: ProductContextFacade }, index) => { | ||
const field = this.field.fieldGroup[index].fieldGroup[0]; | ||
const formControl = field.formControl; | ||
|
||
context.context.connect('sku', formControl.valueChanges.pipe(debounceTime(500))); | ||
formControl.setAsyncValidators(() => | ||
context.context.select('product').pipe( | ||
map(product => { | ||
this.cdRef.markForCheck(); | ||
return product.failed && formControl.value.trim !== '' ? { validProduct: false } : undefined; | ||
}) | ||
) | ||
); | ||
}); | ||
} | ||
} |