1- import { Component , EventEmitter , OnDestroy , OnInit , Output } from '@angular/core' ;
1+ import {
2+ Component ,
3+ EventEmitter ,
4+ OnDestroy ,
5+ OnInit ,
6+ Output ,
7+ } from '@angular/core' ;
28import { Title } from '@angular/platform-browser' ;
39import { ActivatedRoute , Router } from '@angular/router' ;
410import { Application } from '@applications/application.model' ;
@@ -12,95 +18,106 @@ import { MeService } from '@shared/services/me.service';
1218import { Subscription } from 'rxjs' ;
1319
1420@Component ( {
15- selector : 'app-application' ,
16- templateUrl : './application-detail.component.html' ,
17- styleUrls : [ './application-detail.component.scss' ] ,
21+ selector : 'app-application' ,
22+ templateUrl : './application-detail.component.html' ,
23+ styleUrls : [ './application-detail.component.scss' ] ,
1824} )
1925export class ApplicationDetailComponent implements OnInit , OnDestroy {
20- @Output ( ) deleteApplication = new EventEmitter ( ) ;
21- public applicationsSubscription : Subscription ;
22- private deleteDialogSubscription : Subscription ;
23- public application : Application ;
24- public backButton : BackButton = { label : '' , routerLink : '/applications' } ;
25- public id : number ;
26- public pageLimit = environment . tablePageSize ;
27- public dropdownButton : DropdownButton ;
28- public errorMessage : string ;
29- public canEdit = false ;
26+ @Output ( ) deleteApplication = new EventEmitter ( ) ;
27+ public applicationsSubscription : Subscription ;
28+ private deleteDialogSubscription : Subscription ;
29+ public application : Application ;
30+ public backButton : BackButton = { label : '' , routerLink : '/applications' } ;
31+ public id : number ;
32+ public pageLimit = environment . tablePageSize ;
33+ public dropdownButton : DropdownButton ;
34+ public errorMessage : string ;
35+ public canEdit = false ;
3036
31- constructor (
32- private applicationService : ApplicationService ,
33- private route : ActivatedRoute ,
34- public translate : TranslateService ,
35- public router : Router ,
36- private meService : MeService ,
37- private titleService : Title ,
38- private deleteDialogService : DeleteDialogService
39- ) { }
37+ constructor (
38+ private applicationService : ApplicationService ,
39+ private route : ActivatedRoute ,
40+ public translate : TranslateService ,
41+ public router : Router ,
42+ private meService : MeService ,
43+ private titleService : Title ,
44+ private deleteDialogService : DeleteDialogService
45+ ) { }
4046
41- ngOnInit ( ) : void {
42- this . id = + this . route . snapshot . paramMap . get ( 'id' ) ;
43- if ( this . id ) {
44- this . bindApplication ( this . id ) ;
45- this . dropdownButton = {
46- label : '' ,
47- editRouterLink : '../../edit-application/' + this . id ,
48- isErasable : true ,
49- } ;
47+ ngOnInit ( ) : void {
48+ this . id = + this . route . snapshot . paramMap . get ( 'id' ) ;
49+ if ( this . id ) {
50+ this . bindApplication ( this . id ) ;
51+ this . dropdownButton = {
52+ label : '' ,
53+ editRouterLink : '../../edit-application/' + this . id ,
54+ isErasable : true ,
55+ } ;
5056
51- console . log ( this . id ) ;
52- }
57+ console . log ( this . id ) ;
58+ }
5359
54- this . translate . get ( [ 'NAV.APPLICATIONS' , 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' , 'TITLE.APPLICATION' ] )
55- . subscribe ( translations => {
56- this . backButton . label = translations [ 'NAV.APPLICATIONS' ] ;
57- this . dropdownButton . label = translations [ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ] ;
58- this . titleService . setTitle ( translations [ 'TITLE.APPLICATION' ] ) ;
59- } ) ;
60- this . canEdit = this . meService . canWriteInTargetOrganization ( ) ;
60+ this . translate
61+ . get ( [
62+ 'NAV.APPLICATIONS' ,
63+ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ,
64+ 'TITLE.APPLICATION' ,
65+ ] )
66+ . subscribe ( ( translations ) => {
67+ this . backButton . label = translations [ 'NAV.APPLICATIONS' ] ;
68+ this . dropdownButton . label =
69+ translations [ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ] ;
70+ this . titleService . setTitle ( translations [ 'TITLE.APPLICATION' ] ) ;
71+ } ) ;
72+ this . canEdit = this . meService . canWriteInTargetOrganization ( ) ;
73+ }
74+
75+ onDeleteApplication ( ) {
76+ let message : string ;
77+ if ( this . applicationHasDevices ( ) ) {
78+ message = this . translate . instant ( 'APPLICATION.DELETE-HAS-DEVICES-PROMPT' ) ;
6179 }
6280
63- onDeleteApplication ( ) {
64- let message : string ;
65- if ( this . applicationHasDevices ( ) ) {
66- message = this . translate . instant ( 'APPLICATION.DELETE-HAS-DEVICES-PROMPT' ) ;
81+ this . deleteDialogSubscription = this . deleteDialogService
82+ . showSimpleDialog ( message )
83+ . subscribe ( ( response ) => {
84+ if ( response ) {
85+ this . applicationService
86+ . deleteApplication ( this . application . id )
87+ . subscribe ( ( response ) => {
88+ if ( response . ok && response . body . affected > 0 ) {
89+ console . log (
90+ 'delete application with id:' + this . application . id . toString ( )
91+ ) ;
92+ this . router . navigate ( [ 'applications' ] ) ;
93+ } else {
94+ this . errorMessage = response ?. error ?. message ;
95+ }
96+ } ) ;
97+ } else {
98+ console . log ( response ) ;
6799 }
100+ } ) ;
101+ }
68102
69- this . deleteDialogSubscription = this . deleteDialogService . showSimpleDialog ( message ) . subscribe (
70- ( response ) => {
71- if ( response ) {
72- this . applicationService . deleteApplication ( this . application . id ) . subscribe ( ( response ) => {
73- if ( response . ok && response . body . affected > 0 ) {
74- console . log ( 'delete application with id:' + this . application . id . toString ( ) ) ;
75- this . router . navigate ( [ 'applications' ] ) ;
76- } else {
77- this . errorMessage = response ?. error ?. message ;
78- }
79- } ) ;
80- } else {
81- console . log ( response ) ;
82- }
83- }
84- ) ;
85- }
103+ applicationHasDevices ( ) : boolean {
104+ return this . application . iotDevices ?. length > 0 ;
105+ }
86106
87- applicationHasDevices ( ) : boolean {
88- return this . application . iotDevices ?. length > 0 ;
89- }
107+ bindApplication ( id : number ) : void {
108+ this . applicationsSubscription = this . applicationService
109+ . getApplication ( id )
110+ . subscribe ( ( application ) => {
111+ this . application = application ;
112+ } ) ;
113+ }
90114
91-
92- bindApplication ( id : number ) : void {
93- this . applicationsSubscription = this . applicationService . getApplication ( id ) . subscribe ( ( application ) => {
94- this . application = application ;
95- } ) ;
115+ ngOnDestroy ( ) {
116+ if ( this . applicationsSubscription ) {
117+ this . applicationsSubscription . unsubscribe ( ) ;
96118 }
97-
98- ngOnDestroy ( ) {
99- if ( this . applicationsSubscription ) {
100- this . applicationsSubscription . unsubscribe ( ) ;
101- }
102- if ( this . deleteDialogSubscription ) {
103- this . deleteDialogSubscription . unsubscribe ( ) ;
104- }
119+ if ( this . deleteDialogSubscription ) {
120+ this . deleteDialogSubscription . unsubscribe ( ) ;
105121 }
122+ }
106123}
0 commit comments