From d448bf868134d4caf70313213f79f64729c62032 Mon Sep 17 00:00:00 2001 From: xmlking Date: Mon, 19 Nov 2018 14:23:12 -0800 Subject: [PATCH] fix(ThemePicker): ThemePicker now using PreferenceState for storage --- libs/core/src/lib/state/app.state.ts | 4 +- libs/core/src/lib/state/preference.state.ts | 13 ++- .../containers/settings/settings.component.ts | 2 +- .../src/lib/theme-picker.component.html | 8 +- .../src/lib/theme-picker.component.ts | 80 +++++++------------ 5 files changed, 45 insertions(+), 62 deletions(-) diff --git a/libs/core/src/lib/state/app.state.ts b/libs/core/src/lib/state/app.state.ts index fe2923119..3457ec9f3 100644 --- a/libs/core/src/lib/state/app.state.ts +++ b/libs/core/src/lib/state/app.state.ts @@ -70,14 +70,14 @@ export class AppState { } @Action(Online) - enableNotifications({ patchState }: StateContext) { + makeOnline({ patchState }: StateContext) { patchState({ online: true, }); } @Action(Offline) - disableNotifications({ patchState }: StateContext) { + makeOffline({ patchState }: StateContext) { patchState({ online: false, }); diff --git a/libs/core/src/lib/state/preference.state.ts b/libs/core/src/lib/state/preference.state.ts index 4bf4bb717..2918b510a 100644 --- a/libs/core/src/lib/state/preference.state.ts +++ b/libs/core/src/lib/state/preference.state.ts @@ -1,4 +1,4 @@ -import { Action, State, StateContext, Store } from '@ngxs/store'; +import { Action, Selector, State, StateContext, Store } from '@ngxs/store'; export type Language = 'en' | 'es' | 'de' | 'fr' | 'cn'; @@ -74,7 +74,7 @@ export class DisableNotifications { export interface PreferenceStateModel { language: Language; - theme: ThemeName; + activeThemeName: ThemeName; enableNotifications: boolean; } @@ -82,13 +82,18 @@ export interface PreferenceStateModel { name: 'preference', defaults: { language: 'en', - theme: ThemeName.PINK_BLUEGREY, + activeThemeName: ThemeName.INDIGO_PINK, enableNotifications: false, }, }) export class PreferenceState { constructor(private store: Store) {} + @Selector() + static activeThemeName(state: PreferenceStateModel) { + return state.activeThemeName; + } + @Action(ChangeLanguage) changeLanguage({ patchState }: StateContext, { payload }: ChangeLanguage) { patchState({ @@ -99,7 +104,7 @@ export class PreferenceState { @Action(ChangeTheme) changeTheme({ patchState }: StateContext, { payload }: ChangeTheme) { patchState({ - theme: payload, + activeThemeName: payload, }); } diff --git a/libs/dashboard/src/lib/containers/settings/settings.component.ts b/libs/dashboard/src/lib/containers/settings/settings.component.ts index 3668c383d..b72f3dbc5 100644 --- a/libs/dashboard/src/lib/containers/settings/settings.component.ts +++ b/libs/dashboard/src/lib/containers/settings/settings.component.ts @@ -67,7 +67,7 @@ export class SettingsComponent implements OnInit, OnDestroy { buildForm() { this.settingsForm = this.fb.group({ selectedLanguage: [this.preferences.language], - selectedTheme: [this.preferences.theme], + selectedTheme: [this.preferences.activeThemeName], enableNotifications: this.preferences.enableNotifications, }); } diff --git a/libs/theme-picker/src/lib/theme-picker.component.html b/libs/theme-picker/src/lib/theme-picker.component.html index c37017bda..e26ecdc33 100644 --- a/libs/theme-picker/src/lib/theme-picker.component.html +++ b/libs/theme-picker/src/lib/theme-picker.component.html @@ -5,11 +5,11 @@ - -
+ +
- check_circle -
+ check_circle +
diff --git a/libs/theme-picker/src/lib/theme-picker.component.ts b/libs/theme-picker/src/lib/theme-picker.component.ts index c2859d6e3..425c5f09f 100644 --- a/libs/theme-picker/src/lib/theme-picker.component.ts +++ b/libs/theme-picker/src/lib/theme-picker.component.ts @@ -1,6 +1,8 @@ -import { Component, ViewEncapsulation, ChangeDetectionStrategy, HostBinding } from '@angular/core'; +import { Component, ViewEncapsulation, ChangeDetectionStrategy, HostBinding, OnInit, OnDestroy } from '@angular/core'; import { StyleManagerService } from './style-manager.service'; -import { ThemeStorageService, DocsSiteTheme } from './theme-storage.service'; +import { Select, Store } from '@ngxs/store'; +import { themes, PreferenceState, ThemeName, ChangeTheme } from '@ngx-starter-kit/core'; +import { untilDestroy } from '@ngx-starter-kit/ngx-utils'; @Component({ selector: 'theme-picker', @@ -10,61 +12,37 @@ import { ThemeStorageService, DocsSiteTheme } from './theme-storage.service'; encapsulation: ViewEncapsulation.None, // host: { 'aria-hidden': 'true' } }) -export class ThemePickerComponent { - @HostBinding('attr.aria-hidden') - ariaHidden = true; - currentTheme; +export class ThemePickerComponent implements OnInit, OnDestroy { + @HostBinding('attr.aria-hidden') ariaHidden = true; + allThemes = themes; + @Select(PreferenceState.activeThemeName) activeThemeName$; + currentTheme: ThemeName; - themes = [ - { - primary: '#673AB7', - accent: '#FFC107', - href: 'deeppurple-amber.css', - isDark: false, - }, - { - primary: '#3F51B5', - accent: '#E91E63', - href: 'indigo-pink.css', - isDark: false, - isDefault: true, - }, - { - primary: '#E91E63', - accent: '#607D8B', - href: 'pink-bluegrey.css', - isDark: true, - }, - { - primary: '#9C27B0', - accent: '#4CAF50', - href: 'purple-green.css', - isDark: true, - }, - ]; + constructor(private store: Store, public styleManager: StyleManagerService) {} - constructor(public styleManager: StyleManagerService, private _themeStorage: ThemeStorageService) { - const currentTheme = this._themeStorage.getStoredTheme(); - if (currentTheme) { - this.installTheme(currentTheme); - } + changeTheme(themeName: ThemeName) { + this.store.dispatch(new ChangeTheme(themeName)); } - installTheme(theme: DocsSiteTheme) { - this.currentTheme = this._getCurrentThemeFromHref(theme.href); + ngOnInit(): void { + // this.installTheme(this.store.selectSnapshot(PreferenceState.activeThemeName)); + this.activeThemeName$.pipe(untilDestroy(this)).subscribe(themeName => { + this.installTheme(themeName); + }); + } - if (theme.isDefault) { - this.styleManager.removeStyle('theme'); - } else { - this.styleManager.setStyle('theme', `assets/themes/${theme.href}`); - } + ngOnDestroy(): void {} - if (this.currentTheme) { - this._themeStorage.storeTheme(this.currentTheme); + installTheme(themeName: ThemeName) { + console.log(`installing ${themeName}`); + if (themeName) { + this.currentTheme = themeName; + const theme = this.allThemes.get(themeName); + if (theme.isDefault) { + this.styleManager.removeStyle('theme'); + } else { + this.styleManager.setStyle('theme', `assets/themes/${theme.href}`); + } } } - - private _getCurrentThemeFromHref(href: string): DocsSiteTheme { - return this.themes.find(theme => theme.href === href); - } }