wip ref(core): Profile & ProfileGroup add creation methods in ProfileService

This commit is contained in:
Clem Fern 2023-07-23 22:23:57 +02:00
parent a0804cc564
commit 1c06a510bd
3 changed files with 50 additions and 10 deletions

View file

@ -9,6 +9,8 @@ import { configMerge, ConfigProxy, ConfigService } from './config.service'
import { NotificationsService } from './notifications.service'
import { SelectorService } from './selector.service'
import deepClone from 'clone-deep'
import { v4 as uuidv4 } from 'uuid'
import slugify from 'slugify'
@Injectable({ providedIn: 'root' })
export class ProfilesService {
@ -89,6 +91,28 @@ export class ProfilesService {
return clone ? deepClone(list) : list
}
/**
* Insert a new Profile in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
* arg: genId (default: true) -> generate uuid in before pushing Profile into config
*/
async newProfile (profile: PartialProfile<Profile>, saveConfig = true, genId = true): Promise<void> {
if (genId) {
profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}`
}
const cProfile = this.config.store.profiles.find(p => p.id === profile.id)
if (cProfile) {
throw new Error(`Cannot insert new Profile, duplicated Id: ${profile.id}`)
}
this.config.store.profiles.push(profile)
if (saveConfig) {
return this.config.save()
}
}
/**
* Write a Profile in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
@ -417,6 +441,28 @@ export class ProfilesService {
return groups
}
/**
* Insert a new ProfileGroup in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
* arg: genId (default: true) -> generate uuid in before pushing Profile into config
*/
async newProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true, genId = true): Promise<void> {
if (genId) {
group.id = `${uuidv4()}`
}
const cProfileGroup = this.config.store.groups.find(p => p.id === group.id)
if (cProfileGroup) {
throw new Error(`Cannot insert new ProfileGroup, duplicated Id: ${group.id}`)
}
this.config.store.groups.push(group)
if (saveConfig) {
return this.config.save()
}
}
/**
* Write a ProfileGroup in config
* arg: saveConfig (default: true) -> invoke after the ProfileGroup was updated

View file

@ -99,7 +99,7 @@ export class EditProfileModalComponent<P extends Profile> {
id: uuidv4(),
name: this.profileGroup,
}
this.profilesService.writeProfileGroup(newGroup, false)
this.profilesService.newProfileGroup(newGroup, false, false)
this.profileGroup = newGroup
}
this.profile.group = this.profileGroup.id

View file

@ -1,6 +1,4 @@
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'
import { v4 as uuidv4 } from 'uuid'
import slugify from 'slugify'
import deepClone from 'clone-deep'
import { Component, Inject } from '@angular/core'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
@ -16,7 +14,6 @@ _('Ungrouped')
styleUrls: ['./profilesSettingsTab.component.scss'],
})
export class ProfilesSettingsTabComponent extends BaseComponent {
profiles: PartialProfile<Profile>[] = []
builtinProfiles: PartialProfile<Profile>[] = []
templateProfiles: PartialProfile<Profile>[] = []
profileGroups: PartialProfileGroup<ProfileGroup>[]
@ -39,7 +36,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
async ngOnInit (): Promise<void> {
this.refresh()
this.builtinProfiles = (await this.profilesService.getProfiles()).filter(x => x.isBuiltin)
this.builtinProfiles = (await this.profilesService.getProfiles(true, false)).filter(x => x.isBuiltin)
this.templateProfiles = this.builtinProfiles.filter(x => x.isTemplate)
this.builtinProfiles = this.builtinProfiles.filter(x => !x.isTemplate)
this.refresh()
@ -52,7 +49,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
async newProfile (base?: PartialProfile<Profile>): Promise<void> {
if (!base) {
let profiles = [...this.templateProfiles, ...this.builtinProfiles, ...this.profiles]
let profiles = await this.profilesService.getProfiles()
profiles = profiles.filter(x => !this.isProfileBlacklisted(x))
profiles.sort((a, b) => (a.weight ?? 0) - (b.weight ?? 0))
base = await this.selector.show(
@ -83,9 +80,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
const cfgProxy = this.profilesService.getConfigProxyForProfile(profile)
profile.name = this.profilesService.providerForProfile(profile)?.getSuggestedName(cfgProxy) ?? this.translate.instant('{name} copy', base)
}
profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}`
this.config.store.profiles = [profile, ...this.config.store.profiles]
await this.config.save()
this.profilesService.newProfile(profile)
}
async editProfile (profile: PartialProfile<Profile>): Promise<void> {
@ -142,7 +137,6 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
}
async refresh (): Promise<void> {
this.profiles = this.config.store.profiles
const groups = await this.profilesService.getProfileGroups(true, true)
groups.sort((a, b) => a.name.localeCompare(b.name))
groups.sort((a, b) => (a.id === 'built-in' ? 1 : 0) - (b.id === 'built-in' ? 1 : 0))