thingsboard/ui-ngx/src/app/modules/home/pages/admin/trendz-settings.component.ts

99 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-05-02 12:42:14 +03:00
///
/// Copyright © 2016-2025 The Thingsboard Authors
///
/// 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.
///
2025-05-05 11:38:16 +03:00
import { Component, OnInit, DestroyRef } from '@angular/core';
2025-05-02 12:42:14 +03:00
import { PageComponent } from '@shared/components/page.component';
import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TrendzSettingsService } from '@core/http/trendz-settings.service';
import { TrendzSettings } from '@shared/models/trendz-settings.models';
2025-05-05 11:38:16 +03:00
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2025-05-02 12:42:14 +03:00
@Component({
selector: 'tb-trendz-settings',
templateUrl: './trendz-settings.component.html',
styleUrls: ['./trendz-settings.component.scss', './settings-card.scss']
})
export class TrendzSettingsComponent extends PageComponent implements OnInit, HasConfirmForm {
trendzSettingsForm: FormGroup;
2025-05-05 11:38:16 +03:00
constructor(private fb: FormBuilder,
private trendzSettingsService: TrendzSettingsService,
private destroyRef: DestroyRef) {
super();
2025-05-02 12:42:14 +03:00
}
ngOnInit() {
this.trendzSettingsForm = this.fb.group({
2025-05-22 12:49:11 +01:00
isTrendzEnabled: [false],
2025-05-02 12:42:14 +03:00
trendzUrl: [null, [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]],
2025-05-22 12:49:11 +01:00
apiKey: [null]
2025-05-02 12:42:14 +03:00
});
this.trendzSettingsService.getTrendzSettings().subscribe((trendzSettings) => {
this.setTrendzSettings(trendzSettings);
});
this.trendzSettingsForm.get('isTrendzEnabled').valueChanges
2025-05-05 11:38:16 +03:00
.pipe(takeUntilDestroyed(this.destroyRef))
2025-05-02 12:42:14 +03:00
.subscribe((enabled: boolean) => this.toggleUrlRequired(enabled));
}
toggleUrlRequired(enabled: boolean) {
const trendzUrlControl = this.trendzSettingsForm.get('trendzUrl')!;
if (enabled) {
2025-05-05 11:38:16 +03:00
trendzUrlControl.addValidators(Validators.required);
} else {
trendzUrlControl.removeValidators(Validators.required);
2025-05-02 12:42:14 +03:00
}
trendzUrlControl.updateValueAndValidity();
}
setTrendzSettings(trendzSettings: TrendzSettings) {
this.trendzSettingsForm.reset({
2025-05-22 12:49:11 +01:00
isTrendzEnabled: trendzSettings?.enabled ?? false,
2025-05-02 12:42:14 +03:00
trendzUrl: trendzSettings?.baseUrl,
2025-05-22 12:49:11 +01:00
apiKey: trendzSettings?.apiKey
2025-05-02 12:42:14 +03:00
});
this.toggleUrlRequired(this.trendzSettingsForm.get('isTrendzEnabled').value);
}
confirmForm(): FormGroup {
return this.trendzSettingsForm;
}
save(): void {
const isTrendzEnabled = this.trendzSettingsForm.get('isTrendzEnabled').value;
2025-05-22 12:49:11 +01:00
const trendzUrl = this.trendzSettingsForm.get('trendzUrl').value;
const apiKey = this.trendzSettingsForm.get('apiKey').value;
2025-05-02 12:42:14 +03:00
const trendzSettings: TrendzSettings = {
2025-05-22 12:49:11 +01:00
enabled: isTrendzEnabled,
2025-05-02 12:42:14 +03:00
baseUrl: trendzUrl,
2025-05-22 12:49:11 +01:00
apiKey: apiKey
2025-05-02 12:42:14 +03:00
};
2025-05-22 12:49:11 +01:00
this.trendzSettingsService.saveTrendzSettings(trendzSettings)
.subscribe(() => {
this.setTrendzSettings(trendzSettings);
})
2025-05-02 12:42:14 +03:00
}
}