Browse Source

feat: improve config-state.service

pull/6156/head
mehmet-erim 6 years ago
parent
commit
beac5fa4e1
  1. 120
      npm/ng-packs/packages/core/src/lib/services/config-state.service.ts

120
npm/ng-packs/packages/core/src/lib/services/config-state.service.ts

@ -1,68 +1,122 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store'; import { Observable } from 'rxjs';
import { GetAppConfiguration, SetEnvironment } from '../actions/config.actions'; import { map } from 'rxjs/operators';
import { ConfigState } from '../states'; import { ApplicationConfiguration } from '../models/application-configuration';
import { InternalStore } from '../utils/internal-store-utils';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class ConfigStateService { export class ConfigStateService {
constructor(private store: Store) {} private readonly store = new InternalStore({} as ApplicationConfiguration.Response);
getAll() { readonly onUpdate$ = this.store.sliceUpdate;
return this.store.selectSnapshot(ConfigState.getAll);
setState = (state: ApplicationConfiguration.Response) => {
this.store.patch(state);
};
getOne$(key: string) {
return this.store.sliceState(state => state[key]);
} }
getApplicationInfo() { getOne(key: string) {
return this.store.selectSnapshot(ConfigState.getApplicationInfo); return this.store.state[key];
} }
getEnvironment() { getAll$(): Observable<ApplicationConfiguration.Response> {
return this.store.selectSnapshot(ConfigState.getEnvironment); return this.store.sliceState(state => state);
} }
getOne(...args: Parameters<typeof ConfigState.getOne>) { getAll(): ApplicationConfiguration.Response {
return this.store.selectSnapshot(ConfigState.getOne(...args)); return this.store.state;
} }
getDeep(...args: Parameters<typeof ConfigState.getDeep>) { getDeep$(keys: string[] | string) {
return this.store.selectSnapshot(ConfigState.getDeep(...args)); keys = splitKeys(keys);
return this.store
.sliceState(state => state)
.pipe(
map(state => {
return (keys as string[]).reduce((acc, val) => {
if (acc) {
return acc[val];
}
return undefined;
}, state);
}),
);
} }
getApiUrl(...args: Parameters<typeof ConfigState.getApiUrl>) { getDeep(keys: string[] | string) {
return this.store.selectSnapshot(ConfigState.getApiUrl(...args)); keys = splitKeys(keys);
return (keys as string[]).reduce((acc, val) => {
if (acc) {
return acc[val];
}
return undefined;
}, this.store.state);
} }
getFeature(...args: Parameters<typeof ConfigState.getFeature>) { getFeature(key: string) {
return this.store.selectSnapshot(ConfigState.getFeature(...args)); return this.store.state.features?.values?.[key];
} }
getSetting(...args: Parameters<typeof ConfigState.getSetting>) { getFeature$(key: string) {
return this.store.selectSnapshot(ConfigState.getSetting(...args)); return this.store.sliceState(state => state.features?.values?.[key]);
} }
getSettings(...args: Parameters<typeof ConfigState.getSettings>) { getSetting(key: string) {
return this.store.selectSnapshot(ConfigState.getSettings(...args)); return this.store.state.setting?.values?.[key];
} }
/** @deprecated Use PermissionService's getGrantedPolicySnapshot method */ getSetting$(key: string) {
getGrantedPolicy(...args: Parameters<typeof ConfigState.getGrantedPolicy>) { return this.store.sliceState(state => state.setting?.values?.[key]);
return this.store.selectSnapshot(ConfigState.getGrantedPolicy(...args));
} }
getLocalization(...args: Parameters<typeof ConfigState.getLocalization>) { getSettings(keyword?: string) {
return this.store.selectSnapshot(ConfigState.getLocalization(...args)); const settings = this.store.state.setting?.values || {};
if (!keyword) return settings;
const keysFound = Object.keys(settings).filter(key => key.indexOf(keyword) > -1);
return keysFound.reduce((acc, key) => {
acc[key] = settings[key];
return acc;
}, {});
} }
getLocalizationResource(...args: Parameters<typeof ConfigState.getLocalizationResource>) { getSettings$(keyword?: string) {
return this.store.selectSnapshot(ConfigState.getLocalizationResource(...args)); return this.store
.sliceState(state => state.setting?.values)
.pipe(
map((settings = {}) => {
if (!keyword) return settings;
const keysFound = Object.keys(settings).filter(key => key.indexOf(keyword) > -1);
return keysFound.reduce((acc, key) => {
acc[key] = settings[key];
return acc;
}, {});
}),
);
} }
}
dispatchGetAppConfiguration() { function splitKeys(keys: string[] | string): string[] {
return this.store.dispatch(new GetAppConfiguration()); if (typeof keys === 'string') {
keys = keys.split('.');
} }
dispatchSetEnvironment(...args: ConstructorParameters<typeof SetEnvironment>) { if (!Array.isArray(keys)) {
return this.store.dispatch(new SetEnvironment(...args)); throw new Error('The argument must be a dot string or an string array.');
} }
return keys;
} }

Loading…
Cancel
Save