Use app initializer to wait on settings

This commit is contained in:
Michael Shamoon 2022-05-07 00:41:35 -07:00
parent 321e0ced2a
commit 7d9575b7fd
3 changed files with 96 additions and 96 deletions

View File

@ -1,5 +1,5 @@
import { BrowserModule } from '@angular/platform-browser' import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core' import { APP_INITIALIZER, NgModule } from '@angular/core'
import { AppRoutingModule } from './app-routing.module' import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component' import { AppComponent } from './app.component'
import { import {
@ -87,6 +87,8 @@ import localeSr from '@angular/common/locales/sr'
import localeSv from '@angular/common/locales/sv' import localeSv from '@angular/common/locales/sv'
import localeTr from '@angular/common/locales/tr' import localeTr from '@angular/common/locales/tr'
import localeZh from '@angular/common/locales/zh' import localeZh from '@angular/common/locales/zh'
import { Observable } from 'rxjs'
import { SettingsService } from './services/settings.service'
registerLocaleData(localeBe) registerLocaleData(localeBe)
registerLocaleData(localeCs) registerLocaleData(localeCs)
@ -109,6 +111,12 @@ registerLocaleData(localeSv)
registerLocaleData(localeTr) registerLocaleData(localeTr)
registerLocaleData(localeZh) registerLocaleData(localeZh)
function initializeApp(settings: SettingsService) {
return () => {
return settings.initializeSettings()
}
}
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
@ -174,6 +182,12 @@ registerLocaleData(localeZh)
ColorSliderModule, ColorSliderModule,
], ],
providers: [ providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [SettingsService],
multi: true,
},
DatePipe, DatePipe,
CookieService, CookieService,
{ {

View File

@ -72,10 +72,6 @@ export class SettingsComponent implements OnInit, OnDestroy, DirtyComponent {
ngOnInit() { ngOnInit() {
this.savedViewService.listAll().subscribe((r) => { this.savedViewService.listAll().subscribe((r) => {
this.savedViews = r.results this.savedViews = r.results
this.settings
.retrieveSettings()
.pipe(first())
.subscribe(() => {
let storeData = { let storeData = {
bulkEditConfirmationDialogs: this.settings.get( bulkEditConfirmationDialogs: this.settings.get(
SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS
@ -140,10 +136,7 @@ export class SettingsComponent implements OnInit, OnDestroy, DirtyComponent {
}) })
// Initialize dirtyCheck // Initialize dirtyCheck
this.isDirty$ = dirtyCheck( this.isDirty$ = dirtyCheck(this.settingsForm, this.store.asObservable())
this.settingsForm,
this.store.asObservable()
)
// Record dirty in case we need to 'undo' appearance settings if not saved on close // Record dirty in case we need to 'undo' appearance settings if not saved on close
this.isDirty$.subscribe((dirty) => { this.isDirty$.subscribe((dirty) => {
@ -159,7 +152,6 @@ export class SettingsComponent implements OnInit, OnDestroy, DirtyComponent {
) )
}) })
}) })
})
} }
ngOnDestroy() { ngOnDestroy() {

View File

@ -139,7 +139,6 @@ export class SettingsService {
protected baseUrl: string = environment.apiBaseUrl + 'frontend_settings/' protected baseUrl: string = environment.apiBaseUrl + 'frontend_settings/'
private settings: Object = {} private settings: Object = {}
private settings$: Observable<Results<any>>
constructor( constructor(
rendererFactory: RendererFactory2, rendererFactory: RendererFactory2,
@ -150,20 +149,15 @@ export class SettingsService {
protected http: HttpClient protected http: HttpClient
) { ) {
this.renderer = rendererFactory.createRenderer(null, null) this.renderer = rendererFactory.createRenderer(null, null)
this.retrieveSettings()
.pipe(first())
.subscribe((response) => {
Object.assign(this.settings, response['settings'])
this.updateAppearanceSettings()
})
} }
public retrieveSettings(): Observable<Results<any>> { // this is called by the app initializer in app.module
if (!this.settings$) public initializeSettings(): Observable<Results<any>> {
this.settings$ = this.http.get<Results<any>>(this.baseUrl) let settings$ = this.http.get<Results<any>>(this.baseUrl)
return this.settings$ settings$.pipe(first()).subscribe((response) => {
Object.assign(this.settings, response['settings'])
})
return settings$
} }
public updateAppearanceSettings( public updateAppearanceSettings(