committed by
GitHub
28 changed files with 607 additions and 10 deletions
@ -0,0 +1,80 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.controller; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.security.access.prepost.PreAuthorize; |
||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.config.annotations.ApiOperation; |
||||
|
import org.thingsboard.server.dao.trendz.TrendzSettingsService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.security.model.SecurityUser; |
||||
|
import org.thingsboard.server.service.security.permission.Operation; |
||||
|
import org.thingsboard.server.service.security.permission.Resource; |
||||
|
|
||||
|
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_END; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_START; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.NEW_LINE; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH; |
||||
|
|
||||
|
@RestController |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
@RequestMapping("/api") |
||||
|
public class TrendzController extends BaseController { |
||||
|
|
||||
|
private final TrendzSettingsService trendzSettingsService; |
||||
|
|
||||
|
@ApiOperation(value = "Save Trendz settings (saveTrendzSettings)", |
||||
|
notes = "Saves Trendz settings for this tenant.\n" + NEW_LINE + |
||||
|
"Here is an example of the Trendz settings:\n" + |
||||
|
MARKDOWN_CODE_BLOCK_START + |
||||
|
"{\n" + |
||||
|
" \"enabled\": true,\n" + |
||||
|
" \"baseUrl\": \"https://some.domain.com:18888/also_necessary_prefix\"\n" + |
||||
|
"}" + |
||||
|
MARKDOWN_CODE_BLOCK_END + |
||||
|
TENANT_AUTHORITY_PARAGRAPH) |
||||
|
@PostMapping("/trendz/settings") |
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
public TrendzSettings saveTrendzSettings(@RequestBody TrendzSettings trendzSettings, |
||||
|
@AuthenticationPrincipal SecurityUser user) throws ThingsboardException { |
||||
|
accessControlService.checkPermission(user, Resource.ADMIN_SETTINGS, Operation.WRITE); |
||||
|
TenantId tenantId = user.getTenantId(); |
||||
|
trendzSettingsService.saveTrendzSettings(tenantId, trendzSettings); |
||||
|
return trendzSettings; |
||||
|
} |
||||
|
|
||||
|
@ApiOperation(value = "Get Trendz Settings (getTrendzSettings)", |
||||
|
notes = "Retrieves Trendz settings for this tenant." + |
||||
|
TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) |
||||
|
@GetMapping("/trendz/settings") |
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
public TrendzSettings getTrendzSettings(@AuthenticationPrincipal SecurityUser user) { |
||||
|
TenantId tenantId = user.getTenantId(); |
||||
|
return trendzSettingsService.findTrendzSettings(tenantId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.controller; |
||||
|
|
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||
|
|
||||
|
@DaoSqlTest |
||||
|
public class TrendzControllerTest extends AbstractControllerTest { |
||||
|
|
||||
|
private final String trendzUrl = "https://some.domain.com:18888/also_necessary_prefix"; |
||||
|
|
||||
|
@Before |
||||
|
public void setUp() throws Exception { |
||||
|
loginTenantAdmin(); |
||||
|
|
||||
|
TrendzSettings trendzSettings = new TrendzSettings(); |
||||
|
trendzSettings.setEnabled(true); |
||||
|
trendzSettings.setBaseUrl(trendzUrl); |
||||
|
|
||||
|
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testTrendzSettingsWhenTenant() throws Exception { |
||||
|
loginTenantAdmin(); |
||||
|
|
||||
|
TrendzSettings trendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
|
||||
|
assertThat(trendzSettings).isNotNull(); |
||||
|
assertThat(trendzSettings.isEnabled()).isTrue(); |
||||
|
assertThat(trendzSettings.getBaseUrl()).isEqualTo(trendzUrl); |
||||
|
|
||||
|
String updatedUrl = "https://some.domain.com:18888/tenant_trendz"; |
||||
|
trendzSettings.setBaseUrl(updatedUrl); |
||||
|
|
||||
|
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk()); |
||||
|
|
||||
|
TrendzSettings updatedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
assertThat(updatedTrendzSettings).isEqualTo(trendzSettings); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testTrendzSettingsWhenCustomer() throws Exception { |
||||
|
loginCustomerUser(); |
||||
|
|
||||
|
TrendzSettings newTrendzSettings = new TrendzSettings(); |
||||
|
newTrendzSettings.setEnabled(true); |
||||
|
newTrendzSettings.setBaseUrl("https://some.domain.com:18888/customer_trendz"); |
||||
|
|
||||
|
doPost("/api/trendz/settings", newTrendzSettings).andExpect(status().isForbidden()); |
||||
|
|
||||
|
TrendzSettings fetchedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
assertThat(fetchedTrendzSettings).isNotNull(); |
||||
|
assertThat(fetchedTrendzSettings.isEnabled()).isTrue(); |
||||
|
assertThat(fetchedTrendzSettings.getBaseUrl()).isEqualTo(trendzUrl); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.trendz; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
|
||||
|
public interface TrendzSettingsService { |
||||
|
|
||||
|
void saveTrendzSettings(TenantId tenantId, TrendzSettings settings); |
||||
|
|
||||
|
TrendzSettings findTrendzSettings(TenantId tenantId); |
||||
|
|
||||
|
void deleteTrendzSettings(TenantId tenantId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.trendz; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class TrendzSettings { |
||||
|
|
||||
|
private boolean enabled; |
||||
|
private String baseUrl; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.trendz; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.cache.annotation.CacheEvict; |
||||
|
import org.springframework.cache.annotation.Cacheable; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.AdminSettings; |
||||
|
import org.thingsboard.server.common.data.CacheConstants; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.dao.settings.AdminSettingsService; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
public class DefaultTrendzSettingsService implements TrendzSettingsService { |
||||
|
|
||||
|
private final AdminSettingsService adminSettingsService; |
||||
|
|
||||
|
private static final String SETTINGS_KEY = "trendz"; |
||||
|
|
||||
|
@CacheEvict(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
||||
|
@Override |
||||
|
public void saveTrendzSettings(TenantId tenantId, TrendzSettings settings) { |
||||
|
AdminSettings adminSettings = Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
||||
|
.orElseGet(() -> { |
||||
|
AdminSettings newAdminSettings = new AdminSettings(); |
||||
|
newAdminSettings.setTenantId(tenantId); |
||||
|
newAdminSettings.setKey(SETTINGS_KEY); |
||||
|
return newAdminSettings; |
||||
|
}); |
||||
|
adminSettings.setJsonValue(JacksonUtil.valueToTree(settings)); |
||||
|
adminSettingsService.saveAdminSettings(tenantId, adminSettings); |
||||
|
} |
||||
|
|
||||
|
@Cacheable(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
||||
|
@Override |
||||
|
public TrendzSettings findTrendzSettings(TenantId tenantId) { |
||||
|
return Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
||||
|
.map(adminSettings -> JacksonUtil.treeToValue(adminSettings.getJsonValue(), TrendzSettings.class)) |
||||
|
.orElseGet(TrendzSettings::new); |
||||
|
} |
||||
|
|
||||
|
@CacheEvict(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
||||
|
@Override |
||||
|
public void deleteTrendzSettings(TenantId tenantId) { |
||||
|
adminSettingsService.deleteAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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, RequestConfig } from '@core/http/http-utils'; |
||||
|
|
||||
|
@Injectable({ |
||||
|
providedIn: 'root' |
||||
|
}) |
||||
|
export class TrendzSettingsService { |
||||
|
|
||||
|
constructor( |
||||
|
private http: HttpClient |
||||
|
) {} |
||||
|
|
||||
|
public getTrendzSettings(config?: RequestConfig): Observable<TrendzSettings> { |
||||
|
return this.http.get<TrendzSettings>(`/api/trendz/settings`, defaultHttpOptionsFromConfig(config)) |
||||
|
} |
||||
|
|
||||
|
public saveTrendzSettings(trendzSettings: TrendzSettings, config?: RequestConfig): Observable<TrendzSettings> { |
||||
|
return this.http.post<TrendzSettings>(`/api/trendz/settings`, trendzSettings, defaultHttpOptionsFromConfig(config)) |
||||
|
} |
||||
|
} |
||||
@ -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,94 @@ |
|||||
|
///
|
||||
|
/// 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, DestroyRef } from '@angular/core'; |
||||
|
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'; |
||||
|
import { isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
||||
|
|
||||
|
@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(private fb: FormBuilder, |
||||
|
private trendzSettingsService: TrendzSettingsService, |
||||
|
private destroyRef: DestroyRef) { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
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 |
||||
|
.pipe(takeUntilDestroyed(this.destroyRef)) |
||||
|
.subscribe((enabled: boolean) => this.toggleUrlRequired(enabled)); |
||||
|
} |
||||
|
|
||||
|
toggleUrlRequired(enabled: boolean) { |
||||
|
const trendzUrlControl = this.trendzSettingsForm.get('trendzUrl')!; |
||||
|
|
||||
|
if (enabled) { |
||||
|
trendzUrlControl.addValidators(Validators.required); |
||||
|
} else { |
||||
|
trendzUrlControl.removeValidators(Validators.required); |
||||
|
} |
||||
|
|
||||
|
trendzUrlControl.updateValueAndValidity(); |
||||
|
} |
||||
|
|
||||
|
setTrendzSettings(trendzSettings: TrendzSettings) { |
||||
|
this.trendzSettingsForm.reset({ |
||||
|
trendzUrl: trendzSettings?.baseUrl, |
||||
|
isTrendzEnabled: 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