@ -14,9 +14,11 @@
/// limitations under the License.
///
import { Component , Inject } from '@angular/core' ;
import { Component , DestroyRef , Inject } from '@angular/core' ;
import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
import { MAT_DIALOG_DATA , MatDialog , MatDialogRef } from '@angular/material/dialog' ;
import { Router } from '@angular/router' ;
import { FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms' ;
import { Store } from '@ngrx/store' ;
import { AppState } from '@core/core.state' ;
import { DialogComponent } from '@shared/components/dialog.component' ;
@ -70,15 +72,21 @@ export type InstallState =
export class TbIotHubInstallDialogComponent extends DialogComponent < TbIotHubInstallDialogComponent > {
ItemType = ItemType ;
EntityType = EntityType ;
item : MpItemVersionView ;
typeTranslations = itemTypeTranslations ;
state : InstallState = 'confirm' ;
state ! : InstallState ;
errorMessage = '' ;
entityDetailsUrl : string | null = null ;
selectedEntityId : EntityId | null = null ;
pendingOverwrite : PendingOverwrite | null = null ;
ruleChainInstallForm ! : FormGroup < {
setAsDefault : FormControl < boolean > ;
entityType : FormControl < EntityType > ;
entityId : FormControl < string | null > ;
} > ;
private readonly selectEntityConfig : Partial < Record < ItemType , SelectEntityConfig > > = {
[ ItemType . CALCULATED_FIELD ] : {
@ -92,13 +100,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
defaultType : EntityType.DEVICE_PROFILE ,
required : true ,
promptKey : 'iot-hub.select-entity-for-alarm-rule' ,
} ,
[ ItemType . RULE_CHAIN ] : {
allowed : [ EntityType . DEVICE_PROFILE , EntityType . ASSET_PROFILE ] ,
defaultType : EntityType.DEVICE_PROFILE ,
required : true ,
promptKey : 'iot-hub.select-profile-for-rule-chain' ,
} ,
}
} ;
get activeSelectEntityConfig ( ) : SelectEntityConfig | null {
@ -115,10 +117,47 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
private iotHubApiService : IotHubApiService ,
private deviceProfileService : DeviceProfileService ,
private assetProfileService : AssetProfileService ,
private ruleChainService : RuleChainService
private ruleChainService : RuleChainService ,
private fb : FormBuilder ,
private destroyRef : DestroyRef
) {
super ( store , router , dialogRef ) ;
this . item = data . item ;
this . state = this . computeInitialState ( ) ;
if ( this . item . type === ItemType . RULE_CHAIN ) {
this . initRuleChainForm ( ) ;
}
}
private computeInitialState ( ) : InstallState {
return this . activeSelectEntityConfig || this . item . type === ItemType . RULE_CHAIN
? 'select-entity'
: 'confirm' ;
}
private initRuleChainForm ( ) : void {
this . ruleChainInstallForm = this . fb . group ( {
setAsDefault : this.fb.nonNullable.control < boolean > ( false ) ,
entityType : this.fb.nonNullable.control < EntityType > ( EntityType . DEVICE_PROFILE ) ,
entityId : this.fb.control < string | null > ( null )
} ) ;
this . ruleChainInstallForm . controls . setAsDefault . valueChanges
. pipe ( takeUntilDestroyed ( this . destroyRef ) )
. subscribe ( setAsDefault = > {
const entityIdCtrl = this . ruleChainInstallForm . controls . entityId ;
if ( setAsDefault ) {
entityIdCtrl . setValidators ( Validators . required ) ;
} else {
entityIdCtrl . clearValidators ( ) ;
entityIdCtrl . setValue ( null ) ;
}
entityIdCtrl . updateValueAndValidity ( ) ;
} ) ;
this . ruleChainInstallForm . controls . entityType . valueChanges
. pipe ( takeUntilDestroyed ( this . destroyRef ) )
. subscribe ( ( ) = > {
this . ruleChainInstallForm . controls . entityId . setValue ( null ) ;
} ) ;
}
getTypeLabel ( ) : string {
@ -126,39 +165,33 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
return key ? this . translate . instant ( key ) : '' ;
}
install ( ) : void {
if ( this . item . type === ItemType . CALCULATED_FIELD || this . item . type === ItemType . ALARM_RULE ) {
this . state = 'select-entity' ;
onEntitySelectInstall ( ) : void {
if ( ! this . selectedEntityId ) {
return ;
}
this . doI nstall( ) ;
this . i nstall( ) ;
}
installAsEntityProfileDefault ( ) : void {
this . state = 'select-entity' ;
}
selectEntityBack ( ) : void {
this . selectedEntityId = null ;
this . state = 'confirm' ;
}
onSelectEntityInstall ( ) : void {
if ( ! this . selectedEntityId ) {
onRuleChainInstall ( ) : void {
const { setAsDefault , entityType , entityId } = this . ruleChainInstallForm . getRawValue ( ) ;
if ( ! setAsDefault ) {
this . selectedEntityId = null ;
this . install ( ) ;
return ;
}
if ( this . item . type !== ItemType . RULE_CHAIN ) {
this . doInstall ( ) ;
if ( ! entityId ) {
return ;
}
this . resolveOverwrite ( this . selectedEntityId ) . subscribe ( {
const profileEntityId : EntityId = { entityType , id : entityId } ;
this . selectedEntityId = profileEntityId ;
this . resolveOverwrite ( profileEntityId ) . subscribe ( {
next : ( pending ) = > {
if ( pending ) {
this . pendingOverwrite = pending ;
this . state = 'confirm-overwrite' ;
} else {
this . pendingOverwrite = null ;
this . doI nstall( ) ;
this . i nstall( ) ;
}
} ,
error : ( err ) = > {
@ -170,7 +203,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
}
confirmOverwriteReplace ( ) : void {
this . doI nstall( ) ;
this . i nstall( ) ;
}
confirmOverwriteCancel ( ) : void {
@ -204,7 +237,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
) ;
}
doI nstall( ) : void {
i nstall( ) : void {
this . state = 'installing' ;
const versionId = this . item . id as string ;
const data = this . selectedEntityId ? { entityId : this.selectedEntityId } : undefined ;