wip ref(core/profiles.service): add methods to interract with Provider defaults

This commit is contained in:
Clem Fern 2023-07-22 20:16:27 +02:00
parent ee4487a517
commit 8a85fcac21
2 changed files with 37 additions and 8 deletions

View file

@ -212,12 +212,7 @@ export class ProfilesService {
}
getConfigProxyForProfile <T extends Profile> (profile: PartialProfile<T>, skipUserDefaults = false): T {
const provider = this.providerForProfile(profile)
const defaults = [
this.profileDefaults,
provider?.configDefaults ?? {},
!provider || skipUserDefaults ? {} : this.config.store.profileDefaults[provider.id] ?? {},
].reduce(configMerge, {})
const defaults = this.getProfileDefaults(profile).reduce(configMerge, {})
return new ConfigProxy(profile, defaults) as unknown as T
}
@ -234,4 +229,38 @@ export class ProfilesService {
}
window.localStorage['recentProfiles'] = JSON.stringify(recentProfiles)
}
/*
* Methods used to interract with Profile/ProfileGroup/Global defaults
*/
/**
* Return global defaults for a given profile provider
* Always return something, empty object if no defaults found
*/
getProviderDefaults (provider: ProfileProvider<Profile>): any {
const defaults = this.config.store.profileDefaults
return defaults[provider.id] ?? {}
}
/**
* Set global defaults for a given profile provider
*/
setProviderDefaults (provider: ProfileProvider<Profile>, pdefaults: any) {
this.config.store.profileDefaults[provider.id] = pdefaults
}
/**
* Return defaults for a given profile
* Always return something, empty object if no defaults found
*/
getProfileDefaults (profile: PartialProfile<Profile>, skipUserDefaults = false): any {
const provider = this.providerForProfile(profile)
return [
this.profileDefaults,
provider?.configDefaults ?? {},
!provider || skipUserDefaults ? {} : this.getProviderDefaults(provider),
]
}
}

View file

@ -282,7 +282,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
EditProfileModalComponent,
{ size: 'lg' },
)
const model = this.config.store.profileDefaults[provider.id] ?? {}
const model = this.profilesService.getProviderDefaults(provider)
model.type = provider.id
modal.componentInstance.profile = Object.assign({}, model)
modal.componentInstance.profileProvider = provider
@ -295,7 +295,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
delete model[k]
}
Object.assign(model, result)
this.config.store.profileDefaults[provider.id] = model
this.profilesService.setProviderDefaults(provider, model)
await this.config.save()
}