@@ -10,19 +10,13 @@ import { PerunConfig } from '@perun-web-apps/perun/models';
10
10
providedIn : 'root' ,
11
11
} )
12
12
export class StoreService {
13
- private instanceConfig : PerunConfig ;
14
- private defaultConfig : PerunConfig ;
13
+ private config : PerunConfig ;
15
14
private appsConfig : PerunAppsConfig ;
16
15
private principal : PerunPrincipal ;
17
16
private initialPageId : number ;
18
- private branding = '' ;
19
-
20
- setInstanceConfig ( instanceConfig : PerunConfig ) : void {
21
- this . instanceConfig = instanceConfig ;
22
- }
23
17
24
18
setDefaultConfig ( defaultConfig : PerunConfig ) : void {
25
- this . defaultConfig = defaultConfig ;
19
+ this . config = defaultConfig ;
26
20
}
27
21
28
22
getAppsConfig ( ) : PerunAppsConfig {
@@ -57,35 +51,35 @@ export class StoreService {
57
51
return this . getProperty ( 'member_profile_attributes_friendly_names' ) ;
58
52
}
59
53
60
- setBanding ( branding : string ) : void {
61
- this . branding = branding ;
62
- }
63
-
64
54
getProperty < T extends keyof PerunConfig > ( key : T ) : PerunConfig [ T ] {
65
- if ( ! this . instanceConfig || ! this . defaultConfig ) {
55
+ if ( ! this . config ) {
66
56
return null ;
67
57
}
58
+ return this . config [ key ] ;
59
+ }
68
60
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
+ ) ;
80
73
}
81
- if ( currentValue === null ) {
82
- return defaultValue ;
83
- }
84
-
85
- return this . addMissingValuesToProperty ( currentValue , defaultValue ) ;
86
74
}
87
75
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 > (
89
83
value : T [ K ] ,
90
84
defaultValue : T [ K ]
91
85
) : T [ K ] {
0 commit comments