on settings save, return the new settings.
update the frontend to persist settings to the database. Using ScrutinyConfigService instead of TreoConfigService. Using snake case settings in frontend. Make sure we're using AppConfig type where possible.
This commit is contained in:
@@ -10,19 +10,47 @@ export type DashboardSort = 'status' | 'title' | 'age'
|
||||
|
||||
export type TemperatureUnit = 'celsius' | 'fahrenheit'
|
||||
|
||||
|
||||
enum MetricsNotifyLevel {
|
||||
Warn = 1,
|
||||
Fail = 2
|
||||
}
|
||||
|
||||
enum MetricsStatusFilterAttributes {
|
||||
All = 0,
|
||||
Critical = 1
|
||||
}
|
||||
|
||||
enum MetricsStatusThreshold {
|
||||
Smart = 1,
|
||||
Scrutiny = 2,
|
||||
|
||||
// shortcut
|
||||
Both = 3
|
||||
}
|
||||
|
||||
/**
|
||||
* AppConfig interface. Update this interface to strictly type your config
|
||||
* object.
|
||||
*/
|
||||
export interface AppConfig {
|
||||
theme: Theme;
|
||||
layout: Layout;
|
||||
theme?: Theme;
|
||||
layout?: Layout;
|
||||
|
||||
// Dashboard options
|
||||
dashboardDisplay: DashboardDisplay;
|
||||
dashboardSort: DashboardSort;
|
||||
dashboard_display?: DashboardDisplay;
|
||||
dashboard_sort?: DashboardSort;
|
||||
|
||||
temperature_unit?: TemperatureUnit;
|
||||
|
||||
// Settings from Scrutiny API
|
||||
|
||||
metrics?: {
|
||||
notify_level?: MetricsNotifyLevel
|
||||
status_filter_attributes?: MetricsStatusFilterAttributes
|
||||
status_threshold?: MetricsStatusThreshold
|
||||
}
|
||||
|
||||
temperatureUnit: TemperatureUnit;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,12 +62,17 @@ export interface AppConfig {
|
||||
* "ConfigService".
|
||||
*/
|
||||
export const appConfig: AppConfig = {
|
||||
theme : 'light',
|
||||
theme: 'light',
|
||||
layout: 'material',
|
||||
|
||||
dashboardDisplay: 'name',
|
||||
dashboardSort: 'status',
|
||||
dashboard_display: 'name',
|
||||
dashboard_sort: 'status',
|
||||
|
||||
temperatureUnit: 'celsius',
|
||||
temperature_unit: 'celsius',
|
||||
metrics: {
|
||||
notify_level: MetricsNotifyLevel.Fail,
|
||||
status_filter_attributes: MetricsStatusFilterAttributes.All,
|
||||
status_threshold: MetricsStatusThreshold.Both
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import {ModuleWithProviders, NgModule} from '@angular/core';
|
||||
import {ScrutinyConfigService} from 'app/core/config/scrutiny-config.service';
|
||||
import {TREO_APP_CONFIG} from '@treo/services/config/config.constants';
|
||||
|
||||
@NgModule()
|
||||
export class ScrutinyConfigModule {
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param {ScrutinyConfigService} _scrutinyConfigService
|
||||
*/
|
||||
constructor(
|
||||
private _scrutinyConfigService: ScrutinyConfigService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* forRoot method for setting user configuration
|
||||
*
|
||||
* @param config
|
||||
*/
|
||||
static forRoot(config: any): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: ScrutinyConfigModule,
|
||||
providers: [
|
||||
{
|
||||
provide: TREO_APP_CONFIG,
|
||||
useValue: config
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import {Inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {TREO_APP_CONFIG} from '@treo/services/config/config.constants';
|
||||
import {BehaviorSubject, Observable} from 'rxjs';
|
||||
import {getBasePath} from '../../app.routing';
|
||||
import {map, tap} from 'rxjs/operators';
|
||||
import {AppConfig} from './app.config';
|
||||
import {merge} from 'lodash';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ScrutinyConfigService {
|
||||
// Private
|
||||
private _config: BehaviorSubject<AppConfig>;
|
||||
private _defaultConfig: AppConfig;
|
||||
|
||||
constructor(
|
||||
private _httpClient: HttpClient,
|
||||
@Inject(TREO_APP_CONFIG) defaultConfig: AppConfig
|
||||
) {
|
||||
// Set the private defaults
|
||||
this._defaultConfig = defaultConfig
|
||||
this._config = new BehaviorSubject(null);
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Accessors
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Setter & getter for config
|
||||
*/
|
||||
set config(value: AppConfig) {
|
||||
// get the current config, merge the new values, and then submit. (setTheme only sets a single key, not the whole obj)
|
||||
const mergedSettings = merge({}, this._config.getValue(), value);
|
||||
|
||||
console.log('saving settings...', mergedSettings)
|
||||
this._httpClient.post(getBasePath() + '/api/settings', mergedSettings).pipe(
|
||||
map((response: any) => {
|
||||
console.log('settings resp')
|
||||
return response.settings
|
||||
}),
|
||||
tap((settings: AppConfig) => {
|
||||
this._config.next(settings);
|
||||
return settings
|
||||
})
|
||||
).subscribe(resp => {
|
||||
console.log('updated settings', resp)
|
||||
})
|
||||
}
|
||||
|
||||
get config$(): Observable<AppConfig> {
|
||||
if (this._config.getValue()) {
|
||||
console.log('using cached settings:', this._config.getValue())
|
||||
return this._config.asObservable()
|
||||
} else {
|
||||
console.log('retrieving settings')
|
||||
return this._httpClient.get(getBasePath() + '/api/settings').pipe(
|
||||
map((response: any) => {
|
||||
return response.settings
|
||||
}),
|
||||
tap((settings: AppConfig) => {
|
||||
this._config.next(settings);
|
||||
return this._config.asObservable()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Public methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Resets the config to the default
|
||||
*/
|
||||
reset(): void {
|
||||
// Set the config
|
||||
this.config = this._defaultConfig
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user