Reputation: 179
I am trying to get a the correct pop-up message depending on a users input.
If the user is a 'Con Owner' they must fill out all items labelled as 'Con'. If they do not, they should have a warning message when they click submit.
I have written the below to loop through each item and where the owner of that item is equal to Con and the Value is empty it should give me the warning message. If they have filled it in, it should be the confirmation message.
However, the below isn't giving me the results I require. Irrespective of what happens, I always get the warning message as a pop up, even if the value has been populated.
isValuePopulated() {
let attrData = this.attributes.attrData;
let isAttributeCompleted = true;
attrData.forEach(item=>{
if(item.owner==='Con' && commonFunctions.isEmptyString(item.value)){
isAttributeCompleted = false;
}}
);
if (isAttributeCompleted){
this.showErrorInfoPopUp('All correct!!!!', 'Confirmation', true);
} else {
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('Are you sure you want to continue?', 'Warning', true);
}
}
Edit:
Thanks for the comments so far I have since removed the commonFunctions.isEmptyString(item.value))
as suggest. But I assume the error is down to the loop being asyc (also mentioned). However, as I am new to this, I am not sure how to resolve this. My first thought was to have two methods and have one loop through the items and then push the value to another method which determines the pop up. But I keep getting a "Expression Expected" error message when trying to build.
isValuePopulated() {
let attrData = this.attributes.attrData;
let isAttributeCompleted = true;
attrData.forEach(item=>{
if(item.owner==='Con' && !item.attributeValue){
isAttributeCompleted = false;
}}
);
this.typeOfPopUp(isAttributeCompleted )
}
typeOfPopUp(isAttributeCompleted ){
if (isAttributeCompleted ){
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('All correct!!!!', 'Confirmation', true);
} else {
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('Are you sure you want to continue?, 'Warning', true);
}
}
Upvotes: 2
Views: 6767
Reputation: 2165
While the point thats made by @Jérôme is true, the issue you face is because you expect an async code block to run syncroniously. You can use a simple for-of loop instead.
for (const item of arrayData)
Upvotes: 2
Reputation: 179
Thanks for the suggestions. I didn't realise the loop I was orignally using was async and didn't realise there is alternative non-async loop solution The following code worked:
isValuePopulated() {
let attrData = this.attributes.attrData;
let attributeCompleted: boolean = true;
for (const item of attrData){
if(item.owner==='Con' && !item.value){
attributeCompleted = false;
}
}
if (attributeCompleted){
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('All correct!!!!', 'Confirmation', true);
} else {
this.setErrorPopUpButtonStatus(true, false, true);
this.showErrorInfoPopUp('Are you sure you want to continue?', 'Warning', true);
}
}
Upvotes: 0
Reputation: 56
As a comment said, replace your function isEmptyString() by !item.value. The condition if (item.value) will evaluates to true if item.value is NOT :
Upvotes: 1