Skip to content

Commit 9feb23a

Browse files
committed
Fix residential clearing when it remains
1 parent e9b5148 commit 9feb23a

File tree

3 files changed

+84
-97
lines changed

3 files changed

+84
-97
lines changed

portal-frontend/src/app/features/applications/edit-submission/proposal/pfrs-proposal/pfrs-proposal.component.ts

+28-33
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
416416

417417
this.structuresSource = new MatTableDataSource(this.proposedStructures);
418418

419-
if (this.hasInput(structure.type)) {
419+
if (this.hasInput(structure.type, newType)) {
420420
this.confirmationDialogService
421421
.openDialog({
422422
title: 'Change Structure Type',
@@ -451,29 +451,25 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
451451
this.form.markAsDirty();
452452
}
453453

454-
private hasInput(type: STRUCTURE_TYPES | null) {
455-
switch (type) {
456-
case STRUCTURE_TYPES.FARM_STRUCTURE:
457-
return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value);
458-
459-
case STRUCTURE_TYPES.ACCESSORY_STRUCTURE:
460-
return !!(
461-
this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value
462-
);
463-
464-
case STRUCTURE_TYPES.OTHER_STRUCTURE:
465-
return !!this.soilStructureOtherUseReason.value;
466-
467-
case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE:
468-
case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE:
469-
return !!this.soilStructureResidentialUseReason.value;
470-
471-
case null:
472-
return false;
473-
474-
default:
475-
return true;
476-
}
454+
private hasInput(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) {
455+
const residentialTypes = [
456+
STRUCTURE_TYPES.PRINCIPAL_RESIDENCE,
457+
STRUCTURE_TYPES.ADDITIONAL_RESIDENCE,
458+
STRUCTURE_TYPES.ACCESSORY_STRUCTURE,
459+
];
460+
const changingFromResidentialType = oldType && residentialTypes.includes(oldType);
461+
const changingToResidentialType = newType && residentialTypes.includes(newType);
462+
463+
return !!(
464+
(oldType &&
465+
oldType === STRUCTURE_TYPES.FARM_STRUCTURE &&
466+
(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) ||
467+
(changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) ||
468+
(oldType &&
469+
oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE &&
470+
this.soilStructureResidentialAccessoryUseReason.value) ||
471+
(oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value)
472+
);
477473
}
478474

479475
private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) {
@@ -498,9 +494,10 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
498494

499495
updateStructureTypeFields() {
500496
// Remove
501-
502497
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) {
498+
this.soilStructureFarmUseReason.reset();
503499
this.soilStructureFarmUseReason.removeValidators([Validators.required]);
500+
this.soilAgriParcelActivity.reset();
504501
this.soilAgriParcelActivity.removeValidators([Validators.required]);
505502
}
506503

@@ -509,24 +506,25 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
509506
this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 &&
510507
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0
511508
) {
509+
this.soilStructureResidentialUseReason.reset();
512510
this.soilStructureResidentialUseReason.removeValidators([Validators.required]);
513511
}
514512

515-
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
513+
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
514+
this.soilStructureResidentialAccessoryUseReason.reset();
516515
this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]);
517516
}
518517

519-
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
518+
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
519+
this.soilStructureOtherUseReason.reset();
520520
this.soilStructureOtherUseReason.removeValidators([Validators.required]);
521521
}
522522

523523
// Add
524524

525525
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) {
526526
this.soilStructureFarmUseReason.setValidators([Validators.required]);
527-
this.soilStructureFarmUseReason.reset();
528527
this.soilAgriParcelActivity.setValidators([Validators.required]);
529-
this.soilAgriParcelActivity.reset();
530528
}
531529

532530
if (
@@ -535,17 +533,14 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
535533
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0
536534
) {
537535
this.soilStructureResidentialUseReason.setValidators([Validators.required]);
538-
this.soilStructureResidentialUseReason.reset();
539536
}
540537

541538
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) {
542539
this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]);
543-
this.soilStructureResidentialAccessoryUseReason.reset();
544540
}
545541

546542
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) {
547543
this.soilStructureOtherUseReason.setValidators([Validators.required]);
548-
this.soilStructureOtherUseReason.reset();
549544
}
550545
}
551546

@@ -627,8 +622,8 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
627622
this.structuresForm.removeControl(`${id}-area`);
628623
this.structuresForm.markAsDirty();
629624

630-
this.updateStructureTypeFields();
631625
this.updateStructureCounts(structureToDelete.type, null);
626+
this.updateStructureTypeFields();
632627
}
633628

