@@ -331,22 +331,26 @@ export class AdditionalInformationComponent extends FilesStepComponent implement
331
331
}
332
332
}
333
333
334
- private checkStructureTypeInput ( type : STRUCTURE_TYPES | null ) {
335
- switch ( type ) {
336
- case STRUCTURE_TYPES . FARM_STRUCTURE :
337
- return ! ! ( this . soilAgriParcelActivity . value || this . soilStructureFarmUseReason . value ) ;
338
- case STRUCTURE_TYPES . ACCESSORY_STRUCTURE :
339
- return ! ! (
340
- this . soilStructureResidentialUseReason . value || this . soilStructureResidentialAccessoryUseReason . value
341
- ) ;
342
- case STRUCTURE_TYPES . OTHER_STRUCTURE :
343
- return ! ! this . soilStructureOtherUseReason . value ;
344
- case STRUCTURE_TYPES . PRINCIPAL_RESIDENCE :
345
- case STRUCTURE_TYPES . ADDITIONAL_RESIDENCE :
346
- return ! ! this . soilStructureResidentialUseReason . value ;
347
- case null :
348
- return false ;
349
- }
334
+ private structureChangeRequiresConfirmation ( oldType : STRUCTURE_TYPES | null , newType : STRUCTURE_TYPES ) : boolean {
335
+ const residentialTypes = [
336
+ STRUCTURE_TYPES . PRINCIPAL_RESIDENCE ,
337
+ STRUCTURE_TYPES . ADDITIONAL_RESIDENCE ,
338
+ STRUCTURE_TYPES . ACCESSORY_STRUCTURE ,
339
+ ] ;
340
+ const changingFromResidentialType = oldType && residentialTypes . includes ( oldType ) ;
341
+ const changingToResidentialType = newType && residentialTypes . includes ( newType ) ;
342
+
343
+ return ! ! (
344
+ oldType !== newType &&
345
+ ( ( oldType &&
346
+ oldType === STRUCTURE_TYPES . FARM_STRUCTURE &&
347
+ ( this . soilAgriParcelActivity . value || this . soilStructureFarmUseReason . value ) ) ||
348
+ ( changingFromResidentialType && ! changingToResidentialType && this . soilStructureResidentialUseReason . value ) ||
349
+ ( oldType &&
350
+ oldType === STRUCTURE_TYPES . ACCESSORY_STRUCTURE &&
351
+ this . soilStructureResidentialAccessoryUseReason . value ) ||
352
+ ( oldType && oldType === STRUCTURE_TYPES . OTHER_STRUCTURE && this . soilStructureOtherUseReason . value ) )
353
+ ) ;
350
354
}
351
355
352
356
private setStructureTypeInput ( structure : FormProposedStructure , newType : STRUCTURE_TYPES ) {
@@ -362,18 +366,16 @@ export class AdditionalInformationComponent extends FilesStepComponent implement
362
366
this . form . markAsDirty ( ) ;
363
367
}
364
368
365
- onChangeStructureType ( id : string , value : STRUCTURE_TYPES ) {
369
+ onChangeStructureType ( id : string , newType : STRUCTURE_TYPES ) {
366
370
const structure = this . proposedStructures . find ( ( structure ) => structure . id === id ) ;
367
371
if ( ! structure ) {
368
372
console . error ( 'Failed to find structure' ) ;
369
373
return ;
370
374
}
371
375
this . structuresSource = new MatTableDataSource ( this . proposedStructures ) ;
372
- const prevType = structure . type ;
373
- const hasInput = this . checkStructureTypeInput ( prevType ) ;
374
-
375
- if ( ! hasInput ) {
376
- return this . setStructureTypeInput ( structure , value ) ;
376
+ const oldType = structure . type ;
377
+ if ( ! this . structureChangeRequiresConfirmation ( oldType , newType ) ) {
378
+ return this . setStructureTypeInput ( structure , newType ) ;
377
379
} else {
378
380
const dialog = this . confirmationDialogService . openDialog ( {
379
381
title : 'Change Structure Type' ,
@@ -384,9 +386,9 @@ export class AdditionalInformationComponent extends FilesStepComponent implement
384
386
385
387
dialog . subscribe ( ( isConfirmed ) => {
386
388
if ( isConfirmed ) {
387
- this . setStructureTypeInput ( structure , value ) ;
389
+ this . setStructureTypeInput ( structure , newType ) ;
388
390
} else {
389
- this . structuresForm . get ( structure . id + '-type' ) ?. setValue ( prevType ) ;
391
+ this . structuresForm . get ( structure . id + '-type' ) ?. setValue ( oldType ) ;
390
392
}
391
393
} ) ;
392
394
}
0 commit comments