fix(docs): guard before redirecting

This commit is contained in:
Alex 2020-04-01 16:23:58 +03:00 committed by Sergey Andrievskiy
parent 413cbdbd01
commit ec3ef4e879
5 changed files with 21 additions and 10 deletions

View file

@ -15,12 +15,16 @@ export class ThemeGuard implements CanActivate {
): Observable<boolean> | Promise<boolean> | boolean {
return this.currentThemeService.currentTheme$.pipe(
map(theme => {
const currentThemeExpiration = JSON.parse(theme).expires_in;
const currentDate = new Date().getTime();
if (!theme || currentDate > currentThemeExpiration) {
if (!theme || this.hasExpired(theme)) {
this.router.navigate(['themes']);
}
return true;
}));
}
private hasExpired(theme): boolean {
const currentThemeExpiration = JSON.parse(theme).expires_in;
const currentDate = new Date().getTime();
return currentDate > currentThemeExpiration;
}
}

View file

@ -21,7 +21,7 @@ export class CurrentThemeService implements OnDestroy {
}
getCurrentTheme(): string {
return JSON.parse(localStorage.theme).themeName;
return localStorage.theme ? JSON.parse(localStorage.theme).themeName : 'default';
}
calculateExpiration(iat: number): number {

View file

@ -62,15 +62,14 @@ export class HeaderComponent implements OnInit, OnDestroy {
private analytics: AnalyticsService,
private currentThemeService: CurrentThemeService,
) {
}
ngOnInit() {
this.materialTheme$ = new Observable(subscriber => {
const themeName: string = this.currentThemeService.getCurrentTheme();
subscriber.next(themeName.startsWith('material'));
});
}
ngOnInit() {
this.currentTheme = this.themeService.currentTheme;
this.userService.getUsers()
@ -104,6 +103,10 @@ export class HeaderComponent implements OnInit, OnDestroy {
changeTheme(themeName: string) {
this.currentThemeService.setCurrentTheme(themeName);
this.themeService.changeTheme(themeName);
this.materialTheme$ = new Observable(subscriber => {
subscriber.next(this.currentThemeService.getCurrentTheme().startsWith('material'));
});
}
toggleSidebar(): boolean {

View file

@ -53,8 +53,8 @@ export const routes: Routes = [
},
],
},
{ path: '', redirectTo: 'themes', pathMatch: 'full' },
{ path: '**', redirectTo: 'themes' },
{ path: '', redirectTo: 'pages/dashboard', pathMatch: 'full' },
{ path: '**', redirectTo: 'pages/dashboard' },
];
const config: ExtraOptions = {

View file

@ -11,6 +11,7 @@ import { NbThemeService } from '@nebular/theme';
import { AnalyticsService } from './@core/utils/analytics.service';
import { AbService } from './@core/utils/ab.service';
import { SeoService } from './@core/utils/seo.service';
import { CurrentThemeService } from './@core/utils/theme.service';
@Component({
selector: 'ngx-app',
@ -24,7 +25,8 @@ export class AppComponent implements OnInit {
private seoService: SeoService,
private activatedRoute: ActivatedRoute,
private abService: AbService,
private themeService: NbThemeService) {
private themeService: NbThemeService,
private currentThemeService: CurrentThemeService) {
this.themeService.onThemeChange()
.subscribe((theme: any) => {
@ -35,6 +37,8 @@ export class AppComponent implements OnInit {
.subscribe((params: any) => {
if (params.theme && this.themes.includes(params.theme)) {
this.themeService.changeTheme(params.theme);
} else {
this.themeService.changeTheme(this.currentThemeService.getCurrentTheme());
}
});
}