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