13 changed files with 302 additions and 7 deletions
@ -0,0 +1,39 @@ |
|||
///
|
|||
/// 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.
|
|||
///
|
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { Observable } from 'rxjs'; |
|||
import { HttpClient } from '@angular/common/http'; |
|||
import { TrendzSettings } from '@shared/models/trendz-settings.models'; |
|||
import { defaultHttpOptionsFromConfig } from '@core/http/http-utils'; |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root' |
|||
}) |
|||
export class TrendzSettingsService { |
|||
|
|||
constructor( |
|||
private http: HttpClient |
|||
) {} |
|||
|
|||
public getTrendzSettings(): Observable<TrendzSettings> { |
|||
return this.http.get<TrendzSettings>(`/api/trendz/settings`, defaultHttpOptionsFromConfig({ignoreLoading: true, ignoreErrors: true})) |
|||
} |
|||
|
|||
public saveTrendzSettings(trendzSettings: TrendzSettings): Observable<TrendzSettings> { |
|||
return this.http.post<TrendzSettings>(`/api/trendz/settings`, trendzSettings, defaultHttpOptionsFromConfig({ignoreLoading: true, ignoreErrors: true})) |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
<!-- |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<div> |
|||
<mat-card appearance="outlined" class="settings-card"> |
|||
<mat-card-header> |
|||
<mat-card-title> |
|||
<span class="mat-headline-5" translate>admin.trendz-settings</span> |
|||
</mat-card-title> |
|||
<span class="flex-1"></span> |
|||
<div tb-help="trendzSettings"></div> |
|||
</mat-card-header> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<mat-card-content> |
|||
<form [formGroup]="trendzSettingsForm" (ngSubmit)="save()"> |
|||
<fieldset [disabled]="isLoading$ | async"> |
|||
<section class="tb-trendz-section flex flex-col gt-sm:flex-row"> |
|||
<mat-form-field class="tb-trendz-url mat-block flex-1" subscriptSizing="dynamic"> |
|||
<mat-label translate>admin.trendz-url</mat-label> |
|||
<input matInput formControlName="trendzUrl"> |
|||
</mat-form-field> |
|||
<mat-checkbox class="flex flex-1" formControlName="isTrendzEnabled"> |
|||
{{ 'admin.trendz-enable' | translate }} |
|||
</mat-checkbox> |
|||
</section> |
|||
<div class="flex w-full flex-row flex-wrap items-center justify-end"> |
|||
<button mat-button mat-raised-button color="primary" [disabled]="(isLoading$ | async) || trendzSettingsForm.invalid || !trendzSettingsForm.dirty" type="submit"> |
|||
{{'action.save' | translate}} |
|||
</button> |
|||
</div> |
|||
</fieldset> |
|||
</form> |
|||
</mat-card-content> |
|||
</mat-card> |
|||
</div> |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
@import "../../../../../scss/constants"; |
|||
|
|||
:host { |
|||
.mat-mdc-card-header { |
|||
min-height: 64px; |
|||
} |
|||
|
|||
.tb-trendz-section { |
|||
margin: 16px 0; |
|||
} |
|||
|
|||
.tb-trendz-url { |
|||
@media #{$mat-gt-sm} { |
|||
padding-right: 12px; |
|||
} |
|||
|
|||
@media #{$mat-lt-md} { |
|||
padding-bottom: 12px; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
///
|
|||
/// 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.
|
|||
///
|
|||
|
|||
import { Component, OnInit } from '@angular/core'; |
|||
import { PageComponent } from '@shared/components/page.component'; |
|||
import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; |
|||
import { select, Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { TrendzSettingsService } from '@core/http/trendz-settings.service'; |
|||
import { TrendzSettings } from '@shared/models/trendz-settings.models'; |
|||
import { isDefinedAndNotNull } from '@core/utils'; |
|||
|
|||
@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; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
private fb: FormBuilder, |
|||
private trendzSettingsService: TrendzSettingsService) { |
|||
super(store); |
|||
} |
|||
|
|||
ngOnInit() { |
|||
this.trendzSettingsForm = this.fb.group({ |
|||
trendzUrl: [null, [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]], |
|||
isTrendzEnabled: [false] |
|||
}); |
|||
|
|||
this.trendzSettingsService.getTrendzSettings().subscribe((trendzSettings) => { |
|||
this.setTrendzSettings(trendzSettings); |
|||
}); |
|||
|
|||
this.trendzSettingsForm.get('isTrendzEnabled').valueChanges |
|||
.subscribe((enabled: boolean) => this.toggleUrlRequired(enabled)); |
|||
} |
|||
|
|||
toggleUrlRequired(enabled: boolean) { |
|||
const trendzUrlControl = this.trendzSettingsForm.get('trendzUrl')!; |
|||
const validators = [Validators.pattern(/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i)]; |
|||
|
|||
if (enabled) { |
|||
validators.push(Validators.required); |
|||
} |
|||
|
|||
trendzUrlControl.setValidators(validators); |
|||
trendzUrlControl.updateValueAndValidity(); |
|||
} |
|||
|
|||
setTrendzSettings(trendzSettings: TrendzSettings) { |
|||
this.trendzSettingsForm.reset({ |
|||
trendzUrl: trendzSettings?.baseUrl, |
|||
isTrendzEnabled: isDefinedAndNotNull(trendzSettings?.enabled) ? |
|||
trendzSettings?.enabled : false |
|||
}); |
|||
|
|||
this.toggleUrlRequired(this.trendzSettingsForm.get('isTrendzEnabled').value); |
|||
} |
|||
|
|||
confirmForm(): FormGroup { |
|||
return this.trendzSettingsForm; |
|||
} |
|||
|
|||
save(): void { |
|||
const trendzUrl = this.trendzSettingsForm.get('trendzUrl').value; |
|||
const isTrendzEnabled = this.trendzSettingsForm.get('isTrendzEnabled').value; |
|||
|
|||
const trendzSettings: TrendzSettings = { |
|||
baseUrl: trendzUrl, |
|||
enabled: isTrendzEnabled |
|||
}; |
|||
|
|||
this.trendzSettingsService.saveTrendzSettings(trendzSettings).subscribe(() => { |
|||
this.setTrendzSettings(trendzSettings); |
|||
}) |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
///
|
|||
/// 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.
|
|||
///
|
|||
|
|||
export interface TrendzSettings { |
|||
baseUrl: string, |
|||
enabled: boolean |
|||
} |
|||
|
|||
export const initialTrendzSettings: TrendzSettings = { |
|||
baseUrl: null, |
|||
enabled: false |
|||
} |
|||
Loading…
Reference in new issue