import { UserService } from './../../shared/services/user.service' ; import { Links , RuntimeImage } from './../../shared/models/constants' ; import { Component , OnInit , OnDestroy , Input , Injector } from '@angular/core' ; import { FormBuilder , FormGroup } from '@angular/forms' ; import { CustomFormControl } from './../../controls/click-to-edit/click-to-edit.component' ; import { TranslateService } from '@ngx-translate/core' ; import { Subject } from 'rxjs/Subject' ; import { errorIds } from './../../shared/models/error-ids' ; import { BroadcastService } from './../../shared/services/broadcast.service' ; import { AiService } from './../../shared/services/ai.service' ; import { ArmObj } from './../../shared/models/arm/arm-obj' ; import { CacheService } from './../../shared/services/cache.service' ; import { GlobalStateService } from './../../shared/services/global-state.service' ; import { SiteNameValidator } from './../../shared/validators/siteNameValidator' ; import { DropDownElement } from './../../shared/models/drop-down-element' ; import { AppsNode } from './../../tree-view/apps-node' ; import { PortalResources } from './../../shared/models/portal-resources' ; import { TreeViewInfo } from './../../tree-view/models/tree-view-info' ; import { RequiredValidator } from 'app/shared/validators/requiredValidator' ; import { Site } from 'app/shared/models/arm/site' ; import { BroadcastEvent } from 'app/shared/models/broadcast-event' ; import { DashboardType } from 'app/tree-view/models/dashboard-type' ; import { ErrorableComponent } from 'app/shared/components/errorable-component' ; @ Component ( { selector : 'create-app' , templateUrl : './create-app.component.html' , styleUrls : [ './create-app.component.scss' ] , } ) export class CreateAppComponent extends ErrorableComponent implements OnInit , OnDestroy { public Resources = PortalResources ; public group : FormGroup ; public viewInfoStream : Subject < TreeViewInfo < any > > ; public FwdLinks = Links ; public subscriptionOptions : DropDownElement < string > [ ] = [ ] ; public runtimeImageOptions : DropDownElement < string > [ ] = [ ] ; private _viewInfo : TreeViewInfo < any > ; private _ngUnsubscribe = new Subject < void > ( ) ; constructor ( broadcastService , private _cacheService , private _globalStateService , private _translateService , _fb , private _aiService , userService , injector ) { super ( 'create-app' , broadcastService ) ; userService . getStartupInfo ( ) . first ( ) . subscribe ( info => { const subs = info . subscriptions ; let defaultSubId ; subs . forEach ( sub => { if ( sub . state === 'Enabled' ) { this . subscriptionOptions . push ( { displayLabel : `${ sub . displayName }(${ sub . subscriptionId })` , value : sub . subscriptionId , } ) ; if ( ! defaultSubId ) { defaultSubId = sub . subscriptionId ; } } } ) ; const sub = subs . find ( s => s . state === 'Enabled' ) ; if ( ! sub ) { return ; } const required = new RequiredValidator ( this . _translateService ) ; const siteNameValidator = new SiteNameValidator ( injector , sub . subscriptionId ) ; this . group = _fb . group ( { name : [ null , required . validate . bind ( required ) , siteNameValidator . validate . bind ( siteNameValidator ) ] , subscription : defaultSubId , runtimeImage : RuntimeImage . v2 , } ) ; } ) ; this . runtimeImageOptions . push ( { displayLabel : this . _translateService . instant ( PortalResources . runtimeImagev1 ) , value : RuntimeImage . v1 , } ) ; this . runtimeImageOptions . push ( { displayLabel : this . _translateService . instant ( PortalResources . runtimeImagev2 ) , value : RuntimeImage . v2 , default : true , } ) ; this . viewInfoStream = new Subject < TreeViewInfo < any > > ( ) ; this . viewInfoStream . subscribe ( viewInfo => { this . _viewInfo = viewInfo ; } ) ; } @ Input ( ) set viewInfoInput ( viewInfo ) { this . viewInfoStream . next ( viewInfo ) ; } ngOnInit ( ) { this . _broadcastService . getEvents < TreeViewInfo < any > > ( BroadcastEvent . TreeNavigation ) . filter ( info => { return info . dashboardType === DashboardType . createApp ; } ) . takeUntil ( this . _ngUnsubscribe ) . subscribe ( info => { this . viewInfoStream . next ( info ) ; } ) ; } ngOnDestroy ( ) { this . _ngUnsubscribe . next ( ) ; } create ( ) { const nameControl = < CustomFormControl > this . group . controls [ 'name' ] ; const name = nameControl . value ; if ( ! name ) { nameControl . _msRunValidation = true ; nameControl . updateValueAndValidity ( ) ; return ; } const id = `/subscriptions/${ this . group . controls [ 'subscription' ] . value }/resourceGroups/StandaloneResourceGroup/providers/Microsoft.Web/sites/${ name }` ; const body = { properties : { siteConfig : { appSettings : [ ] , } , sku : 'Dynamic' , clientAffinityEnabled : false , runtimeImage : this . group . controls [ 'runtimeImage' ] . value , } , location : 'local' , kind : 'functionapp' , } ; this . _globalStateService . setBusyState ( ) ; this . _cacheService . putArm ( id , null , body ) . subscribe ( r => { this . _globalStateService . clearBusyState ( ) ; const siteObj = r . json ( ) ; const appsNode = < AppsNode > this . _viewInfo . node ; appsNode . addChild ( siteObj ) ; } , error => { this . _globalStateService . clearBusyState ( ) ; this . showComponentError ( { message : this . _translateService . instant ( PortalResources . createApp_fail ) , details : this . _translateService . instant ( PortalResources . createApp_fail ) , errorId : errorIds . failedToCreateApp , resourceId : id , } ) ; this . _aiService . trackEvent ( errorIds . failedToCreateApp , { error : error , id : id } ) ; } ) ; } }