Fix applying theme from local broswer
This commit is contained in:
@@ -52,6 +52,7 @@ export class AppConfiguratorComponent {
|
||||
config: PrimeNG = inject(PrimeNG);
|
||||
configService: AppConfigService = inject(AppConfigService);
|
||||
presets = Object.keys(presets);
|
||||
platformId = inject(PLATFORM_ID);
|
||||
|
||||
surfaces = [
|
||||
{
|
||||
@@ -216,6 +217,14 @@ export class AppConfiguratorComponent {
|
||||
return palettes;
|
||||
});
|
||||
|
||||
|
||||
ngOnInit() {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
this.onPresetChange(this.configService.appState().preset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
getPresetExt() {
|
||||
const color = this.primaryColors().find((c) => c.name === this.selectedPrimaryColor());
|
||||
|
||||
|
||||
@@ -3,49 +3,49 @@ import {effect, inject, Injectable, PLATFORM_ID, signal} from '@angular/core';
|
||||
import {AppState} from '../model/state/app-state.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AppConfigService {
|
||||
private readonly STORAGE_KEY = 'appConfigState';
|
||||
appState = signal<AppState>({});
|
||||
document = inject(DOCUMENT);
|
||||
platformId = inject(PLATFORM_ID);
|
||||
private initialized = false;
|
||||
private readonly STORAGE_KEY = 'appConfigState';
|
||||
appState = signal<AppState>({});
|
||||
document = inject(DOCUMENT);
|
||||
platformId = inject(PLATFORM_ID);
|
||||
private initialized = false;
|
||||
|
||||
constructor() {
|
||||
const initialState = this.loadAppState();
|
||||
this.appState.set({...initialState});
|
||||
this.document.documentElement.classList.add('p-dark');
|
||||
effect(
|
||||
() => {
|
||||
const state = this.appState();
|
||||
if (!this.initialized || !state) {
|
||||
this.initialized = true;
|
||||
return;
|
||||
}
|
||||
this.saveAppState(state);
|
||||
},
|
||||
{allowSignalWrites: true}
|
||||
);
|
||||
}
|
||||
|
||||
private loadAppState(): AppState {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
const storedState = localStorage.getItem(this.STORAGE_KEY);
|
||||
if (storedState) {
|
||||
return JSON.parse(storedState);
|
||||
}
|
||||
constructor() {
|
||||
const initialState = this.loadAppState();
|
||||
this.appState.set({...initialState});
|
||||
this.document.documentElement.classList.add('p-dark');
|
||||
effect(
|
||||
() => {
|
||||
const state = this.appState();
|
||||
if (!this.initialized || !state) {
|
||||
this.initialized = true;
|
||||
return;
|
||||
}
|
||||
return {
|
||||
preset: 'Aura',
|
||||
primary: 'green',
|
||||
surface: 'viva',
|
||||
};
|
||||
}
|
||||
this.saveAppState(state);
|
||||
},
|
||||
{allowSignalWrites: true}
|
||||
);
|
||||
}
|
||||
|
||||
private saveAppState(state: AppState): void {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(state));
|
||||
}
|
||||
private loadAppState(): AppState {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
const storedState = localStorage.getItem(this.STORAGE_KEY);
|
||||
if (storedState) {
|
||||
return JSON.parse(storedState);
|
||||
}
|
||||
}
|
||||
return {
|
||||
preset: 'Aura',
|
||||
primary: 'green',
|
||||
surface: 'viva',
|
||||
};
|
||||
}
|
||||
|
||||
private saveAppState(state: AppState): void {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(state));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user