Skip to content

Commit

Permalink
EMBCESSMOD-5153 fix infinite loop, and fix to not hide form immediate…
Browse files Browse the repository at this point in the history
…ly on entering a value in profile form (bcgov#2049)
  • Loading branch information
HemanthKona authored Apr 16, 2024
1 parent a5c8939 commit 0aab7a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import {
Component,
EventEmitter,
OnDestroy,
OnInit,
Output
} from '@angular/core';
import {
AbstractControl,
UntypedFormBuilder,
UntypedFormGroup,
Validators
} from '@angular/forms';
import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
import { AbstractControl, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { MatRadioChange } from '@angular/material/radio';
import { BehaviorSubject } from 'rxjs';
Expand Down Expand Up @@ -86,10 +75,7 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
}

// Displaying household member form in case 'haveHouseholdMembers' has been set to true
if (
this.stepEssFileService.haveHouseHoldMembers === 'Yes' &&
this.stepEssFileService.householdMembers.length === 1
) {
if (this.stepEssFileService.haveHouseHoldMembers === 'Yes' && this.stepEssFileService.householdMembers.length === 1) {
this.showMemberForm = true;
this.householdForm.get('addMemberFormIndicator').setValue(true);
this.householdForm.get('addMemberIndicator').setValue(true);
Expand All @@ -102,11 +88,9 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
}

// Updates the status of the form according to changes
this.householdForm
.get('addMemberFormIndicator')
.valueChanges.subscribe(() => {
this.updateOnVisibility();
});
this.householdForm.get('addMemberFormIndicator').valueChanges.subscribe(() => {
this.updateOnVisibility();
});

this.runValidation();

Expand Down Expand Up @@ -184,7 +168,10 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
* Resets the household Member form and goes back to the main Form
*/
cancel(): void {
this.householdService.cancel(this.householdForm);
this.householdForm.get('addMemberFormIndicator').setValue(false);
this.householdForm.get('addMemberIndicator').setValue(false);
this.householdForm.get('houseHoldMember').reset();

this.showMemberForm = false;
this.editFlag = false;
this.duplicateFlag = false;
Expand Down Expand Up @@ -235,9 +222,7 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
this.addNewMember = false;

// If "no members", still keep Primary
this.members = this.stepEssFileService.householdMembers.filter(
(m) => m.isPrimaryRegistrant
);
this.members = this.stepEssFileService.householdMembers.filter((m) => m.isPrimaryRegistrant);
this.memberSource.next(this.members);

this.householdForm.get('houseHoldMember').reset();
Expand Down Expand Up @@ -280,15 +265,11 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
*/
ngOnDestroy(): void {
if (this.stepEssFileService.checkForEdit()) {
const isFormUpdated = this.wizardService.hasChanged(
this.householdForm.controls,
'householdMember'
);
const isFormUpdated = this.wizardService.hasChanged(this.householdForm.controls, 'householdMember');

this.wizardService.setEditStatus({
tabName: 'householdMember',
tabUpdateStatus:
isFormUpdated || this.stepEssFileService.needsAssessmentSubmitFlag
tabUpdateStatus: isFormUpdated || this.stepEssFileService.needsAssessmentSubmitFlag
});
this.stepEssFileService.updateEditedFormStatus();
}
Expand All @@ -299,19 +280,13 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
* Create Household Members main form
*/
private createHouseholdForm(): void {
if (!this.stepEssFileService.householdMembers)
this.stepEssFileService.householdMembers = [];
if (!this.stepEssFileService.householdMembers) this.stepEssFileService.householdMembers = [];

this.householdForm = this.formBuilder.group({
hasHouseholdMembers: [
this.stepEssFileService.haveHouseHoldMembers,
Validators.required
],
hasHouseholdMembers: [this.stepEssFileService.haveHouseHoldMembers, Validators.required],
houseHoldMember: this.createHouseholdMemberForm(),
addMemberIndicator: [this.stepEssFileService.addMemberIndicator ?? false],
addMemberFormIndicator: [
this.stepEssFileService.addMemberFormIndicator ?? false
]
addMemberFormIndicator: [this.stepEssFileService.addMemberFormIndicator ?? false]
});
}

Expand All @@ -326,55 +301,31 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
this.stepEssFileService?.tempHouseholdMember?.firstName ?? '',
[
this.customValidation
.conditionalValidation(
() =>
this.householdForm.get('addMemberFormIndicator').value === true,
this.customValidation.whitespaceValidator()
)
.conditionalValidation(() => this.householdForm.get('addMemberFormIndicator').value === true, this.customValidation.whitespaceValidator())
.bind(this.customValidation)
]
],
lastName: [
this.stepEssFileService?.tempHouseholdMember?.lastName ?? '',
[
this.customValidation
.conditionalValidation(
() =>
this.householdForm.get('addMemberFormIndicator').value === true,
this.customValidation.whitespaceValidator()
)
.conditionalValidation(() => this.householdForm.get('addMemberFormIndicator').value === true, this.customValidation.whitespaceValidator())
.bind(this.customValidation)
]
],
dateOfBirth: [
this.stepEssFileService?.tempHouseholdMember?.dateOfBirth ?? '',
[
this.customValidation
.conditionalValidation(
() =>
this.householdForm.get('addMemberFormIndicator').value === true,
Validators.required
)
.bind(this.customValidation),
this.customValidation.conditionalValidation(() => this.householdForm.get('addMemberFormIndicator').value === true, Validators.required).bind(this.customValidation),
this.customValidation.dateOfBirthValidator()
]
],
gender: [
this.stepEssFileService?.tempHouseholdMember?.gender ?? '',
[
this.customValidation
.conditionalValidation(
() =>
this.householdForm.get('addMemberFormIndicator').value === true,
Validators.required
)
.bind(this.customValidation)
]
[this.customValidation.conditionalValidation(() => this.householdForm.get('addMemberFormIndicator').value === true, Validators.required).bind(this.customValidation)]
],
initials: [this.stepEssFileService?.tempHouseholdMember?.initials ?? ''],
sameLastName: [
this.stepEssFileService?.tempHouseholdMember?.sameLastName ?? ''
],
sameLastName: [this.stepEssFileService?.tempHouseholdMember?.sameLastName ?? ''],
id: ['']
});
}
Expand All @@ -387,36 +338,22 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
}

