Skip to content

Commit

Permalink
Merge pull request #1960 from bcgov/features/ALCS-1156-QA-2
Browse files Browse the repository at this point in the history
1156 QA fixes 2
  • Loading branch information
trslater authored Nov 6, 2024
2 parents b76b2bc + dc0cb0e commit 6930ffd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
this.structuresForm = new FormGroup({});
this.proposedStructures = [];
for (const structure of applicationSubmission.soilProposedStructures) {
console.log(structure);
this.addControl(structure.type, structure.area);
const newStructure = this.addControl(structure.area);
if (structure.type !== null) {
this.setStructureTypeInput(newStructure, structure.type);
}
}
this.structuresSource = new MatTableDataSource(this.proposedStructures);

Expand Down Expand Up @@ -475,51 +477,56 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
}

private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) {
if (structure.type !== null && this.structureTypeCounts[structure.type] > 0) {
this.structureTypeCounts[structure.type]--;
}
this.structureTypeCounts[newType]++;
this.updateStructureCounts(structure.type, newType);

structure.type = newType;
this.structuresForm.get(`${structure.id}-type`)?.setValue(newType);

this.updateStructureTypeFields();
this.form.markAsDirty();
}

private updateStructureCounts(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) {
if (oldType !== null && this.structureTypeCounts[oldType] > 0) {
this.structureTypeCounts[oldType]--;
}

if (newType !== null) {
this.structureTypeCounts[newType]++;
}
}

updateStructureTypeFields() {
// Remove

if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) {
this.soilStructureFarmUseReason.removeValidators([Validators.required]);
this.soilStructureFarmUseReason.reset();
this.soilAgriParcelActivity.removeValidators([Validators.required]);
this.soilAgriParcelActivity.reset();
}

if (
this.structureTypeCounts[STRUCTURE_TYPES.PRINCIPAL_RESIDENCE] === 0 &&
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) {
this.soilStructureResidentialAccessoryUseReason.reset();
this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]);
}

if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
this.soilStructureOtherUseReason.reset();
this.soilStructureOtherUseReason.removeValidators([Validators.required]);
}

// Add

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 (
Expand All @@ -528,14 +535,17 @@ 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();
}
}

Expand All @@ -553,43 +563,38 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
.beforeClosed()
.subscribe(async (result: { isEditing: boolean; structureId: string; dto: ProposedStructure }) => {
if (!result) return;
this.addControl(result.dto.type, result.dto.area);
const newStructure = this.addControl(result.dto.area);
if (result.dto.type !== null) {
this.setStructureTypeInput(newStructure, result.dto.type);
}
this.structuresSource = new MatTableDataSource(this.proposedStructures);

this.structureTypeCounts[result.dto.type!]++;
});
} else {
this.addControl();
this.structuresSource = new MatTableDataSource(this.proposedStructures);
}
}

