Please let me know if I have to provide any more code or explanations.
I am trying to execute validate, if validation not successful, confirm(Yes/No), and then save method for all selected rows in a for loop.
Requirement: I have show a success/failure message after looping through all selected rows. That means:
If 5 rows are selected (2 are valid and 3 are not valid) - got from myService.validate
It will ask if you still want to continue for 3 not valid records - myConfirmService.confirm
It should save those 2 valid records
If user selects 'Yes' in confirm window (lets says for 2 records), then it should save these 2 as well.
At the end - it will display success/failure message - alertService.success
What I want:
To fulfil what I described in the requirement
and The success/failure message should display only once after all record saved
component.ts:
save() {
this.selectRowData.forEach(async s => {
this.myService.validate(s.id).subscribe(data => {
console.log(data);
if(data == false) {
this.myConfirmService
.confirm('Please confirm..', 'Selected Part do not match expected part data. \n Do you still want to continue? ', 'Yes', 'No')
.then((confirmed) => {
if(confirmed) {
console.log("User selected: Yes");
this.myService.savePart();
} else {
console.log("User selected: No");
}
})
.catch(() => {
console.log("Error...");
});
} else {
console.log("Selected Rule matches expected rules for: " + s.designChangeClass);
this.saveModelPart(s);
}
}, (error: any) => {
alert("Error Linking Lot");
});
});
console.log('1st After for loop');
this.alertService.success('Part saved successfully.')
}
service.ts
validate(id: string): Observable < boolean > {
return this.httpClient.get < boolean > ('/RestWeb/validate?id=' + id);
}
validate1(id: string) {
return this.httpClient.get < boolean > ('/RestWeb/validate?id=' + id).toPromise();
}
savePart(item: Part): Observable<any> {
return this.httpClient.post('/RestService/savePart', item);
}