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