@@ -10,19 +10,13 @@ import { PerunConfig } from '@perun-web-apps/perun/models';
1010 providedIn : 'root' ,
1111} )
1212export class StoreService {
13- private instanceConfig : PerunConfig ;
14- private defaultConfig : PerunConfig ;
13+ private config : PerunConfig ;
1514 private appsConfig : PerunAppsConfig ;
1615 private principal : PerunPrincipal ;
1716 private initialPageId : number ;
18- private branding = '' ;
19-
20- setInstanceConfig ( instanceConfig : PerunConfig ) : void {
21- this . instanceConfig = instanceConfig ;
22- }
2317
2418 setDefaultConfig ( defaultConfig : PerunConfig ) : void {
25- this . defaultConfig = defaultConfig ;
19+ this . config = defaultConfig ;
2620 }
2721
2822 getAppsConfig ( ) : PerunAppsConfig {
@@ -57,35 +51,35 @@ export class StoreService {
5751 return this . getProperty ( 'member_profile_attributes_friendly_names' ) ;
5852 }
5953
60- setBanding ( branding : string ) : void {
61- this . branding = branding ;
62- }
63-
6454 getProperty < T extends keyof PerunConfig > ( key : T ) : PerunConfig [ T ] {
65- if ( ! this . instanceConfig || ! this . defaultConfig ) {
55+ if ( ! this . config ) {
6656 return null ;
6757 }
58+ return this . config [ key ] ;
59+ }
6860
69- const configs : PerunConfig [ ] = [
70- this . instanceConfig ?. brandings ?. [ this . branding ] ,
71- this . instanceConfig ,
72- ] ;
73-
74- const defaultValue : PerunConfig [ T ] = this . defaultConfig [ key ] ;
75- let currentValue : PerunConfig [ T ] = null ;
76- for ( const config of configs ) {
77- if ( config && ( currentValue === null || currentValue === undefined ) ) {
78- currentValue = config [ key ] ;
79- }
61+ /*
62+ Merges a config into the existing config, substituting values in the existing with values in the configToMerge.
63+ Objects are merged per property, not as a whole (see `addMissingValuesToProperty`)
64+ Be careful of order of merging (make sure default config is set before merging another config)
65+ */
66+ mergeConfig < T extends object , K extends keyof T > ( configToMerge : PerunConfig ) : void {
67+ for ( const key of Object . keys ( configToMerge ) ) {
68+ if ( key === 'brandings' ) continue ;
69+ this . config [ key ] = this . addMissingValuesToProperty (
70+ configToMerge [ key ] as T [ K ] ,
71+ this . config [ key ] as T [ K ]
72+ ) ;
8073 }
81- if ( currentValue === null ) {
82- return defaultValue ;
83- }
84-
85- return this . addMissingValuesToProperty ( currentValue , defaultValue ) ;
8674 }
8775
88- addMissingValuesToProperty < T extends object , K extends keyof T > (
76+ /*
77+ For an object property, merge properties from the `defaultValue` with properties from `value`.
78+ Properties defined in `value` are overwritten with their value, remaining properties are kept from `defaultValue`
79+ Returns `value` if a simple type is passed.
80+ Works recursively
81+ */
82+ private addMissingValuesToProperty < T extends object , K extends keyof T > (
8983 value : T [ K ] ,
9084 defaultValue : T [ K ]
9185 ) : T [ K ] {
0 commit comments