mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-16 15:40:11 +01:00
fix(theme): save theme in local storage after theme changing
This commit is contained in:
parent
05cb3b3ffd
commit
5eef948132
3 changed files with 24 additions and 16 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import {Injectable, OnDestroy} from '@angular/core';
|
import {Injectable, OnDestroy} from '@angular/core';
|
||||||
import {Observable} from 'rxjs';
|
import {Observable} from 'rxjs';
|
||||||
import {takeWhile} from 'rxjs/operators';
|
import {takeWhile} from 'rxjs/operators';
|
||||||
|
import {environment} from '../../../environments/environment';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CurrentThemeService implements OnDestroy {
|
export class CurrentThemeService implements OnDestroy {
|
||||||
|
|
@ -10,6 +11,22 @@ export class CurrentThemeService implements OnDestroy {
|
||||||
subscriber.next(localStorage.theme);
|
subscriber.next(localStorage.theme);
|
||||||
}).pipe(takeWhile(() => this.alive));
|
}).pipe(takeWhile(() => this.alive));
|
||||||
|
|
||||||
|
setCurrentTheme(themeName: string): void {
|
||||||
|
const currentTheme = {
|
||||||
|
themeName: themeName,
|
||||||
|
expires_in: this.calculateExpiration(environment.currentThemeLife),
|
||||||
|
};
|
||||||
|
|
||||||
|
localStorage.setItem('theme', JSON.stringify(currentTheme));
|
||||||
|
}
|
||||||
|
|
||||||
|
calculateExpiration(iat: number): number {
|
||||||
|
const currentDate = new Date().getTime();
|
||||||
|
const timestamp = iat || Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
return Math.floor(timestamp + currentDate);
|
||||||
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.alive = false;
|
this.alive = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import { AnalyticsService, LayoutService } from '../../../@core/utils';
|
||||||
import { map, takeUntil } from 'rxjs/operators';
|
import { map, takeUntil } from 'rxjs/operators';
|
||||||
import { Subject, Observable } from 'rxjs';
|
import { Subject, Observable } from 'rxjs';
|
||||||
import { RippleService } from '../../../@core/utils/ripple.service';
|
import { RippleService } from '../../../@core/utils/ripple.service';
|
||||||
|
import {CurrentThemeService} from '../../../@core/utils/theme.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ngx-header',
|
selector: 'ngx-header',
|
||||||
|
|
@ -59,6 +60,7 @@ export class HeaderComponent implements OnInit, OnDestroy {
|
||||||
private breakpointService: NbMediaBreakpointsService,
|
private breakpointService: NbMediaBreakpointsService,
|
||||||
private rippleService: RippleService,
|
private rippleService: RippleService,
|
||||||
private analytics: AnalyticsService,
|
private analytics: AnalyticsService,
|
||||||
|
private currentThemeService: CurrentThemeService,
|
||||||
) {
|
) {
|
||||||
this.materialTheme$ = this.themeService.onThemeChange()
|
this.materialTheme$ = this.themeService.onThemeChange()
|
||||||
.pipe(map(theme => {
|
.pipe(map(theme => {
|
||||||
|
|
@ -99,6 +101,7 @@ export class HeaderComponent implements OnInit, OnDestroy {
|
||||||
}
|
}
|
||||||
|
|
||||||
changeTheme(themeName: string) {
|
changeTheme(themeName: string) {
|
||||||
|
this.currentThemeService.setCurrentTheme(themeName);
|
||||||
this.themeService.changeTheme(themeName);
|
this.themeService.changeTheme(themeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import {Component, OnDestroy} from '@angular/core';
|
||||||
import {NbMediaBreakpoint, NbThemeService} from '@nebular/theme';
|
import {NbMediaBreakpoint, NbThemeService} from '@nebular/theme';
|
||||||
import {Router} from '@angular/router';
|
import {Router} from '@angular/router';
|
||||||
import {AnalyticsService} from '../@core/utils';
|
import {AnalyticsService} from '../@core/utils';
|
||||||
import { environment } from '../../environments/environment';
|
import {CurrentThemeService} from '../@core/utils/theme.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ngx-starter',
|
selector: 'ngx-starter',
|
||||||
|
|
@ -41,18 +41,13 @@ export class NgxStarterComponent implements OnDestroy {
|
||||||
];
|
];
|
||||||
|
|
||||||
constructor(private router: Router,
|
constructor(private router: Router,
|
||||||
protected themeService: NbThemeService,
|
private themeService: NbThemeService,
|
||||||
|
private currentThemeService: CurrentThemeService,
|
||||||
private analytics: AnalyticsService,
|
private analytics: AnalyticsService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
navigate(themeName: string) {
|
navigate(themeName: string) {
|
||||||
const currentTheme = {
|
this.currentThemeService.setCurrentTheme(themeName);
|
||||||
themeName: themeName,
|
|
||||||
expires_in: this.calculateExpiration(environment.currentThemeLife),
|
|
||||||
};
|
|
||||||
|
|
||||||
localStorage.setItem('theme', JSON.stringify(currentTheme));
|
|
||||||
|
|
||||||
this.themeService.changeTheme(themeName);
|
this.themeService.changeTheme(themeName);
|
||||||
this.router.navigate(['/pages/dashboard'], {queryParams: {theme: themeName}});
|
this.router.navigate(['/pages/dashboard'], {queryParams: {theme: themeName}});
|
||||||
}
|
}
|
||||||
|
|
@ -63,11 +58,4 @@ export class NgxStarterComponent implements OnDestroy {
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
calculateExpiration(iat: number): number {
|
|
||||||
const currentDate = new Date().getTime();
|
|
||||||
const timestamp = iat || Math.floor(Date.now() / 1000);
|
|
||||||
|
|
||||||
return Math.floor(timestamp + currentDate);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue