diff --git a/portal-frontend/src/app/features/applications/edit-submission/proposal/pfrs-proposal/pfrs-proposal.component.ts b/portal-frontend/src/app/features/applications/edit-submission/proposal/pfrs-proposal/pfrs-proposal.component.ts index 55de05b595..e887069f3d 100644 --- a/portal-frontend/src/app/features/applications/edit-submission/proposal/pfrs-proposal/pfrs-proposal.component.ts +++ b/portal-frontend/src/app/features/applications/edit-submission/proposal/pfrs-proposal/pfrs-proposal.component.ts @@ -420,7 +420,7 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, this.structuresSource = new MatTableDataSource(this.proposedStructures); - if (this.hasInput(structure.type)) { + if (this.structureChangeRequiresConfirmation(structure.type, newType)) { this.confirmationDialogService .openDialog({ title: 'Change Structure Type', @@ -455,29 +455,29 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, this.form.markAsDirty(); } - private hasInput(type: STRUCTURE_TYPES | null) { - switch (type) { - case STRUCTURE_TYPES.FARM_STRUCTURE: - return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value); - - case STRUCTURE_TYPES.ACCESSORY_STRUCTURE: - return !!( - this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value - ); - - case STRUCTURE_TYPES.OTHER_STRUCTURE: - return !!this.soilStructureOtherUseReason.value; - - case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE: - case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE: - return !!this.soilStructureResidentialUseReason.value; - - case null: - return false; - - default: - return true; - } + private structureChangeRequiresConfirmation( + oldType: STRUCTURE_TYPES | null, + newType: STRUCTURE_TYPES | null, + ): boolean { + const residentialTypes = [ + STRUCTURE_TYPES.PRINCIPAL_RESIDENCE, + STRUCTURE_TYPES.ADDITIONAL_RESIDENCE, + STRUCTURE_TYPES.ACCESSORY_STRUCTURE, + ]; + const changingFromResidentialType = oldType && residentialTypes.includes(oldType); + const changingToResidentialType = newType && residentialTypes.includes(newType); + + return !!( + oldType !== newType && + ((oldType && + oldType === STRUCTURE_TYPES.FARM_STRUCTURE && + (this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) || + (changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) || + (oldType && + oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE && + this.soilStructureResidentialAccessoryUseReason.value) || + (oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value)) + ); } private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) { @@ -502,9 +502,10 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, updateStructureTypeFields() { // Remove - if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) { + this.soilStructureFarmUseReason.reset(); this.soilStructureFarmUseReason.removeValidators([Validators.required]); + this.soilAgriParcelActivity.reset(); this.soilAgriParcelActivity.removeValidators([Validators.required]); } @@ -513,14 +514,17 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 && this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0 ) { + this.soilStructureResidentialUseReason.reset(); this.soilStructureResidentialUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + this.soilStructureResidentialAccessoryUseReason.reset(); this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + this.soilStructureOtherUseReason.reset(); this.soilStructureOtherUseReason.removeValidators([Validators.required]); } @@ -528,9 +532,7 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) { this.soilStructureFarmUseReason.setValidators([Validators.required]); - this.soilStructureFarmUseReason.reset(); this.soilAgriParcelActivity.setValidators([Validators.required]); - this.soilAgriParcelActivity.reset(); } if ( @@ -539,17 +541,14 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0 ) { this.soilStructureResidentialUseReason.setValidators([Validators.required]); - this.soilStructureResidentialUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) { this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]); - this.soilStructureResidentialAccessoryUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) { this.soilStructureOtherUseReason.setValidators([Validators.required]); - this.soilStructureOtherUseReason.reset(); } } @@ -631,8 +630,8 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit, this.structuresForm.removeControl(`${id}-area`); this.structuresForm.markAsDirty(); - this.updateStructureTypeFields(); this.updateStructureCounts(structureToDelete.type, null); + this.updateStructureTypeFields(); } onStructureEdit(id: string) { diff --git a/portal-frontend/src/app/features/applications/edit-submission/proposal/pofo-proposal/pofo-proposal.component.ts b/portal-frontend/src/app/features/applications/edit-submission/proposal/pofo-proposal/pofo-proposal.component.ts index 04132c34cc..b13720206c 100644 --- a/portal-frontend/src/app/features/applications/edit-submission/proposal/pofo-proposal/pofo-proposal.component.ts +++ b/portal-frontend/src/app/features/applications/edit-submission/proposal/pofo-proposal/pofo-proposal.component.ts @@ -347,7 +347,7 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, this.structuresSource = new MatTableDataSource(this.proposedStructures); - if (this.hasInput(structure.type)) { + if (this.hasInput(structure.type, newType)) { this.confirmationDialogService .openDialog({ title: 'Change Structure Type', @@ -382,29 +382,25 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, this.form.markAsDirty(); } - private hasInput(type: STRUCTURE_TYPES | null) { - switch (type) { - case STRUCTURE_TYPES.FARM_STRUCTURE: - return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value); - - case STRUCTURE_TYPES.ACCESSORY_STRUCTURE: - return !!( - this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value - ); - - case STRUCTURE_TYPES.OTHER_STRUCTURE: - return !!this.soilStructureOtherUseReason.value; - - case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE: - case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE: - return !!this.soilStructureResidentialUseReason.value; - - case null: - return false; - - default: - return true; - } + private hasInput(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) { + const residentialTypes = [ + STRUCTURE_TYPES.PRINCIPAL_RESIDENCE, + STRUCTURE_TYPES.ADDITIONAL_RESIDENCE, + STRUCTURE_TYPES.ACCESSORY_STRUCTURE, + ]; + const changingFromResidentialType = oldType && residentialTypes.includes(oldType); + const changingToResidentialType = newType && residentialTypes.includes(newType); + + return !!( + (oldType && + oldType === STRUCTURE_TYPES.FARM_STRUCTURE && + (this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) || + (changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) || + (oldType && + oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE && + this.soilStructureResidentialAccessoryUseReason.value) || + (oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value) + ); } private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) { @@ -431,7 +427,9 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, // Remove if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) { + this.soilStructureFarmUseReason.reset(); this.soilStructureFarmUseReason.removeValidators([Validators.required]); + this.soilAgriParcelActivity.reset(); this.soilAgriParcelActivity.removeValidators([Validators.required]); } @@ -440,14 +438,17 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 && this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0 ) { + this.soilStructureResidentialUseReason.reset(); this.soilStructureResidentialUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + this.soilStructureResidentialAccessoryUseReason.reset(); this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + this.soilStructureOtherUseReason.reset(); this.soilStructureOtherUseReason.removeValidators([Validators.required]); } @@ -455,9 +456,7 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) { this.soilStructureFarmUseReason.setValidators([Validators.required]); - this.soilStructureFarmUseReason.reset(); this.soilAgriParcelActivity.setValidators([Validators.required]); - this.soilAgriParcelActivity.reset(); } if ( @@ -466,17 +465,14 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0 ) { this.soilStructureResidentialUseReason.setValidators([Validators.required]); - this.soilStructureResidentialUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) { this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]); - this.soilStructureResidentialAccessoryUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) { this.soilStructureOtherUseReason.setValidators([Validators.required]); - this.soilStructureOtherUseReason.reset(); } } @@ -558,8 +554,8 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit, this.structuresForm.removeControl(`${id}-area`); this.structuresForm.markAsDirty(); - this.updateStructureTypeFields(); this.updateStructureCounts(structureToDelete.type, null); + this.updateStructureTypeFields(); } onStructureEdit(id: string) { diff --git a/portal-frontend/src/app/features/applications/edit-submission/proposal/roso-proposal/roso-proposal.component.ts b/portal-frontend/src/app/features/applications/edit-submission/proposal/roso-proposal/roso-proposal.component.ts index d5d7e5036b..40a77a683e 100644 --- a/portal-frontend/src/app/features/applications/edit-submission/proposal/roso-proposal/roso-proposal.component.ts +++ b/portal-frontend/src/app/features/applications/edit-submission/proposal/roso-proposal/roso-proposal.component.ts @@ -406,8 +406,8 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, this.structuresForm.removeControl(`${id}-area`); this.structuresForm.markAsDirty(); - this.updateStructureTypeFields(); this.updateStructureCounts(structureToDelete.type, null); + this.updateStructureTypeFields(); } onStructureEdit(id: string) { @@ -456,7 +456,7 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, this.structuresSource = new MatTableDataSource(this.proposedStructures); - if (this.hasInput(structure.type)) { + if (this.hasInput(structure.type, newType)) { this.confirmationDialogService .openDialog({ title: 'Change Structure Type', @@ -491,29 +491,25 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, this.form.markAsDirty(); } - private hasInput(type: STRUCTURE_TYPES | null) { - switch (type) { - case STRUCTURE_TYPES.FARM_STRUCTURE: - return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value); - - case STRUCTURE_TYPES.ACCESSORY_STRUCTURE: - return !!( - this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value - ); - - case STRUCTURE_TYPES.OTHER_STRUCTURE: - return !!this.soilStructureOtherUseReason.value; - - case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE: - case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE: - return !!this.soilStructureResidentialUseReason.value; - - case null: - return false; - - default: - return true; - } + private hasInput(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) { + const residentialTypes = [ + STRUCTURE_TYPES.PRINCIPAL_RESIDENCE, + STRUCTURE_TYPES.ADDITIONAL_RESIDENCE, + STRUCTURE_TYPES.ACCESSORY_STRUCTURE, + ]; + const changingFromResidentialType = oldType && residentialTypes.includes(oldType); + const changingToResidentialType = newType && residentialTypes.includes(newType); + + return !!( + (oldType && + oldType === STRUCTURE_TYPES.FARM_STRUCTURE && + (this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) || + (changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) || + (oldType && + oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE && + this.soilStructureResidentialAccessoryUseReason.value) || + (oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value) + ); } private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) { @@ -540,7 +536,9 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, // Remove if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) { + this.soilStructureFarmUseReason.reset(); this.soilStructureFarmUseReason.removeValidators([Validators.required]); + this.soilAgriParcelActivity.reset(); this.soilAgriParcelActivity.removeValidators([Validators.required]); } @@ -549,14 +547,17 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 && this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0 ) { + this.soilStructureResidentialUseReason.reset(); this.soilStructureResidentialUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + this.soilStructureResidentialAccessoryUseReason.reset(); this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]); } - if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) { + if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) { + this.soilStructureOtherUseReason.reset(); this.soilStructureOtherUseReason.removeValidators([Validators.required]); } @@ -564,9 +565,7 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) { this.soilStructureFarmUseReason.setValidators([Validators.required]); - this.soilStructureFarmUseReason.reset(); this.soilAgriParcelActivity.setValidators([Validators.required]); - this.soilAgriParcelActivity.reset(); } if ( @@ -575,17 +574,14 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit, this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0 ) { this.soilStructureResidentialUseReason.setValidators([Validators.required]); - this.soilStructureResidentialUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) { this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]); - this.soilStructureResidentialAccessoryUseReason.reset(); } if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) { this.soilStructureOtherUseReason.setValidators([Validators.required]); - this.soilStructureOtherUseReason.reset(); } }