The issue seems to be related to the timing of updates for the controlSelected
and isAssessmentDataLoading
variables. The updateQuestions()
method is invoked within the ngOnInit()
method, which is triggered when the component is initialized.
However, the assessmentData.securityControl
variable is not populated until the setAssessmentData()
method finishes, which occurs asynchronously.
Consequently, the updateQuestions()
method is invoked before the assessmentData.securityControl
variable is updated, leading to the controlSelected
variable remaining true
even when a valid security control is present. How can this be resolved?
I intend to modify updateQuestions()
so that it is triggered upon successful retrieval of the assessment.
ngOnInit() {
this.setAssessmentData(this.navigationService.getSavedID());
this.isControlSecurityComplete = this.navigationService.getControlSecurityComplete();
if (this.isControlSecurityComplete) {
this.dataService.selectedSecurity$.subscribe((value) => {
this.selectedSecurity = value;
});
this.questions.forEach(question => {
question.selectedOption = null;
});
}
}
setAssessmentData(assessmentID: string) {
this.assessmentService.getAssessmentById(assessmentID).subscribe(
(assessment) => {
this.assessmentData.nomeAssessment = assessment['nomeAssessment'];
this.assessmentData.dataAssessment = assessment['dataAssessment'];
this.assessmentData.nomeStruttura = assessment['nomeStruttura'];
this.assessmentData.nomeCittaOSito = assessment['nomeCittaOSito'];
this.assessmentData.statoProvinciaRegione = assessment['statoProvinciaRegione'];
this.assessmentData.contatti.nome = assessment['contatti'] ? .nome;
this.assessmentData.contatti.ruolo = assessment['contatti'] ? .ruolo;
this.assessmentData.contatti.email = assessment['contatti'] ? .email;
this.assessmentData.selectedCategory = assessment['selectedCategory'];
this.assessmentData.securityLevel = assessment['securityLevel'];
this.assessmentData.securityControl = assessment['securityControl'];
this.assessmentData.domande = assessment['domande'];
this.assessmentData.rispostaChatGPT = assessment['rispostaChatGPT'];
if (this.assessmentData.securityControl == '') {
this.controlSelected = true;
} else {
this.updateQuestions(this.assessmentData.securityControl);
this.controlSelected = false;
}
},
(error) => {
this.isAssessmentDataLoading = false;
console.error('Error retrieving assessment', error);
}
);
}
updateQuestions(controlName: string) {
this.dataService.getJsonDataNew(controlName).subscribe((data) => {
if (data && data.fileDocument && Array.isArray(data.fileDocument.fileData)) {
this.questions = data.fileDocument.fileData.filter((item: any) => item.Security <= this.selectedSecurity);
} else {
this.questions = [];
}
});
}