import { Injector , OnDestroy } from '@angular/core' ; import { ArmObj , ResourceId } from 'app/shared/models/arm/arm-obj' ; import { ApplicationSettings } from 'app/shared/models/arm/application-settings' ; import { ConnectionStrings } from 'app/shared/models/arm/connection-strings' ; import { Site } from 'app/shared/models/arm/site' ; import { SiteConfig } from 'app/shared/models/arm/site-config' ; import { SlotConfigNames } from 'app/shared/models/arm/slot-config-names' ; import { FeatureComponent } from './feature-component' ; import { BusyStateName } from 'app/busy-state/busy-state.component' ; import { ByosStorageAccounts } from 'app/site/byos/byos' ; export interface ArmSaveConfigs { appSettingsArm ? : ArmObj < ApplicationSettings > ; connectionStringsArm ? : ArmObj < ConnectionStrings > ; siteArm ? : ArmObj < Site > ; siteConfigArm ? : ArmObj < SiteConfig > ; slotConfigNamesArm ? : ArmObj < SlotConfigNames > ; azureStorageAccountsArm ? : ArmObj < ByosStorageAccounts > ; } export interface ArmSaveResult < T > { success : boolean ; error ? : string ; value ? : ArmObj < T > ; } export interface ArmSaveResults { appSettings ? : ArmSaveResult < ApplicationSettings > ; connectionStrings ? : ArmSaveResult < ConnectionStrings > ; site ? : ArmSaveResult < Site > ; siteConfig ? : ArmSaveResult < SiteConfig > ; slotConfigNames ? : ArmSaveResult < SlotConfigNames > ; azureStorageAccounts ? : ArmSaveResult < ByosStorageAccounts > ; } type ConfigType = 'ApplicationSettings' | 'ConnectionStrings' | 'Site' | 'SiteConfig' | 'SlotConfigNames' | 'AzureStorageAccounts' ; interface ConfigState { isNeeded : boolean ; wasSubmitted : boolean ; } interface ConfigStateMap { [ key ] : ConfigState ; } export abstract class ConfigSaveComponent extends FeatureComponent < ResourceId > implements ArmSaveConfigs , OnDestroy { public appSettingsArm : ArmObj < ApplicationSettings > ; public connectionStringsArm : ArmObj < ConnectionStrings > ; public siteArm : ArmObj < Site > ; public siteConfigArm : ArmObj < SiteConfig > ; public slotConfigNamesArm : ArmObj < SlotConfigNames > ; public azureStorageAccountsArm : ArmObj < ByosStorageAccounts > ; protected _saveFailed : boolean ; private readonly _configTypes : ConfigType [ ] = [ 'ApplicationSettings' , 'ConnectionStrings' , 'Site' , 'SiteConfig' , 'SlotConfigNames' , 'AzureStorageAccounts' , ] ; private _configStates : ConfigStateMap ; protected abstract get _isPristine ( ) : boolean ; protected abstract _getConfigsFromForms ( saveConfigs ) ; constructor ( componentName , injector , configTypesUsed , busyComponentName ? ) { super ( componentName , injector , busyComponentName ) ; this . _resetSubmittedStates ( ) ; configTypesUsed . forEach ( t => ( this . _configStates [ t ] . isNeeded = true ) ) ; } protected _resetConfigs ( ) { this . appSettingsArm = null ; this . connectionStringsArm = null ; this . siteArm = null ; this . siteConfigArm = null ; this . slotConfigNamesArm = null ; this . azureStorageAccountsArm = null ; } protected _resetSubmittedStates ( ) { this . _configTypes . forEach ( t => this . _setSubmittedState ( t , false ) ) ; } private _setSubmittedState ( configType , submitted ) { this . _configStates = this . _configStates || { } ; if ( ! this . _configStates [ configType ] ) { this . _configStates [ configType ] = { isNeeded : submitted , wasSubmitted : submitted } ; } else { this . _configStates [ configType ] . wasSubmitted = submitted ; } } private _checkIfSubmitted ( configType ) { return this . _configStates [ configType ] && this . _configStates [ configType ] . wasSubmitted ; } private _checkIfNeeded ( configType ) { return this . _configStates [ configType ] && this . _configStates [ configType ] . isNeeded ; } private _checkConfigExistence ( configType , sourceConfigs ) { switch ( configType ) { case 'ApplicationSettings' : return ! ! sourceConfigs . appSettingsArm ; case 'ConnectionStrings' : return ! ! sourceConfigs . connectionStringsArm ; case 'Site' : return ! ! sourceConfigs . siteArm ; case 'SiteConfig' : return ! ! sourceConfigs . siteConfigArm ; case 'SlotConfigNames' : return ! ! sourceConfigs . slotConfigNamesArm ; case 'AzureStorageAccounts' : return ! ! sourceConfigs . azureStorageAccountsArm ; default : return false ; } } private _assignConfig ( configType , sourceConfigs , destConfigs ? ) { destConfigs = destConfigs || this ; switch ( configType ) { case 'ApplicationSettings' : destConfigs . appSettingsArm = sourceConfigs . appSettingsArm ; break ; case 'ConnectionStrings' : destConfigs . connectionStringsArm = sourceConfigs . connectionStringsArm ; break ; case 'Site' : destConfigs . siteArm = sourceConfigs . siteArm ; break ; case 'SiteConfig' : destConfigs . siteConfigArm = sourceConfigs . siteConfigArm ; break ; case 'SlotConfigNames' : destConfigs . slotConfigNamesArm = sourceConfigs . slotConfigNamesArm ; break ; case 'AzureStorageAccounts' : destConfigs . azureStorageAccountsArm = sourceConfigs . azureStorageAccountsArm ; break ; default : break ; } } private _assignConfigFromResults ( configType , results , destConfigs ? ) { const configs = { appSettingsArm : results . appSettings ? results . appSettings . value : null , connectionStringsArm : results . connectionStrings ? results . connectionStrings . value : null , siteArm : results . site ? results . site . value : null , siteConfigArm : results . siteConfig ? results . siteConfig . value : null , slotConfigNamesArm : results . slotConfigNames ? results . slotConfigNames . value : null , azureStorageAccountsArm : results . azureStorageAccounts ? results . azureStorageAccounts . value : null , } ; this . _assignConfig ( configType , configs , destConfigs ) ; } private _appendSaveConfig ( configType , saveConfigs , updatedConfigs ) { if ( this . _checkConfigExistence ( configType , updatedConfigs ) ) { this . _assignConfig ( configType , updatedConfigs , saveConfigs ) ; this . _setSubmittedState ( configType , true ) ; } } getSaveConfigs ( saveConfigs ) { this . _saveFailed = false ; this . _resetSubmittedStates ( ) ; if ( ! this . _isPristine ) { const updatedConfigs = this . _getConfigsFromForms ( saveConfigs ) ; this . _configTypes . forEach ( t => this . _appendSaveConfig ( t , saveConfigs , updatedConfigs ) ) ; } } private _checkResultSuccess ( configType , results ) { let result ; switch ( configType ) { case 'ApplicationSettings' : result = results . appSettings ; break ; case 'ConnectionStrings' : result = results . connectionStrings ; break ; case 'Site' : result = results . site ; break ; case 'SiteConfig' : result = results . siteConfig ; break ; case 'SlotConfigNames' : result = results . slotConfigNames ; break ; case 'AzureStorageAccounts' : result = results . azureStorageAccounts ; break ; default : break ; } return ! ! result && result . success && ! ! result . value ; } private _processSaveResult ( configType , results ) { const isNeeded = this . _checkIfNeeded ( configType ) ; const wasSubmitted = this . _checkIfSubmitted ( configType ) ; this . _setSubmittedState ( configType , false ) ; if ( isNeeded || wasSubmitted ) { if ( this . _checkResultSuccess ( configType , results ) ) { this . _assignConfigFromResults ( configType , results ) ; } else if ( wasSubmitted ) { this . _saveFailed = true ; } } } processSaveResults ( results ) { if ( results ) { this . _configTypes . forEach ( t => this . _processSaveResult ( t , results ) ) ; } else { } } ngOnDestroy ( ) { super . ngOnDestroy ( ) ; this . clearBusy ( ) ; } }