common-ui/src/lib/services/iqser-config.service.ts
2024-06-19 13:05:12 +03:00

42 lines
1.6 KiB
TypeScript

import { Inject, inject, Injectable } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { CacheApiService } from '../caching/cache-api.service';
import { wipeAllCaches } from '../caching/cache-utils';
import { IqserAppConfig } from '../utils/iqser-app-config';
@Injectable()
export class IqserConfigService<T extends IqserAppConfig = IqserAppConfig> {
protected readonly _cacheApiService = inject(CacheApiService);
protected readonly _titleService = inject(Title);
constructor(@Inject('Doesnt matter') protected _values: T) {
this._checkFrontendVersion();
this._titleService.setTitle(this._values.APP_NAME);
}
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);
}
protected _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<T extends IqserAppConfig = IqserAppConfig>() {
return inject<IqserConfigService<T>>(IqserConfigService).values;
}