T hoguet/issue5 (#37)
* enregistrer les calendar settings dans le localstorage Fixes #5 * Refactor calendar settings service to use a definitions map for settings and update related components to access settings via a getter method * Update src/app/services/calendar-settings-service.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor calendar settings service to utilize a new storedSignal implementation for localStorage persistence and restore the stored-signal utility * Update typescript-eslint to version 8.46.3 and enhance error handling in storedSignal function * Update src/app/services/calendar-settings-service.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Enhance storedSignal function to accept an Injector for improved context handling in localStorage persistence * Skip initial execution in storedSignal effect to prevent unnecessary localStorage writes * Refactor get method in CalendarSettingsService to throw an error if the setting ID doesn't exist --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
1922b9acb2
commit
81d974cbfe
9 files changed
+199
-36
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { Injectable, signal, WritableSignal } from '@angular/core';
|
||||
import { Injectable, WritableSignal, inject, Injector } from '@angular/core';
|
||||
import { storedSignal } from '../shared/utils/stored-signal';
|
||||
|
||||
interface CalendarSetting {
|
||||
signal: WritableSignal<boolean>;
|
||||
id: string;
|
||||
interface SettingDefinition {
|
||||
defaultValue: boolean;
|
||||
description: string;
|
||||
}
|
||||
|
||||
@@ -10,19 +10,59 @@ interface CalendarSetting {
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class CalendarSettingsService {
|
||||
readonly showWeekNumbers = signal(false);
|
||||
readonly samediMalin = signal(true);
|
||||
private readonly injector = inject(Injector);
|
||||
|
||||
readonly settings: readonly CalendarSetting[] = [
|
||||
{
|
||||
signal: this.showWeekNumbers,
|
||||
id: 'showWeekNumbers',
|
||||
description: 'Afficher les numéros de semaine',
|
||||
},
|
||||
{
|
||||
signal: this.samediMalin,
|
||||
id: 'samediMalin',
|
||||
description: 'Activer le Samedi Malin',
|
||||
},
|
||||
];
|
||||
private readonly definitions = new Map<string, SettingDefinition>([
|
||||
[
|
||||
'showWeekNumbers',
|
||||
{
|
||||
defaultValue: false,
|
||||
description: 'Afficher les numéros de semaine',
|
||||
},
|
||||
],
|
||||
[
|
||||
'samediMalin',
|
||||
{
|
||||
defaultValue: true,
|
||||
description: 'Activer le Samedi Malin',
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
private readonly signals = new Map<string, WritableSignal<boolean>>();
|
||||
|
||||
readonly settingsArray: ReadonlyArray<{
|
||||
id: string;
|
||||
signal: WritableSignal<boolean>;
|
||||
description: string;
|
||||
}>;
|
||||
|
||||
constructor() {
|
||||
for (const [id, def] of this.definitions) {
|
||||
const storageKey = `calendarSettings.${id}`;
|
||||
this.signals.set(id, storedSignal(storageKey, def.defaultValue, this.injector));
|
||||
}
|
||||
|
||||
this.settingsArray = Array.from(this.definitions.entries()).map(([id, def]) => ({
|
||||
id,
|
||||
signal: this.signals.get(id)!,
|
||||
description: def.description,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the signal for the given setting ID.
|
||||
* @throws Error if the setting ID doesn't exist
|
||||
*/
|
||||
get(id: string): WritableSignal<boolean> {
|
||||
const signal = this.signals.get(id);
|
||||
if (!signal) {
|
||||
throw new Error(`Setting with id "${id}" does not exist`);
|
||||
}
|
||||
return signal;
|
||||
}
|
||||
|
||||
getDescription(id: string): string {
|
||||
return this.definitions.get(id)?.description ?? '';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user