634629
onStructureEdit(id: string) {

portal-frontend/src/app/features/applications/edit-submission/proposal/pofo-proposal/pofo-proposal.component.ts

+28-32
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
343343

344344
this.structuresSource = new MatTableDataSource(this.proposedStructures);
345345

346-
if (this.hasInput(structure.type)) {
346+
if (this.hasInput(structure.type, newType)) {
347347
this.confirmationDialogService
348348
.openDialog({
349349
title: 'Change Structure Type',
@@ -378,29 +378,25 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
378378
this.form.markAsDirty();
379379
}
380380

381-
private hasInput(type: STRUCTURE_TYPES | null) {
382-
switch (type) {
383-
case STRUCTURE_TYPES.FARM_STRUCTURE:
384-
return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value);
385-
386-
case STRUCTURE_TYPES.ACCESSORY_STRUCTURE:
387-
return !!(
388-
this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value
389-
);
390-
391-
case STRUCTURE_TYPES.OTHER_STRUCTURE:
392-
return !!this.soilStructureOtherUseReason.value;
393-
394-
case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE:
395-
case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE:
396-
return !!this.soilStructureResidentialUseReason.value;
397-
398-
case null:
399-
return false;
400-
401-
default:
402-
return true;
403-
}
381+
private hasInput(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) {
382+
const residentialTypes = [
383+
STRUCTURE_TYPES.PRINCIPAL_RESIDENCE,
384+
STRUCTURE_TYPES.ADDITIONAL_RESIDENCE,
385+
STRUCTURE_TYPES.ACCESSORY_STRUCTURE,
386+
];
387+
const changingFromResidentialType = oldType && residentialTypes.includes(oldType);
388+
const changingToResidentialType = newType && residentialTypes.includes(newType);
389+
390+
return !!(
391+
(oldType &&
392+
oldType === STRUCTURE_TYPES.FARM_STRUCTURE &&
393+
(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) ||
394+
(changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) ||
395+
(oldType &&
396+
oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE &&
397+
this.soilStructureResidentialAccessoryUseReason.value) ||
398+
(oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value)
399+
);
404400
}
405401

406402
private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) {
@@ -427,7 +423,9 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
427423
// Remove
428424

429425
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) {
426+
this.soilStructureFarmUseReason.reset();
430427
this.soilStructureFarmUseReason.removeValidators([Validators.required]);
428+
this.soilAgriParcelActivity.reset();
431429
this.soilAgriParcelActivity.removeValidators([Validators.required]);
432430
}
433431

@@ -436,24 +434,25 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
436434
this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 &&
437435
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0
438436
) {
437+
this.soilStructureResidentialUseReason.reset();
439438
this.soilStructureResidentialUseReason.removeValidators([Validators.required]);
440439
}
441440

442-
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
441+
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
442+
this.soilStructureResidentialAccessoryUseReason.reset();
443443
this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]);
444444
}
445445

446-
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
446+
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
447+
this.soilStructureOtherUseReason.reset();
447448
this.soilStructureOtherUseReason.removeValidators([Validators.required]);
448449
}
449450

450451
// Add
451452

452453
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) {
453454
this.soilStructureFarmUseReason.setValidators([Validators.required]);
454-
this.soilStructureFarmUseReason.reset();
455455
this.soilAgriParcelActivity.setValidators([Validators.required]);
456-
this.soilAgriParcelActivity.reset();
457456
}
458457

459458
if (
@@ -462,17 +461,14 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
462461
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0
463462
) {
464463
this.soilStructureResidentialUseReason.setValidators([Validators.required]);
465-
this.soilStructureResidentialUseReason.reset();
466464
}
467465

468466
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) {
469467
this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]);
470-
this.soilStructureResidentialAccessoryUseReason.reset();
471468
}
472469

473470
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) {
474471
this.soilStructureOtherUseReason.setValidators([Validators.required]);
475-
this.soilStructureOtherUseReason.reset();
476472
}
477473
}
478474

@@ -554,8 +550,8 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
554550
this.structuresForm.removeControl(`${id}-area`);
555551
this.structuresForm.markAsDirty();
556552

557-
this.updateStructureTypeFields();
558553
this.updateStructureCounts(structureToDelete.type, null);
554+
this.updateStructureTypeFields();
559555
}
560556

561557
onStructureEdit(id: string) {

portal-frontend/src/app/features/applications/edit-submission/proposal/roso-proposal/roso-proposal.component.ts

+28-32
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
402402
this.structuresForm.removeControl(`${id}-area`);
403403
this.structuresForm.markAsDirty();
404404

405-
this.updateStructureTypeFields();
406405
this.updateStructureCounts(structureToDelete.type, null);
406+
this.updateStructureTypeFields();
407407
}
408408

