thingsboard/ui-ngx/src/app/modules/home/pages/device/device-credentials-dialog.component.ts

112 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-08-14 19:55:24 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2019-08-14 19:55:24 +03:00
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
2020-04-03 21:14:39 +03:00
import { Component, Inject, OnInit, SkipSelf } from '@angular/core';
2020-02-10 13:15:29 +02:00
import { ErrorStateMatcher } from '@angular/material/core';
2023-02-17 19:24:01 +02:00
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
2019-08-14 19:55:24 +03:00
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
2023-02-02 15:55:06 +02:00
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup, FormGroupDirective, NgForm } from '@angular/forms';
2020-04-03 21:14:39 +03:00
import { DeviceService } from '@core/http/device.service';
import { DeviceCredentials, DeviceProfileInfo, DeviceTransportType } from '@shared/models/device.models';
import { DialogComponent } from '@shared/components/dialog.component';
import { Router } from '@angular/router';
import { DeviceProfileService } from '@core/http/device-profile.service';
import { forkJoin } from 'rxjs';
2019-08-14 19:55:24 +03:00
export interface DeviceCredentialsDialogData {
isReadOnly: boolean;
deviceId: string;
deviceProfileId: string;
2019-08-14 19:55:24 +03:00
}
@Component({
selector: 'tb-device-credentials-dialog',
templateUrl: './device-credentials-dialog.component.html',
providers: [{provide: ErrorStateMatcher, useExisting: DeviceCredentialsDialogComponent}],
styleUrls: []
})
export class DeviceCredentialsDialogComponent extends
DialogComponent<DeviceCredentialsDialogComponent, DeviceCredentials> implements OnInit, ErrorStateMatcher {
2019-08-14 19:55:24 +03:00
2023-02-02 15:55:06 +02:00
deviceCredentialsFormGroup: UntypedFormGroup;
2021-07-07 15:25:48 +03:00
deviceTransportType: DeviceTransportType;
2019-08-14 19:55:24 +03:00
isReadOnly: boolean;
2021-07-07 15:25:48 +03:00
loadingCredentials = true;
2019-08-14 19:55:24 +03:00
2021-07-07 15:25:48 +03:00
private deviceCredentials: DeviceCredentials;
private submitted = false;
2020-09-10 15:14:37 +03:00
2019-08-14 19:55:24 +03:00
constructor(protected store: Store<AppState>,
protected router: Router,
2019-08-14 19:55:24 +03:00
@Inject(MAT_DIALOG_DATA) public data: DeviceCredentialsDialogData,
private deviceService: DeviceService,
private deviceProfileService: DeviceProfileService,
2019-08-14 19:55:24 +03:00
@SkipSelf() private errorStateMatcher: ErrorStateMatcher,
public dialogRef: MatDialogRef<DeviceCredentialsDialogComponent, DeviceCredentials>,
2023-02-02 15:55:06 +02:00
public fb: UntypedFormBuilder) {
super(store, router, dialogRef);
2019-08-14 19:55:24 +03:00
this.isReadOnly = data.isReadOnly;
}
ngOnInit(): void {
this.deviceCredentialsFormGroup = this.fb.group({
credential: [null]
2019-08-14 19:55:24 +03:00
});
if (this.isReadOnly) {
this.deviceCredentialsFormGroup.disable({emitEvent: false});
}
this.loadDeviceCredentials();
}
2023-02-02 15:55:06 +02:00
isErrorState(control: UntypedFormControl | null, form: FormGroupDirective | NgForm | null): boolean {
2019-08-14 19:55:24 +03:00
const originalErrorState = this.errorStateMatcher.isErrorState(control, form);
const customErrorState = !!(control && control.invalid && this.submitted);
return originalErrorState || customErrorState;
}
loadDeviceCredentials() {
2023-02-06 13:09:43 +02:00
const task = [
this.deviceService.getDeviceCredentials(this.data.deviceId),
this.deviceProfileService.getDeviceProfileInfo(this.data.deviceProfileId)
];
forkJoin(task).subscribe(([deviceCredentials, deviceProfile]: [DeviceCredentials, DeviceProfileInfo]) => {
this.deviceTransportType = deviceProfile.transportType;
this.deviceCredentials = deviceCredentials;
this.deviceCredentialsFormGroup.patchValue({
credential: deviceCredentials
}, {emitEvent: false});
2021-07-07 15:25:48 +03:00
this.loadingCredentials = false;
});
2019-08-14 19:55:24 +03:00
}
cancel(): void {
this.dialogRef.close(null);
}
save(): void {
this.submitted = true;
const deviceCredentialsValue = this.deviceCredentialsFormGroup.value.credential;
2020-09-10 15:14:37 +03:00
this.deviceCredentials = {...this.deviceCredentials, ...deviceCredentialsValue};
2019-08-14 19:55:24 +03:00
this.deviceService.saveDeviceCredentials(this.deviceCredentials).subscribe(
(deviceCredentials) => {
this.dialogRef.close(deviceCredentials);
}
);
}
}