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,110 +18,92 @@ 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 dropdownButton : DropdownButton ;
27- public errorMessage : string ;
28- public canEdit = false ;
26+ @Output ( ) deleteApplication = new EventEmitter ( ) ;
27+ public applicationsSubscription : Subscription ;
28+ public application : Application ;
29+ public backButton : BackButton = { label : '' , routerLink : '/applications' } ;
30+ public id : number ;
31+ public dropdownButton : DropdownButton ;
32+ public errorMessage : string ;
33+ public canEdit = false ;
2934
30- constructor (
31- private applicationService : ApplicationService ,
32- private route : ActivatedRoute ,
33- public translate : TranslateService ,
34- public router : Router ,
35- private meService : MeService ,
36- private titleService : Title ,
37- private deleteDialogService : DeleteDialogService
38- ) { }
35+ constructor (
36+ private applicationService : ApplicationService ,
37+ private route : ActivatedRoute ,
38+ public translate : TranslateService ,
39+ public router : Router ,
40+ private meService : MeService ,
41+ private titleService : Title ,
42+ private deleteDialogService : DeleteDialogService
43+ ) { }
3944
40- ngOnInit ( ) : void {
41- this . id = + this . route . snapshot . paramMap . get ( 'id' ) ;
42- if ( this . id ) {
43- this . bindApplication ( this . id ) ;
44- this . dropdownButton = {
45- label : '' ,
46- editRouterLink : '../../edit-application/' + this . id ,
47- isErasable : true ,
48- } ;
45+ ngOnInit ( ) : void {
46+ this . id = + this . route . snapshot . paramMap . get ( 'id' ) ;
47+ if ( this . id ) {
48+ this . bindApplication ( this . id ) ;
49+ this . dropdownButton = {
50+ label : '' ,
51+ editRouterLink : '../../edit-application/' + this . id ,
52+ isErasable : true ,
53+ } ;
4954
50- console . log ( this . id ) ;
51- }
52-
53- this . translate . get ( [ 'NAV.APPLICATIONS' , 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' , 'TITLE.APPLICATION' ] )
54- . subscribe ( translations => {
55- this . backButton . label = translations [ 'NAV.APPLICATIONS' ] ;
56- this . dropdownButton . label = translations [ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ] ;
57- this . titleService . setTitle ( translations [ 'TITLE.APPLICATION' ] ) ;
58- } ) ;
59- this . canEdit = this . meService . canWriteInTargetOrganization ( ) ;
55+ console . log ( this . id ) ;
6056 }
6157
62- onDeleteApplication ( ) {
63-
64- let message : string ;
65- let showAccept : boolean = true ;
66- const hasSigfoxDevices : boolean = this . applicationHasSigFoxDevices ( ) ;
67-
68- if ( hasSigfoxDevices ) {
69- message = this . translate . instant (
70- 'APPLICATION.DELETE-HAS-SIGFOX-DEVICES-PROMPT'
71- ) ;
72- showAccept = false ;
58+ this . translate
59+ . get ( [
60+ 'NAV.APPLICATIONS' ,
61+ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ,
62+ 'TITLE.APPLICATION' ,
63+ ] )
64+ . subscribe ( ( translations ) => {
65+ this . backButton . label = translations [ 'NAV.APPLICATIONS' ] ;
66+ this . dropdownButton . label =
67+ translations [ 'APPLICATION-TABLE-ROW.SHOW-OPTIONS' ] ;
68+ this . titleService . setTitle ( translations [ 'TITLE.APPLICATION' ] ) ;
69+ } ) ;
70+ this . canEdit = this . meService . canWriteInTargetOrganization ( ) ;
71+ }
7372
74- } else if ( this . applicationHasDevices ( ) ) {
75- message = this . translate . instant ( 'APPLICATION.DELETE-HAS-DEVICES-PROMPT' ) ;
73+ onDeleteApplication ( ) {
74+ this . deleteDialogService
75+ . showApplicationDialog ( this . application )
76+ . subscribe ( ( response ) => {
77+ if ( response ) {
78+ this . applicationService
79+ . deleteApplication ( this . application . id )
80+ . subscribe ( ( response ) => {
81+ if ( response . ok && response . body . affected > 0 ) {
82+ console . log (
83+ 'delete application with id:' + this . application . id . toString ( )
84+ ) ;
85+ this . router . navigate ( [ 'applications' ] ) ;
86+ } else {
87+ this . errorMessage = response ?. error ?. message ;
88+ }
89+ } ) ;
90+ } else {
91+ console . log ( response ) ;
7692 }
93+ } ) ;
94+ }
7795
78- this . deleteDialogSubscription = this . deleteDialogService . showSimpleDialog ( message , showAccept ) . subscribe (
79- ( response ) => {
80- if ( response ) {
81- this . applicationService . deleteApplication ( this . application . id ) . subscribe ( ( response ) => {
82- if ( response . ok && response . body . affected > 0 ) {
83- console . log ( 'delete application with id:' + this . application . id . toString ( ) ) ;
84- this . router . navigate ( [ 'applications' ] ) ;
85- } else {
86- this . errorMessage = response ?. error ?. message ;
87- }
88- } ) ;
89- } else {
90- console . log ( response ) ;
91- }
92- }
93- ) ;
94- }
95-
96- applicationHasDevices ( ) : boolean {
97- return this . application . iotDevices ?. length > 0 ;
98- }
99-
100- applicationHasSigFoxDevices ( ) : boolean {
101- const sigfoxDevice = this . application . iotDevices . find ( ( device ) => {
102- return device . type === DeviceType . SIGFOX ;
103- } ) ;
104- return sigfoxDevice !== undefined ;
105- }
96+ bindApplication ( id : number ) : void {
97+ this . applicationsSubscription = this . applicationService
98+ . getApplication ( id )
99+ . subscribe ( ( application ) => {
100+ this . application = application ;
101+ } ) ;
102+ }
106103
107- bindApplication ( id : number ) : void {
108- this . applicationsSubscription = this . applicationService . getApplication ( id ) . subscribe ( ( application ) => {
109- this . application = application ;
110- } ) ;
111- }
112-
113- ngOnDestroy ( ) {
114- if ( this . applicationsSubscription ) {
115- this . applicationsSubscription . unsubscribe ( ) ;
116- }
117- if ( this . deleteDialogSubscription ) {
118- this . deleteDialogSubscription . unsubscribe ( ) ;
119- }
104+ ngOnDestroy ( ) {
105+ if ( this . applicationsSubscription ) {
106+ this . applicationsSubscription . unsubscribe ( ) ;
120107 }
108+ }
121109}
0 commit comments