add base config service
This commit is contained in:
parent
bc799eae4d
commit
93d3f294d6
@ -1,4 +1,4 @@
|
||||
import { ModuleWithProviders, NgModule, Provider } from '@angular/core';
|
||||
import { ModuleWithProviders, NgModule, Provider, Type } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
@ -34,9 +34,10 @@ import { DragDropFileUploadDirective } from './upload-file/drag-drop-file-upload
|
||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||
import { ConfirmationDialogComponent } from './dialog';
|
||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { DefaultUserPreferenceService } from './services/default-user-preference.service';
|
||||
import { CommonUiOptions } from './utils/types/common-ui-options';
|
||||
import { BaseUserPreferenceService } from './services';
|
||||
import { BaseConfigService } from './services/base-config.service';
|
||||
import { DefaultUserPreferenceService } from './services/default-user-preference.service';
|
||||
|
||||
const matModules = [MatIconModule, MatProgressSpinnerModule, MatButtonModule, MatDialogModule, MatCheckboxModule, MatTooltipModule];
|
||||
const modules = [
|
||||
@ -75,8 +76,14 @@ const pipes = [SortByPipe, HumanizePipe, CapitalizePipe];
|
||||
exports: [...components, ...pipes, ...modules],
|
||||
})
|
||||
export class CommonUiModule {
|
||||
static forRoot<T extends BaseUserPreferenceService>(options?: CommonUiOptions<T>): ModuleWithProviders<CommonUiModule> {
|
||||
const userPreferenceService = this._getUserPreferenceService(options);
|
||||
static forRoot<T extends BaseUserPreferenceService, C extends BaseConfigService>(
|
||||
options?: CommonUiOptions<T, C>,
|
||||
): ModuleWithProviders<CommonUiModule> {
|
||||
const userPreferenceService = this._getService(
|
||||
BaseUserPreferenceService,
|
||||
DefaultUserPreferenceService,
|
||||
options?.existingUserPreferenceService,
|
||||
);
|
||||
|
||||
return {
|
||||
ngModule: CommonUiModule,
|
||||
@ -84,13 +91,13 @@ export class CommonUiModule {
|
||||
};
|
||||
}
|
||||
|
||||
private static _getUserPreferenceService<T extends BaseUserPreferenceService>(options?: CommonUiOptions<T>): Provider {
|
||||
if (options?.existingUserPreferenceService) {
|
||||
private static _getService<B, D, E extends B>(base: B, _default: Type<D>, existing?: E): Provider {
|
||||
if (existing) {
|
||||
return {
|
||||
provide: BaseUserPreferenceService,
|
||||
useExisting: options.existingUserPreferenceService,
|
||||
provide: base,
|
||||
useExisting: existing,
|
||||
};
|
||||
}
|
||||
return { provide: BaseUserPreferenceService, useClass: DefaultUserPreferenceService };
|
||||
return { provide: base, useClass: _default };
|
||||
}
|
||||
}
|
||||
|
||||
38
src/lib/services/base-config.service.ts
Normal file
38
src/lib/services/base-config.service.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { Title } from '@angular/platform-browser';
|
||||
import { CacheApiService, wipeAllCaches } from '../caching';
|
||||
import { BaseAppConfig } from '../utils';
|
||||
|
||||
export class BaseConfigService<T extends BaseAppConfig = BaseAppConfig> {
|
||||
protected readonly _cacheApiService = inject(CacheApiService);
|
||||
protected readonly _titleService = inject(Title);
|
||||
|
||||
constructor(protected _values: T) {
|
||||
this._checkFrontendVersion();
|
||||
}
|
||||
|
||||
get values() {
|
||||
return this._values;
|
||||
}
|
||||
|
||||
updateDisplayName(name: string): void {
|
||||
this._values = { ...this._values, APP_NAME: name ?? this._values.APP_NAME };
|
||||
this._titleService.setTitle(this._values.APP_NAME);
|
||||
}
|
||||
|
||||
private _checkFrontendVersion(): void {
|
||||
this._cacheApiService.getCachedValue('FRONTEND_APP_VERSION').then(async lastVersion => {
|
||||
const version = this._values.FRONTEND_APP_VERSION;
|
||||
console.log('Last app version: ', lastVersion, ' current version ', version);
|
||||
if (lastVersion !== version) {
|
||||
console.warn('Version-mismatch - wiping caches!');
|
||||
await wipeAllCaches();
|
||||
}
|
||||
await this._cacheApiService.cacheValue('FRONTEND_APP_VERSION', version);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function getConfig() {
|
||||
return inject(BaseConfigService).values;
|
||||
}
|
||||
@ -7,3 +7,4 @@ export * from './stats.service';
|
||||
export * from './entities-map.service';
|
||||
export * from './base-user-preference.service';
|
||||
export * from './language.service';
|
||||
export * from './base-config.service';
|
||||
|
||||
8
src/lib/utils/base-app-config.ts
Normal file
8
src/lib/utils/base-app-config.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export interface BaseAppConfig {
|
||||
readonly API_URL: string;
|
||||
readonly APP_NAME: string;
|
||||
readonly FRONTEND_APP_VERSION: string;
|
||||
readonly OAUTH_CLIENT_ID: string;
|
||||
readonly OAUTH_IDP_HINT: string;
|
||||
readonly OAUTH_URL: string;
|
||||
}
|
||||
@ -18,3 +18,4 @@ export * from './custom-route-reuse.strategy';
|
||||
export * from './headers-configuration';
|
||||
export * from './context.component';
|
||||
export * from './tokens';
|
||||
export * from './base-app-config';
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { BaseConfigService, BaseUserPreferenceService } from '../../services';
|
||||
import { Type } from '@angular/core';
|
||||
import { BaseUserPreferenceService } from '../../services';
|
||||
|
||||
export interface CommonUiOptions<T extends BaseUserPreferenceService> {
|
||||
export interface CommonUiOptions<T extends BaseUserPreferenceService, C extends BaseConfigService> {
|
||||
existingUserPreferenceService: Type<T>;
|
||||
configServiceFactory: () => C;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user