private runValidation() {
// Remove Edit form if displayed while tabbing out
if (this.editFlag) this.cancel();

if (!this.essFileNumber) {
if (
this.householdForm.valid &&
this.householdForm.get('addMemberIndicator').value === false &&
!(
this.householdForm.get('hasHouseholdMembers').value === 'Yes' &&
this.members.length < 2
)
!(this.householdForm.get('hasHouseholdMembers').value === 'Yes' && this.members.length < 2)
) {
this.validHouseholdMemebersIndicator.emit(true);
} else if (
this.stepEssFileService.checkForPartialUpdates(this.householdForm)
) {
} else if (this.stepEssFileService.checkForPartialUpdates(this.householdForm)) {
this.validHouseholdMemebersIndicator.emit(false);
} else {
this.validHouseholdMemebersIndicator.emit(false);
}
} else if (this.essFileNumber) {
if (
this.householdForm.valid &&
this.householdForm.get('addMemberIndicator').value === false &&
this.selection.selected.length >= 1
) {
if (this.householdForm.valid && this.householdForm.get('addMemberIndicator').value === false && this.selection.selected.length >= 1) {
this.validHouseholdMemebersIndicator.emit(true);
} else if (
this.stepEssFileService.checkForPartialUpdates(this.householdForm)
) {
} else if (this.stepEssFileService.checkForPartialUpdates(this.householdForm)) {
this.validHouseholdMemebersIndicator.emit(false);
} else {
this.validHouseholdMemebersIndicator.emit(false);
Expand All @@ -430,30 +367,19 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
* Saves information inserted inthe form into the service
*/
private saveFormData() {
this.stepEssFileService.haveHouseHoldMembers = this.householdForm.get(
'hasHouseholdMembers'
).value;
this.stepEssFileService.haveHouseHoldMembers = this.householdForm.get('hasHouseholdMembers').value;

this.stepEssFileService.householdMembers = this.members;
this.stepEssFileService.selectedHouseholdMembers = this.selection.selected;

this.stepEssFileService.addMemberFormIndicator = this.householdForm.get(
'addMemberFormIndicator'
).value;
this.stepEssFileService.addMemberIndicator =
this.householdForm.get('addMemberIndicator').value;
this.stepEssFileService.addMemberFormIndicator = this.householdForm.get('addMemberFormIndicator').value;
this.stepEssFileService.addMemberIndicator = this.householdForm.get('addMemberIndicator').value;

this.stepEssFileService.tempHouseholdMember =
this.householdForm.get('houseHoldMember').value;
this.stepEssFileService.tempHouseholdMember = this.householdForm.get('houseHoldMember').value;
}

private saveNewMember(): void {
if (
!this.householdService.householdMemberExists(
this.householdForm.get('houseHoldMember').value,
this.members
)
) {
if (!this.householdService.householdMemberExists(this.householdForm.get('houseHoldMember').value, this.members)) {
this.members.push({
...this.householdForm.get('houseHoldMember').value,
isPrimaryRegistrant: false,
Expand All @@ -473,15 +399,9 @@ export class HouseholdMembersComponent implements OnInit, OnDestroy {
}

private saveEditedMember(): void {
const similarMember = this.householdService.householdMemberExists(
this.householdForm.get('houseHoldMember').value,
this.members
);

if (
similarMember === this.members[this.editIndex] ||
similarMember === undefined
) {
const similarMember = this.householdService.householdMemberExists(this.householdForm.get('houseHoldMember').value, this.members);

if (similarMember === this.members[this.editIndex] || similarMember === undefined) {
this.members[this.editIndex] = {
...this.members[this.editIndex],
...this.householdForm.get('houseHoldMember').value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ export class HouseholdMembersService {
return householdForm;
}

/**
* Resets the househol Member form and goes back to the main Form
*/
cancel(householdForm: UntypedFormGroup): UntypedFormGroup {
householdForm.get('addMemberFormIndicator').setValue(false);
householdForm.get('addMemberIndicator').setValue(false);
householdForm.get('houseHoldMember').reset();
return householdForm;
}

householdMemberExists(
newMember: HouseholdMemberModel,
household: HouseholdMemberModel[]
Expand Down

0 comments on commit 0aab7a8

Please sign in to comment.