addControl(type: STRUCTURE_TYPES | null = null, area: number | null = null) {
addControl(area: number | null = null): FormProposedStructure {
const areaStr = area ? area.toString(10) : null;
const newStructure = { type, area: areaStr, id: v4() };
const newStructure: FormProposedStructure = { type: null, area: areaStr, id: v4() };
this.proposedStructures.push(newStructure);
this.structuresForm.addControl(
`${newStructure.id}-type`,
new FormControl<string | null>(type, [Validators.required]),
new FormControl<string | null>(null, [Validators.required]),
);
this.structuresForm.addControl(
`${newStructure.id}-area`,
new FormControl<string | null>(areaStr, [Validators.required]),
);

if (type) {
this.structureTypeCounts[type]++;
}

this.structuresForm.markAsDirty();

return newStructure;
}

isWarning(index: number, item: ProposedStructure): boolean {
return (
item.type === STRUCTURE_TYPES.PRINCIPAL_RESIDENCE ||
item.type === STRUCTURE_TYPES.ADDITIONAL_RESIDENCE ||
item.type === STRUCTURE_TYPES.ACCESSORY_STRUCTURE
);
return item.type === STRUCTURE_TYPES.PRINCIPAL_RESIDENCE || item.type === STRUCTURE_TYPES.ADDITIONAL_RESIDENCE;
}

onStructureRemove(id: string) {
Expand Down Expand Up @@ -622,9 +627,8 @@ export class PfrsProposalComponent extends FilesStepComponent implements OnInit,
this.structuresForm.removeControl(`${id}-area`);
this.structuresForm.markAsDirty();

if (structureToDelete.type !== null && this.structureTypeCounts[structureToDelete.type] > 0) {
this.structureTypeCounts[structureToDelete.type]--;
}
this.updateStructureTypeFields();
this.updateStructureCounts(structureToDelete.type, null);
}

onStructureEdit(id: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
this.proposedStructures = [];
this.structuresForm = new FormGroup({});
for (const structure of applicationSubmission.soilProposedStructures) {
this.addControl(structure.type, structure.area);
const newStructure = this.addControl(structure.area);
if (structure.type !== null) {
this.setStructureTypeInput(newStructure, structure.type);
}
}
this.structuresSource = new MatTableDataSource(this.proposedStructures);

Expand Down Expand Up @@ -401,51 +404,56 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
}

private setStructureTypeInput(structure: FormProposedStructure, newType: STRUCTURE_TYPES) {
if (structure.type !== null && this.structureTypeCounts[structure.type] > 0) {
this.structureTypeCounts[structure.type]--;
}
this.structureTypeCounts[newType]++;
this.updateStructureCounts(structure.type, newType);

structure.type = newType;
this.structuresForm.get(`${structure.id}-type`)?.setValue(newType);

this.updateStructureTypeFields();
this.form.markAsDirty();
}

private updateStructureCounts(oldType: STRUCTURE_TYPES | null, newType: STRUCTURE_TYPES | null) {
if (oldType !== null && this.structureTypeCounts[oldType] > 0) {
this.structureTypeCounts[oldType]--;
}

if (newType !== null) {
this.structureTypeCounts[newType]++;
}
}

updateStructureTypeFields() {
// Remove

if (this.structureTypeCounts[STRUCTURE_TYPES.FARM_STRUCTURE] === 0) {
this.soilStructureFarmUseReason.removeValidators([Validators.required]);
this.soilStructureFarmUseReason.reset();
this.soilAgriParcelActivity.removeValidators([Validators.required]);
this.soilAgriParcelActivity.reset();
}

if (
this.structureTypeCounts[STRUCTURE_TYPES.PRINCIPAL_RESIDENCE] === 0 &&
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) {
this.soilStructureResidentialAccessoryUseReason.reset();
this.soilStructureResidentialAccessoryUseReason.removeValidators([Validators.required]);
}

if (this.structureTypeCounts[STRUCTURE_TYPES.ACCESSORY_STRUCTURE] === 0) {
this.soilStructureOtherUseReason.reset();
this.soilStructureOtherUseReason.removeValidators([Validators.required]);
}

// Add

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 (
Expand All @@ -454,14 +462,17 @@ 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();
}
}

Expand All @@ -479,43 +490,38 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
.beforeClosed()
.subscribe(async (result: { isEditing: boolean; structureId: string; dto: ProposedStructure }) => {
if (!result) return;
this.addControl(result.dto.type, result.dto.area);
const newStructure = this.addControl(result.dto.area);
if (result.dto.type !== null) {
this.setStructureTypeInput(newStructure, result.dto.type);
}
this.structuresSource = new MatTableDataSource(this.proposedStructures);

this.structureTypeCounts[result.dto.type!]++;
});
} else {
this.addControl();
this.structuresSource = new MatTableDataSource(this.proposedStructures);
}
}

addControl(type: STRUCTURE_TYPES | null = null, area: number | null = null) {
addControl(area: number | null = null): FormProposedStructure {
const areaStr = area ? area.toString(10) : null;
const newStructure = { type, area: areaStr, id: v4() };
const newStructure: FormProposedStructure = { type: null, area: areaStr, id: v4() };
this.proposedStructures.push(newStructure);
this.structuresForm.addControl(
`${newStructure.id}-type`,
new FormControl<string | null>(type, [Validators.required]),
new FormControl<string | null>(null, [Validators.required]),
);
this.structuresForm.addControl(
`${newStructure.id}-area`,
new FormControl<string | null>(areaStr, [Validators.required]),
);

if (type) {
this.structureTypeCounts[type]++;
}

this.structuresForm.markAsDirty();

return newStructure;
}

isWarning(index: number, item: ProposedStructure): boolean {
return (
item.type === STRUCTURE_TYPES.PRINCIPAL_RESIDENCE ||
item.type === STRUCTURE_TYPES.ADDITIONAL_RESIDENCE ||
item.type === STRUCTURE_TYPES.ACCESSORY_STRUCTURE
);
return item.type === STRUCTURE_TYPES.PRINCIPAL_RESIDENCE || item.type === STRUCTURE_TYPES.ADDITIONAL_RESIDENCE;
}

onStructureRemove(id: string) {
Expand Down Expand Up @@ -548,9 +554,8 @@ export class PofoProposalComponent extends FilesStepComponent implements OnInit,
this.structuresForm.removeControl(`${id}-area`);
this.structuresForm.markAsDirty();

if (structureToDelete.type !== null && this.structureTypeCounts[structureToDelete.type] > 0) {
this.structureTypeCounts[structureToDelete.type]--;
}
this.updateStructureTypeFields();
this.updateStructureCounts(structureToDelete.type, null);
}

onStructureEdit(id: string) {
Expand Down
Loading

0 comments on commit 6930ffd

Please sign in to comment.