Skip to content

Commit 0ca1526

Browse files
PR Changes + formatting
1 parent 4b11b86 commit 0ca1526

File tree

3 files changed

+144
-142
lines changed

3 files changed

+144
-142
lines changed
Lines changed: 83 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
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';
28
import { Title } from '@angular/platform-browser';
39
import { ActivatedRoute, Router } from '@angular/router';
410
import { Application } from '@applications/application.model';
@@ -12,110 +18,92 @@ import { MeService } from '@shared/services/me.service';
1218
import { 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
})
1925
export 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
}

src/app/applications/applications-list/applications-table/applications-table.component.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,10 @@ export class ApplicationsTableComponent implements AfterViewInit, OnInit {
9494
}
9595

9696
deleteApplication(id: number) {
97-
let message: string;
98-
let showAccept: boolean = true;
99-
const hasSigfoxDevices: boolean = this.applicationHasSigFoxDevices(id);
100-
101-
if (hasSigfoxDevices) {
102-
message = this.translate.instant(
103-
'APPLICATION.DELETE-HAS-SIGFOX-DEVICES-PROMPT'
104-
);
105-
showAccept = false;
106-
} else if (this.applicationHasDevices(id)) {
107-
message = this.translate.instant('APPLICATION.DELETE-HAS-DEVICES-PROMPT');
108-
}
97+
const applicationToDelete = this.data?.find((app) => app.id === id);
10998

11099
this.deleteDialogService
111-
.showSimpleDialog(message, showAccept)
100+
.showApplicationDialog(applicationToDelete)
112101
.subscribe((response) => {
113102
if (response) {
114103
this.applicationService
@@ -127,20 +116,6 @@ export class ApplicationsTableComponent implements AfterViewInit, OnInit {
127116
}
128117
});
129118
}
130-
131-
applicationHasDevices(id: number): boolean {
132-
const applicationToDelete = this.data?.find((app) => app.id === id);
133-
return applicationToDelete && applicationToDelete.iotDevices.length > 0;
134-
}
135-
136-
applicationHasSigFoxDevices(id: number): boolean {
137-
const applicationToDelete = this.data?.find((app) => app.id === id);
138-
const sigfoxDevice = applicationToDelete.iotDevices.find((device) => {
139-
return device.type === DeviceType.SIGFOX;
140-
});
141-
return sigfoxDevice !== undefined;
142-
}
143-
144119
navigateToEditPage(applicationId: string) {
145120
this.router.navigate(['applications', 'edit-application', applicationId]);
146121
}
Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,72 @@
11
import { Injectable } from '@angular/core';
22
import { MatDialog } from '@angular/material/dialog';
33
import { DeleteDialogComponent } from './delete-dialog.component';
4-
import { Observable } from 'rxjs';
4+
import { Observable, Subscription } from 'rxjs';
5+
import { Application } from '@applications/application.model';
6+
import { DeviceType } from '@shared/enums/device-type';
7+
import { TranslateService } from '@ngx-translate/core';
58
@Injectable({
69
providedIn: 'root',
710
})
811
export class DeleteDialogService {
9-
12+
private deleteDialogSubscription: Subscription;
1013
constructor(
11-
private dialog: MatDialog
12-
) { }
14+
private dialog: MatDialog,
15+
public translate: TranslateService,
16+
) {}
17+
18+
showSimpleDialog(
19+
message?: string,
20+
showAccept = true,
21+
showCancel = true,
22+
showOk = false,
23+
infoTitle = ''
24+
): Observable<any> {
25+
return new Observable((observer) => {
26+
const dialog = this.dialog.open(DeleteDialogComponent, {
27+
data: {
28+
infoTitle,
29+
showOk,
30+
showAccept,
31+
showCancel,
32+
message: message ? message : 'Er du sikker på at du vil slette?',
33+
},
34+
});
35+
36+
dialog.afterClosed().subscribe((result) => {
37+
observer.next(result);
38+
});
39+
});
40+
}
1341

14-
showSimpleDialog(message?: string, showAccept = true, showCancel = true, showOk = false, infoTitle = ''): Observable<any> {
15-
return new Observable(
16-
(observer) => {
17-
const dialog = this.dialog.open(DeleteDialogComponent, {
18-
data: {
19-
infoTitle,
20-
showOk,
21-
showAccept,
22-
showCancel,
23-
message: message ? message : 'Er du sikker på at du vil slette?'
24-
}
25-
});
42+
showApplicationDialog(application: Application): Observable<any> {
43+
let message: string;
44+
let showAccept: boolean = true;
45+
const hasSigfoxDevices: boolean = this.applicationHasSigFoxDevices(
46+
application
47+
);
2648

27-
dialog.afterClosed().subscribe((result) => {
28-
observer.next(result);
29-
});
49+
if (hasSigfoxDevices) {
50+
message = this.translate.instant(
51+
'APPLICATION.DELETE-HAS-SIGFOX-DEVICES-PROMPT'
52+
);
53+
showAccept = false;
54+
} else if (this.applicationHasDevices(application)) {
55+
message = this.translate.instant(
56+
'APPLICATION.DELETE-HAS-DEVICES-PROMPT'
57+
);
3058
}
31-
);
59+
return this.showSimpleDialog(message, showAccept);
60+
}
61+
62+
applicationHasDevices(application: Application): boolean {
63+
return application.iotDevices?.length > 0;
64+
}
65+
66+
applicationHasSigFoxDevices(application: Application): boolean {
67+
const sigfoxDevice = application.iotDevices.find((device) => {
68+
return device.type === DeviceType.SIGFOX;
69+
});
70+
return sigfoxDevice !== undefined;
3271
}
3372
}

0 commit comments

Comments
 (0)