refactor(frontend): rename booklore-ui to frontend
- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import {effect, inject, Injectable, signal} from '@angular/core';
|
||||
import {DashboardConfig, DEFAULT_DASHBOARD_CONFIG, ScrollerType} from '../models/dashboard-config.model';
|
||||
import {UserService} from '../../settings/user-management/user.service';
|
||||
import {MagicShelfService} from '../../magic-shelf/service/magic-shelf.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DashboardConfigService {
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly magicShelfService = inject(MagicShelfService);
|
||||
|
||||
private readonly _config = signal<DashboardConfig>(this.cloneConfig(DEFAULT_DASHBOARD_CONFIG));
|
||||
readonly config = this._config.asReadonly();
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const user = this.userService.currentUser();
|
||||
if (user) {
|
||||
const dashboardConfig = user.userSettings?.dashboardConfig as DashboardConfig;
|
||||
this._config.set(this.normalizeConfig(dashboardConfig));
|
||||
}
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
const shelves = this.magicShelfService.shelves();
|
||||
const currentConfig = this._config();
|
||||
const syncedConfig = this.syncMagicShelfTitles(currentConfig, shelves);
|
||||
|
||||
if (syncedConfig) {
|
||||
this._config.set(syncedConfig);
|
||||
const user = this.userService.getCurrentUser();
|
||||
if (user) {
|
||||
this.userService.updateUserSetting(user.id, 'dashboardConfig', syncedConfig);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
saveConfig(config: DashboardConfig): void {
|
||||
const nextConfig = this.normalizeConfig(config);
|
||||
this._config.set(nextConfig);
|
||||
|
||||
const user = this.userService.getCurrentUser();
|
||||
if (user) {
|
||||
this.userService.updateUserSetting(user.id, 'dashboardConfig', nextConfig);
|
||||
}
|
||||
}
|
||||
|
||||
resetToDefault(): void {
|
||||
this.saveConfig(DEFAULT_DASHBOARD_CONFIG);
|
||||
}
|
||||
|
||||
private normalizeConfig(config: DashboardConfig | null | undefined): DashboardConfig {
|
||||
if (!config?.scrollers?.length) {
|
||||
return this.cloneConfig(DEFAULT_DASHBOARD_CONFIG);
|
||||
}
|
||||
|
||||
return this.cloneConfig(config);
|
||||
}
|
||||
|
||||
private cloneConfig(config: DashboardConfig): DashboardConfig {
|
||||
return {
|
||||
scrollers: config.scrollers.map(scroller => ({...scroller}))
|
||||
};
|
||||
}
|
||||
|
||||
private syncMagicShelfTitles(
|
||||
config: DashboardConfig,
|
||||
shelves: Array<{ id?: number | null; name: string }>
|
||||
): DashboardConfig | null {
|
||||
let updated = false;
|
||||
|
||||
const scrollers = config.scrollers.map(scroller => {
|
||||
if (scroller.type !== ScrollerType.MAGIC_SHELF || !scroller.magicShelfId) {
|
||||
return scroller;
|
||||
}
|
||||
|
||||
const shelf = shelves.find(currentShelf => currentShelf.id === scroller.magicShelfId);
|
||||
if (!shelf || scroller.title === shelf.name) {
|
||||
return scroller;
|
||||
}
|
||||
|
||||
updated = true;
|
||||
return {
|
||||
...scroller,
|
||||
title: shelf.name
|
||||
};
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {scrollers};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user