From beac5fa4e1d81b67d98a118c3621e2869dea5745 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 11 Nov 2020 15:00:13 +0300 Subject: [PATCH] feat: improve config-state.service --- .../src/lib/services/config-state.service.ts | 120 +++++++++++++----- 1 file changed, 87 insertions(+), 33 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts index e7bbc8599d..abc1bdb115 100644 --- a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts @@ -1,68 +1,122 @@ import { Injectable } from '@angular/core'; -import { Store } from '@ngxs/store'; -import { GetAppConfiguration, SetEnvironment } from '../actions/config.actions'; -import { ConfigState } from '../states'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { ApplicationConfiguration } from '../models/application-configuration'; +import { InternalStore } from '../utils/internal-store-utils'; @Injectable({ providedIn: 'root', }) export class ConfigStateService { - constructor(private store: Store) {} + private readonly store = new InternalStore({} as ApplicationConfiguration.Response); - getAll() { - return this.store.selectSnapshot(ConfigState.getAll); + readonly onUpdate$ = this.store.sliceUpdate; + + setState = (state: ApplicationConfiguration.Response) => { + this.store.patch(state); + }; + + getOne$(key: string) { + return this.store.sliceState(state => state[key]); } - getApplicationInfo() { - return this.store.selectSnapshot(ConfigState.getApplicationInfo); + getOne(key: string) { + return this.store.state[key]; } - getEnvironment() { - return this.store.selectSnapshot(ConfigState.getEnvironment); + getAll$(): Observable { + return this.store.sliceState(state => state); } - getOne(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getOne(...args)); + getAll(): ApplicationConfiguration.Response { + return this.store.state; } - getDeep(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getDeep(...args)); + getDeep$(keys: string[] | string) { + 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) { - return this.store.selectSnapshot(ConfigState.getApiUrl(...args)); + getDeep(keys: string[] | string) { + keys = splitKeys(keys); + + return (keys as string[]).reduce((acc, val) => { + if (acc) { + return acc[val]; + } + + return undefined; + }, this.store.state); } - getFeature(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getFeature(...args)); + getFeature(key: string) { + return this.store.state.features?.values?.[key]; } - getSetting(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getSetting(...args)); + getFeature$(key: string) { + return this.store.sliceState(state => state.features?.values?.[key]); } - getSettings(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getSettings(...args)); + getSetting(key: string) { + return this.store.state.setting?.values?.[key]; } - /** @deprecated Use PermissionService's getGrantedPolicySnapshot method */ - getGrantedPolicy(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getGrantedPolicy(...args)); + getSetting$(key: string) { + return this.store.sliceState(state => state.setting?.values?.[key]); } - getLocalization(...args: Parameters) { - return this.store.selectSnapshot(ConfigState.getLocalization(...args)); + getSettings(keyword?: string) { + 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) { - return this.store.selectSnapshot(ConfigState.getLocalizationResource(...args)); + getSettings$(keyword?: string) { + 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() { - return this.store.dispatch(new GetAppConfiguration()); +function splitKeys(keys: string[] | string): string[] { + if (typeof keys === 'string') { + keys = keys.split('.'); } - dispatchSetEnvironment(...args: ConstructorParameters) { - return this.store.dispatch(new SetEnvironment(...args)); + if (!Array.isArray(keys)) { + throw new Error('The argument must be a dot string or an string array.'); } + + return keys; }