From a4f3fa314295515d65b780eb6d37ef387e30b3a7 Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Wed, 11 Nov 2026 16:21:53 +0100 Subject: [PATCH] Fixed form flickering on 'Use system settings' pages by loading settings in route resolvers (#16437) * Fixed form content flickering on settings pages with 'Use system settings' option * PR comments fixes --- ui-ngx/src/app/app.component.ts | 32 ++++++++- .../home/pages/admin/admin-routing.module.ts | 45 ++++++++++++- .../home/pages/admin/mail-server.component.ts | 67 +++++++++---------- .../pages/admin/sms-provider.component.ts | 30 +++------ ...-qr-code-widget-settings-routing.module.ts | 30 ++++++++- ...obile-qr-code-widget-settings.component.ts | 17 +++-- 6 files changed, 152 insertions(+), 69 deletions(-) diff --git a/ui-ngx/src/app/app.component.ts b/ui-ngx/src/app/app.component.ts index 3b93872bb1..b811c7fc2c 100644 --- a/ui-ngx/src/app/app.component.ts +++ b/ui-ngx/src/app/app.component.ts @@ -14,7 +14,7 @@ import { LocalStorageService } from '@core/local-storage/local-storage.service'; import { DomSanitizer } from '@angular/platform-browser'; import { MatIconRegistry } from '@angular/material/icon'; import { getCurrentAuthState, selectUserReady } from '@core/auth/auth.selectors'; -import { filter, skip, tap } from 'rxjs/operators'; +import { filter, skip, switchMap, take, takeWhile, tap } from 'rxjs/operators'; import { AuthService } from '@core/auth/auth.service'; import { DashboardReportService } from '@core/http/dashboard-report.service'; import { DevelopmentService } from '@core/http/development.service'; @@ -25,6 +25,9 @@ import { initCustomJQueryEvents } from '@shared/models/jquery-event.models'; import { TranslateDefaultLoader } from '@core/translate/translate-default-loader'; import { SystemSetupService } from '@core/http/system-setup.service'; import { SystemSetupState } from '@shared/models/system-setup.models'; +import { NavigationEnd, NavigationError, Router } from '@angular/router'; +import { ToastNotificationService } from '@core/services/toast-notification.service'; +import { NotificationMessage } from '@core/notification/notification.models'; @Component({ selector: 'tb-root', @@ -43,7 +46,9 @@ export class AppComponent { private authService: AuthService, private reportService: DashboardReportService, private developmentService: DevelopmentService, - private systemSetupService: SystemSetupService) { + private systemSetupService: SystemSetupService, + private router: Router, + private toastNotificationService: ToastNotificationService) { if (!env.production) { console.log(`ThingsBoard Version: ${env.tbVersion}`); @@ -85,6 +90,29 @@ export class AppComponent { }); this.routeAuthenticationChangesWhileSetupIsIncomplete(); + this.gotoDefaultPlaceOnInitialNavigationError(); + } + + private gotoDefaultPlaceOnInitialNavigationError() { + const pendingNotifications: NotificationMessage[] = []; + this.toastNotificationService.getNotification().pipe( + takeWhile(() => !this.router.navigated) + ).subscribe((notification) => pendingNotifications.push(notification)); + this.router.events.pipe( + filter((event) => event instanceof NavigationError && !this.router.navigated), + take(1), + switchMap(() => { + this.authService.gotoDefaultPlace(getCurrentAuthState(this.store).isAuthenticated); + return this.router.events.pipe( + filter((event) => event instanceof NavigationEnd), + take(1) + ); + }) + ).subscribe(() => { + setTimeout(() => { + pendingNotifications.forEach((notification) => this.toastNotificationService.dispatchNotification(notification)); + }); + }); } /** diff --git a/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts b/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts index 0f84b503be..d2605b500c 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts +++ b/ui-ngx/src/app/modules/home/pages/admin/admin-routing.module.ts @@ -20,7 +20,13 @@ import { GeneralSettingsComponent } from '@home/pages/admin/general-settings.com import { SecuritySettingsComponent } from '@modules/home/pages/admin/security-settings.component'; import { MailTemplatesComponent } from '@home/pages/admin/mail-templates.component'; import { forkJoin, mergeMap, Observable, of } from 'rxjs'; -import { MailTemplatesSettings } from '@shared/models/settings.models'; +import { + AdminSettings, + MailConfigTemplate, + MailServerSettings, + MailTemplatesSettings, + SmsProviderConfiguration +} from '@shared/models/settings.models'; import { WhiteLabelingComponent } from '@home/pages/admin/white-labeling.component'; import { SelfRegistrationComponent } from '@home/pages/admin/self-registration.component'; import { HomeSettingsComponent } from '@home/pages/admin/home-settings.component'; @@ -59,6 +65,8 @@ import { AuthService } from '@core/auth/auth.service'; import { AppState } from '@core/core.state'; import { Store } from '@ngrx/store'; import { ActionUpdateLicenseParams } from '@core/auth/auth.actions'; +import { NotificationService } from '@core/http/notification.service'; +import { NotificationSettings } from '@shared/models/notification.models'; export const subscriptionInfoResolver: ResolveFn = ( route: ActivatedRouteSnapshot, @@ -112,6 +120,35 @@ export const mailTemplateSettingsResolver: ResolveFn = ( wl = inject(WhiteLabelingService) ): Observable => wl.getMailTemplates(true); +export const mailServerSettingsResolver: ResolveFn<{ + loginProcessingUrl: string; + mailConfigTemplate: Array; + adminSettings: AdminSettings; +}> = ( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot, + adminService = inject(AdminService) +) => forkJoin({ + loginProcessingUrl: adminService.getLoginProcessingUrl(), + mailConfigTemplate: adminService.getMailConfigTemplate(), + adminSettings: adminService.getAdminSettings('mail') +}); + +export const notificationsSettingsResolver: ResolveFn<{ + notificationSettings: NotificationSettings; + smsProviderSettings: AdminSettings; +}> = ( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot, + adminService = inject(AdminService), + notificationService = inject(NotificationService) +) => forkJoin({ + notificationSettings: notificationService.getNotificationSettings(), + smsProviderSettings: adminService.getAdminSettings('sms', false, {ignoreErrors: true}).pipe( + catchError(() => of({key: 'sms', jsonValue: null} as AdminSettings)) + ) +}); + export const scadaSymbolResolver: ResolveFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot, @@ -332,6 +369,9 @@ const routes: Routes = [ breadcrumb: { menuId: MenuId.mail_server } + }, + resolve: { + mailServerSettings: mailServerSettingsResolver } }, { @@ -344,6 +384,9 @@ const routes: Routes = [ breadcrumb: { menuId: MenuId.notification_settings } + }, + resolve: { + notificationsSettings: notificationsSettingsResolver } }, { diff --git a/ui-ngx/src/app/modules/home/pages/admin/mail-server.component.ts b/ui-ngx/src/app/modules/home/pages/admin/mail-server.component.ts index 9bb4d06a10..0f1f59fb62 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/mail-server.component.ts +++ b/ui-ngx/src/app/modules/home/pages/admin/mail-server.component.ts @@ -25,10 +25,11 @@ import { AuthUser } from '@shared/models/user.model'; import { Authority } from '@shared/models/authority.enum'; import { UserPermissionsService } from '@core/http/user-permissions.service'; import { Operation, Resource } from '@shared/models/security.models'; -import { forkJoin, Subject } from 'rxjs'; +import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { DomainSchema, domainSchemaTranslations, } from '@shared/models/oauth2.models'; import { WINDOW } from '@core/services/window.service'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'tb-mail-server', @@ -132,6 +133,7 @@ export class MailServerComponent extends PageComponent implements OnInit, OnDest private adminService: AdminService, private translate: TranslateService, private userPermissionsService: UserPermissionsService, + private route: ActivatedRoute, public fb: FormBuilder, @Inject(WINDOW) private window: Window) { super(store); @@ -141,40 +143,35 @@ export class MailServerComponent extends PageComponent implements OnInit, OnDest this.buildMailServerSettingsForm(); this.domainFormConfiguration(); - forkJoin({ - loginProcessingUrl: this.adminService.getLoginProcessingUrl(), - mailConfigTemplate: this.adminService.getMailConfigTemplate(), - adminSettings: this.adminService.getAdminSettings('mail') - }).subscribe((data) => { - this.loginProcessingUrl = data.loginProcessingUrl; - this.initTemplates(data.mailConfigTemplate); - this.adminSettings = data.adminSettings; - if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) { - this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true'; - } - if (!this.adminSettings.jsonValue.providerId) { - this.adminSettings.jsonValue.providerId = 'CUSTOM'; - } - this.mailSettings.reset(this.adminSettings.jsonValue, {emitEvent: false}); - if (this.isTenantAdmin()) { - this.mailSettings.get('useSystemMailSettings').setValue( - isDefined(this.adminSettings.jsonValue.useSystemMailSettings) ? - this.adminSettings.jsonValue.useSystemMailSettings : true, {emitEvent: false} - ); - } - this.updateValidators(); - this.enableTls(this.adminSettings.jsonValue.enableTls); - this.helpLink = this.templates.get(this.adminSettings.jsonValue.providerId)?.helpLink || null; - this.enableProxyChanged(); - if (this.adminSettings.jsonValue.enableOauth2) { - this.enableOauth2(!!this.adminSettings.jsonValue.enableOauth2); - this.enableProviderTenantIdChanged(this.adminSettings.jsonValue.providerId); - this.parseUrl(this.adminSettings.jsonValue.redirectUri); - this.mailSettings.get('redirectUri').patchValue(this.adminSettings.jsonValue.redirectUri, {emitEvent: false}); - } else { - this.mailSettings.get('enableOauth2').patchValue(false, {emitEvent: false}); - } - }) + const data = this.route.snapshot.data.mailServerSettings; + this.loginProcessingUrl = data.loginProcessingUrl; + this.initTemplates(data.mailConfigTemplate); + this.adminSettings = data.adminSettings; + if (this.adminSettings.jsonValue && isString(this.adminSettings.jsonValue.enableTls)) { + this.adminSettings.jsonValue.enableTls = (this.adminSettings.jsonValue.enableTls as any) === 'true'; + } + if (!this.adminSettings.jsonValue.providerId) { + this.adminSettings.jsonValue.providerId = 'CUSTOM'; + } + this.mailSettings.reset(this.adminSettings.jsonValue, {emitEvent: false}); + if (this.isTenantAdmin()) { + this.mailSettings.get('useSystemMailSettings').setValue( + isDefined(this.adminSettings.jsonValue.useSystemMailSettings) ? + this.adminSettings.jsonValue.useSystemMailSettings : true, {emitEvent: false} + ); + } + this.updateValidators(); + this.enableTls(this.adminSettings.jsonValue.enableTls); + this.helpLink = this.templates.get(this.adminSettings.jsonValue.providerId)?.helpLink || null; + this.enableProxyChanged(); + if (this.adminSettings.jsonValue.enableOauth2) { + this.enableOauth2(!!this.adminSettings.jsonValue.enableOauth2); + this.enableProviderTenantIdChanged(this.adminSettings.jsonValue.providerId); + this.parseUrl(this.adminSettings.jsonValue.redirectUri); + this.mailSettings.get('redirectUri').patchValue(this.adminSettings.jsonValue.redirectUri, {emitEvent: false}); + } else { + this.mailSettings.get('enableOauth2').patchValue(false, {emitEvent: false}); + } } ngOnDestroy() { diff --git a/ui-ngx/src/app/modules/home/pages/admin/sms-provider.component.ts b/ui-ngx/src/app/modules/home/pages/admin/sms-provider.component.ts index 1a89a7a6b4..ae7eea5fe2 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/sms-provider.component.ts +++ b/ui-ngx/src/app/modules/home/pages/admin/sms-provider.component.ts @@ -19,6 +19,7 @@ import { getCurrentAuthUser } from '@core/auth/auth.selectors'; import { Operation, Resource } from '@shared/models/security.models'; import { UserPermissionsService } from '@core/http/user-permissions.service'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'tb-sms-provider', @@ -42,30 +43,17 @@ export class SmsProviderComponent extends PageComponent implements HasConfirmFor private notificationService: NotificationService, private dialog: MatDialog, private userPermissionsService: UserPermissionsService, + private route: ActivatedRoute, public fb: FormBuilder, private destroyRef: DestroyRef) { super(store); this.buildSmsProviderForm(); this.buildGeneralServerSettingsForm(); - this.notificationService.getNotificationSettings().subscribe( - (settings) => { - this.notificationSettings = settings; - this.setNotificationSettings(this.notificationSettings); - } - ); - this.adminService.getAdminSettings('sms', false, {ignoreErrors: true}).subscribe({ - next: adminSettings => { - this.adminSettings = adminSettings; - this.setSmsProviderSettings(this.adminSettings.jsonValue); - }, - error: () => { - this.adminSettings = { - key: 'sms', - jsonValue: null - }; - this.setSmsProviderSettings(this.adminSettings.jsonValue); - } - }); + const data = this.route.snapshot.data.notificationsSettings; + this.notificationSettings = data.notificationSettings; + this.setNotificationSettings(this.notificationSettings); + this.adminSettings = data.smsProviderSettings; + this.setSmsProviderSettings(this.adminSettings.jsonValue); } private setSmsProviderSettings(smsProviderConfiguration: SmsProviderConfiguration) { @@ -193,7 +181,9 @@ export class SmsProviderComponent extends PageComponent implements HasConfirmFor if(this.readonly) { this.notificationSettingsForm.disable({emitEvent: false}); } else { - this.isLoading$.subscribe((isLoading) => { + this.isLoading$.pipe( + takeUntilDestroyed(this.destroyRef) + ).subscribe((isLoading) => { if (isLoading) { this.notificationSettingsForm.disable({emitEvent: false}); } else { diff --git a/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings-routing.module.ts b/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings-routing.module.ts index bdb2855036..47bd8eebb0 100644 --- a/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings-routing.module.ts +++ b/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings-routing.module.ts @@ -1,13 +1,36 @@ // SPDX-FileCopyrightText: Copyright The ThingsBoard Authors // SPDX-License-Identifier: Apache-2.0 -import { NgModule } from '@angular/core'; -import { RouterModule, Routes } from '@angular/router'; +import { inject, NgModule } from '@angular/core'; +import { ActivatedRouteSnapshot, ResolveFn, RouterModule, RouterStateSnapshot, Routes } from '@angular/router'; import { Authority } from '@shared/models/authority.enum'; import { MenuId } from '@core/services/menu.models'; import { MobileQrCodeWidgetSettingsComponent } from '@home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings.component'; import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard'; +import { forkJoin, of } from 'rxjs'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { getCurrentAuthUser } from '@core/auth/auth.selectors'; +import { MobileApplicationService } from '@core/http/mobile-application.service'; +import { WhiteLabelingService } from '@core/http/white-labeling.service'; +import { QrCodeSettings } from '@shared/models/mobile-app.models'; +import { LoginWhiteLabelingParams } from '@shared/models/white-labeling.models'; + +export const qrCodeWidgetSettingsResolver: ResolveFn<{ + mobileAppSettings: QrCodeSettings; + loginWhiteLabelParams: LoginWhiteLabelingParams; +}> = ( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot, + store = inject(Store), + mobileAppService = inject(MobileApplicationService), + wl = inject(WhiteLabelingService) +) => forkJoin({ + mobileAppSettings: mobileAppService.getMobileAppSettings(), + loginWhiteLabelParams: getCurrentAuthUser(store).authority === Authority.TENANT_ADMIN ? + wl.getCurrentLoginWhiteLabelParams() : of(null as LoginWhiteLabelingParams) +}); export const qrCodeWidgetRoutes: Routes = [ { @@ -20,6 +43,9 @@ export const qrCodeWidgetRoutes: Routes = [ breadcrumb: { menuId: MenuId.mobile_qr_code_widget } + }, + resolve: { + qrCodeWidgetSettings: qrCodeWidgetSettingsResolver } } ]; diff --git a/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings.component.ts b/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings.component.ts index a0cc535039..8fc77b3bce 100644 --- a/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings.component.ts +++ b/ui-ngx/src/app/modules/home/pages/mobile/qr-code-widget/mobile-qr-code-widget-settings.component.ts @@ -16,7 +16,7 @@ import { Authority } from '@shared/models/authority.enum'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Operation, Resource } from '@shared/models/security.models'; import { UserPermissionsService } from '@core/http/user-permissions.service'; -import { WhiteLabelingService } from '@core/http/white-labeling.service'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'tb-mobile-qr-code-widget', @@ -55,15 +55,8 @@ export class MobileQrCodeWidgetSettingsComponent extends PageComponent implement private mobileAppService: MobileApplicationService, private fb: FormBuilder, private userPermissionsService: UserPermissionsService, - private wl: WhiteLabelingService) { + private route: ActivatedRoute) { super(store); - this.mobileAppService.getMobileAppSettings() - .subscribe(settings => this.processMobileAppSettings(settings)); - - if(this.isTenantAdmin()) { - this.wl.getCurrentLoginWhiteLabelParams().subscribe(value => this.setBaseURL = !!value.baseUrl); - } - if (this.readonly) { this.mobileAppSettingsForm.disable() } else { @@ -142,6 +135,12 @@ export class MobileQrCodeWidgetSettingsComponent extends PageComponent implement } }); } + + const data = this.route.snapshot.data.qrCodeWidgetSettings; + if (this.isTenantAdmin()) { + this.setBaseURL = !!data.loginWhiteLabelParams.baseUrl; + } + this.processMobileAppSettings(data.mobileAppSettings); } public isTenantAdmin(): boolean {