From aab71394a618e049bd8bb206d737ddf89a91879f Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Mon, 23 May 2022 14:21:09 +0300 Subject: [PATCH] Implement PR comments #12660 --- docs/en/UI/Angular/Config-State-Service.md | 2 +- docs/en/UI/Angular/GlobalFeatures.md | 40 ++++++++++--------- .../src/lib/services/config-state.service.ts | 19 +++++---- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/docs/en/UI/Angular/Config-State-Service.md b/docs/en/UI/Angular/Config-State-Service.md index ffbf8884d6..b3884f65e1 100644 --- a/docs/en/UI/Angular/Config-State-Service.md +++ b/docs/en/UI/Angular/Config-State-Service.md @@ -13,7 +13,7 @@ import { ConfigStateService } from '@abp/ng.core'; /* class metadata here */ }) class DemoComponent { - constructor(private config: \) {} + constructor(private config: ConfigStateService) {} } ``` diff --git a/docs/en/UI/Angular/GlobalFeatures.md b/docs/en/UI/Angular/GlobalFeatures.md index 195233f5ec..78675bb08b 100644 --- a/docs/en/UI/Angular/GlobalFeatures.md +++ b/docs/en/UI/Angular/GlobalFeatures.md @@ -9,35 +9,39 @@ ````js import { ConfigStateService } from '@abp/ng.core'; +import { Component, OnInit } from '@angular/core'; @Component({ /* class metadata here */ }) -class DemoComponent { +class DemoComponent implements OnInit { constructor(private config: ConfigStateService) {} -} + + ngOnInit(): void { + // Gets all enabled global features. + const getGlobalFeatures = this.config.getGlobalFeatures(); -// Gets all enabled global features. -const getGlobalFeatures = this.config.getGlobalFeatures (); + //Example result is: `{ enabledFeatures: [ 'Shopping.Payment', 'Ecommerce.Subscription' ] }` -// { enabledFeatures: [ 'Shopping.Payment', 'Ecommerce.Subscription' ] } + // or + this.config.getGlobalFeatures$().subscribe(getGlobalFeatures => { + // use getGlobalFeatures here + }) -// or -this.config.getGlobalFeatures$().subscribe(getGlobalFeatures => { - // use getGlobalFeatures here -}) + // Check the global feature is enabled + this.config.getGlobalFeatureIsEnabled('Ecommerce.Subscription') -// Check the global feature is enabled -this.config.getGlobalFeatureIsEnabled('Ecommerce.Subscription') + //Example result is `true` -true + this.config.getGlobalFeatureIsEnabled('My.Subscription') -> this.config.getGlobalFeatureIsEnabled('My.Subscription') + //Example result is `false` -false + // or + this.config.getGlobalFeatureIsEnabled$('Ecommerce.Subscription').subscribe((isEnabled:boolean) => { + // use isEnabled here + }) + } +} -// or -this.config.getGlobalFeatureIsEnabled$('Ecommerce.Subscription').subscribe((isEnabled:boolean) => { - // use isEnabled here -}) 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 76488a38f2..f7c53c45af 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 @@ -147,22 +147,21 @@ export class ConfigStateService { return this.store.sliceState(state => state.globalFeatures); } - getGlobalFeatureIsEnabled(key: string) { - const globalFeatures = this.store.state.globalFeatures; - - if (!(globalFeatures?.enabledFeatures)) { return false }; + private isGlobalFeatureEnabled(key: string, globalFeatures: ApplicationGlobalFeatureConfigurationDto) { + const features = globalFeatures.enabledFeatures || [] + return features.some(f => key === f); + } - return globalFeatures.enabledFeatures.indexOf(key) != -1; + getGlobalFeatureIsEnabled(key: string) { + return this.isGlobalFeatureEnabled(key, this.store.state.globalFeatures); } getGlobalFeatureIsEnabled$(key: string) { - return this.store.sliceState(state => { - debugger - if (!(state.globalFeatures?.enabledFeatures)) { return false }; - return state.globalFeatures.enabledFeatures.indexOf(key) != -1 || true; - }); + return this.store.sliceState(state => this.isGlobalFeatureEnabled(key, state.globalFeatures)); } + + } function splitKeys(keys: string[] | string): string[] {