409409
onStructureEdit(id: string) {
@@ -452,7 +452,7 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
452452

453453
this.structuresSource = new MatTableDataSource(this.proposedStructures);
454454

455-
if (this.hasInput(structure.type)) {
455+
if (this.hasInput(structure.type, newType)) {
456456
this.confirmationDialogService
457457
.openDialog({
458458
title: 'Change Structure Type',
@@ -487,29 +487,25 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
487487
this.form.markAsDirty();
488488
}
489489

490-
private hasInput(type: STRUCTURE_TYPES | null) {
491-
switch (type) {
492-
case STRUCTURE_TYPES.FARM_STRUCTURE:
493-
return !!(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value);
494-
495-
case STRUCTURE_TYPES.ACCESSORY_STRUCTURE:
496-
return !!(
497-
this.soilStructureResidentialUseReason.value || this.soilStructureResidentialAccessoryUseReason.value
498-
);
499-
500-
case STRUCTURE_TYPES.OTHER_STRUCTURE:
501-
return !!this.soilStructureOtherUseReason.value;
502-
503-
case STRUCTURE_TYPES.PRINCIPAL_RESIDENCE:
504-
case STRUCTURE_TYPES.ADDITIONAL_RESIDENCE:
505-
return !!this.soilStructureResidentialUseReason.value;
506-
507-
case null:
508-
return false;
509-
510-
default:
511-
return true;
512-
}
490+
private hasInput(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) {
491+
const residentialTypes = [
492+
STRUCTURE_TYPES.PRINCIPAL_RESIDENCE,
493+
STRUCTURE_TYPES.ADDITIONAL_RESIDENCE,
494+
STRUCTURE_TYPES.ACCESSORY_STRUCTURE,
495+
];
496+
const changingFromResidentialType = oldType && residentialTypes.includes(oldType);
497+
const changingToResidentialType = newType && residentialTypes.includes(newType);
498+
499+
return !!(
500+
(oldType &&
501+
oldType === STRUCTURE_TYPES.FARM_STRUCTURE &&
502+
(this.soilAgriParcelActivity.value || this.soilStructureFarmUseReason.value)) ||
503+
(changingFromResidentialType && !changingToResidentialType && this.soilStructureResidentialUseReason.value) ||
504+
(oldType &&
505+
oldType === STRUCTURE_TYPES.ACCESSORY_STRUCTURE &&
506+
this.soilStructureResidentialAccessoryUseReason.value) ||
507+
(oldType && oldType === STRUCTURE_TYPES.OTHER_STRUCTURE && this.soilStructureOtherUseReason.value)
508+
);
513509
}
514510

515511
private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) {
@@ -536,7 +532,9 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
536532
// Remove
537533

538534
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) {
535+
this.soilStructureFarmUseReason.reset();
539536
this.soilStructureFarmUseReason.removeValidators([Validators.required]);
537+
this.soilAgriParcelActivity.reset();
540538
this.soilAgriParcelActivity.removeValidators([Validators.required]);
541539
}
542540

@@ -545,24 +543,25 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
545543
this.structureTypeCounts[STRUCTURE_TYPES.ADDITIONAL_RESIDENCE] === 0 &&
546544
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0
547545
) {
546+
this.soilStructureResidentialUseReason.reset();
548547
this.soilStructureResidentialUseReason.removeValidators([Validators.required]);
549548
}
550549

551-
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
550+
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
551+
this.soilStructureResidentialAccessoryUseReason.reset();
552552
this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]);
553553
}
554554

555-
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
555+
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] === 0) {
556+
this.soilStructureOtherUseReason.reset();
556557
this.soilStructureOtherUseReason.removeValidators([Validators.required]);
557558
}
558559

559560
// Add
560561

561562
if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] > 0) {
562563
this.soilStructureFarmUseReason.setValidators([Validators.required]);
563-
this.soilStructureFarmUseReason.reset();
564564
this.soilAgriParcelActivity.setValidators([Validators.required]);
565-
this.soilAgriParcelActivity.reset();
566565
}
567566

568567
if (
@@ -571,17 +570,14 @@ export class RosoProposalComponent extends FilesStepComponent implements OnInit,
571570
this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0
572571
) {
573572
this.soilStructureResidentialUseReason.setValidators([Validators.required]);
574-
this.soilStructureResidentialUseReason.reset();
575573
}
576574

577575
if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] > 0) {
578576
this.soilStructureResidentialAccessoryUseReason.setValidators([Validators.required]);
579-
this.soilStructureResidentialAccessoryUseReason.reset();
580577
}
581578

582579
if (this.structureTypeCounts[STRUCTURE_TYPES.OTHER_STRUCTURE] > 0) {
583580
this.soilStructureOtherUseReason.setValidators([Validators.required]);
584-
this.soilStructureOtherUseReason.reset();
585581
}
586582
}
587583

0 commit comments

Comments
 (0)