-
Notifications
You must be signed in to change notification settings - Fork 5
/
device-profiles-edit.component.ts
148 lines (133 loc) · 4.54 KB
/
device-profiles-edit.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Location } from '@angular/common';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { ErrorMessageService } from '@shared/error-message.service';
import { BackButton } from '@shared/models/back-button.model';
import { MeService } from '@shared/services/me.service';
import { Subscription } from 'rxjs';
import { DeviceProfile } from '../device-profile.model';
import { DeviceProfileService } from '../device-profile.service';
import { OrganizationAccessScope } from '@shared/enums/access-scopes';
import { AdrAlgorithm, AdrAlgorithmResponse } from '@app/network-server/adr-algorithm.model';
import { NetworkServerService } from '@app/network-server/network-server.service';
@Component({
selector: 'app-device-profiles-edit',
templateUrl: './device-profiles-edit.component.html',
styleUrls: ['./device-profiles-edit.component.scss']
})
export class DeviceProfilesEditComponent implements OnInit, OnDestroy {
id: string;
deviceProfile = new DeviceProfile();
subscription: Subscription;
public errorMessage: string;
public errorMessages: string[];
public errorFields: string[];
public formFailedSubmit = false;
public title = '';
public backButton: BackButton = { label: '', routerLink: '/profiles' };
public adrAlgorithms: AdrAlgorithm[] = [];
constructor(
private route: ActivatedRoute,
private translate: TranslateService,
private deviceProfileService: DeviceProfileService,
private location: Location,
private meService: MeService,
private errorMessageService: ErrorMessageService,
private networkServerService: NetworkServerService
) { }
ngOnInit(): void {
this.translate.get(['PROFILES.NAME', 'FORM.EDIT-DEVICE-PROFILE'])
.subscribe(translations => {
this.title = translations['FORM.EDIT-DEVICE-PROFILE'];
this.backButton.label = translations['PROFILES.NAME'];
});
this.networkServerService.getAllAdrAlgorithms()
.subscribe(
response => {
this.adrAlgorithms = response.adrAlgorithms;
});
this.id = this.route.snapshot.paramMap.get('deviceId');
if (this.id) {
this.getDeviceProfile(this.id);
} else {
this.deviceProfile.canEdit = true;
}
}
getDeviceProfile(id: string) {
this.subscription = this.deviceProfileService.getOne(id)
.subscribe(
(response) => {
this.deviceProfile = response.deviceProfile;
this.canEdit();
this.deviceProfile.factoryPresetFreqsInput = this.deviceProfile.factoryPresetFreqs.map(String).join();
},
(error: HttpErrorResponse) => {
console.log(error);
}
);
}
canEdit() {
if (this.deviceProfile.organizationID) {
this.deviceProfile.canEdit = this.meService.hasAccessToTargetOrganization(OrganizationAccessScope.ApplicationWrite, this.deviceProfile.internalOrganizationId);
} else {
this.deviceProfile.canEdit = true;
}
}
private create(): void {
this.deviceProfileService.post(this.deviceProfile)
.subscribe(
(response) => {
console.log(response);
this.routeBack();
},
(error: HttpErrorResponse) => {
this.showError(error);
}
);
}
private update(): void {
this.deviceProfileService.put(this.deviceProfile)
.subscribe(
(response) => {
this.routeBack();
},
(error) => {
this.showError(error);
});
}
routeBack(): void {
this.location.back();
}
public compare(o1: any, o2: any): boolean {
return o1 === o2;
}
private showError(error: HttpErrorResponse) {
if (error.status == 403) {
this.errorMessages = ["Forbudt"]
} else {
const errors = this.errorMessageService.handleErrorMessageWithFields(error);
this.errorFields = errors?.errorFields;
this.errorMessages = errors?.errorMessages;
}
}
onSubmit(): void {
if (this.deviceProfile.factoryPresetFreqsInput) {
const splitToNumberString = this.deviceProfile.factoryPresetFreqsInput.split(',');
const convToNumberArray: number[] = splitToNumberString.map(Number);
this.deviceProfile.factoryPresetFreqs = convToNumberArray;
}
if (this.deviceProfile.id) {
this.update();
} else {
this.create();
}
}
ngOnDestroy() {
// prevent memory leak by unsubscribing
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}