Fixes preview generation. Adds notification service

parent 3984ae21
Pipeline #23446 passed with stages
in 12 minutes and 29 seconds
......@@ -134,7 +134,6 @@ export class HomeComponent implements OnInit, OnDestroy {
public ngOnInit(): void {
if (isPlatformServer(this.platformId)) {
console.log('homecomponent ngoninit - isserver');
return;
}
......@@ -169,7 +168,7 @@ export class HomeComponent implements OnInit, OnDestroy {
this._routerSubscription = this.route.params.subscribe(params => {
this._subscriptions.push(this.storageService.stateNotifier.subscribe(async val => {
if (val === DbState.Initialized) {
if ([DbState.Initialized, DbState.Revalidate].includes(val)) {
if (!Object.keys(params).length || !params.themeId || !params.languageId) {
const theme = this.storageService.read(DbTable.Config, StorageKey.DefaultTheme).toPromise();
......
......@@ -3,7 +3,7 @@ import { AfterViewInit, Component, Inject, OnInit, PLATFORM_ID } from '@angular/
import { ActivatedRoute, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router';
import { SwUpdate } from '@angular/service-worker';
import { TranslateService } from '@ngx-translate/core';
import { DeprecatedDb, DeprecatedKeys, StorageKey } from '../../../lib/enums/enums';
import { DeprecatedDb, DeprecatedKeys } from '../../../lib/enums/enums';
import { INamedType } from '../../../lib/interfaces/interfaces';
// tslint:disable-next-line:max-line-length
import { QuizManagerDetailsOverviewComponent } from '../../quiz/quiz-manager/quiz-manager-details/quiz-manager-details-overview/quiz-manager-details-overview.component';
......@@ -12,6 +12,7 @@ import { ConnectionService } from '../../service/connection/connection.service';
import { FooterBarService } from '../../service/footer-bar/footer-bar.service';
import { HeaderLabelService } from '../../service/header-label/header-label.service';
import { I18nService } from '../../service/i18n/i18n.service';
import { NotificationService } from '../../service/notification/notification.service';
import { StorageService } from '../../service/storage/storage.service';
import { ThemesService } from '../../service/themes/themes.service';
import { TrackingService } from '../../service/tracking/tracking.service';
......@@ -62,35 +63,8 @@ export class RootComponent implements OnInit, AfterViewInit {
private storageService: StorageService,
private userService: UserService,
private swUpdate: SwUpdate,
) {
/* Reload the page if the fetch of production chunks failed
* https://stackoverflow.com/a/49805926
*/
// Keep the original error handler
const oldHandler = this.router.errorHandler;
// Replace route error handler
this.router.errorHandler = (err: any) => {
// Check if there is an error loading the chunk
console.error('error while loading route', err);
if (err.originalStack && err.originalStack.indexOf('Error: Loading chunk') >= 0) {
// Check if is the first time the error happend
console.error('loading chunk failed');
if (sessionStorage.getItem(StorageKey.ChunkError) !== err.originalStack) {
// Save the last error to avoid an infinite reload loop if the chunk really does not exists after reload
sessionStorage.setItem(StorageKey.ChunkError, err.originalStack);
console.error('no previous reload found, so forcing reload now');
location.reload(true);
} else {
// The chunk really does not exists after reload
console.error('We really don\'t find the chunk...');
}
}
// Run original handler
oldHandler(err);
};
}
private notificationService: NotificationService,
) {}
public ngOnInit(): void {
if (isPlatformBrowser(this.platformId)) {
......@@ -115,16 +89,13 @@ export class RootComponent implements OnInit, AfterViewInit {
}
});
console.log('sw isenabled', this.swUpdate.isEnabled);
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe((event) => {
console.log('service worker updated');
console.log('service worker update available');
console.log('current version is', event.current);
console.log('available version is', event.available);
console.log('event type is', event.type);
if (window && window.confirm('update?')) {
this.swUpdate.activateUpdate().then(() => document.location.reload());
}
this.swUpdate.activateUpdate().then(() => document.location.reload());
});
this.swUpdate.activated.subscribe(event => {
console.log('previous version was', event.previous);
......
import { TestBed } from '@angular/core/testing';
import { NotificationService } from './notification.service';
describe('NotificationService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: NotificationService = TestBed.get(NotificationService);
expect(service).toBeTruthy();
});
});
import { isPlatformServer } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class NotificationService {
private reg: ServiceWorkerRegistration;
private readonly options: NotificationOptions = {
icon: '/assets/images/theme/theme-Material/logo_s64x64.png',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1,
},
requireInteraction: true,
actions: [
{
action: 'close',
title: 'Close notification',
icon: 'images/xmark.png',
},
],
};
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
}
public async sendNotification(title: string, options?: NotificationOptions): Promise<void> {
if (isPlatformServer(this.platformId)) {
return;
}
if (!this.reg) {
this.reg = await navigator.serviceWorker.ready;
}
if (!this.reg || !title) {
return;
}
// noinspection TypeScriptUnresolvedFunction
const notificationPermission = await Notification.requestPermission();
if (notificationPermission === 'granted') {
return this.reg.showNotification(title, Object.assign({}, this.options, options));
} else {
console.error('cannot send notification', notificationPermission);
}
}
}
......@@ -60,12 +60,9 @@ export class ThemesService {
const themeDataset = document.getElementsByTagName('html').item(0).dataset['theme'];
if (!document.getElementById('link-manifest') && themeDataset === usedTheme) {
console.log('Reloading link nodes because no link node was found', document.getElementById('link-manifest'), themeDataset, usedTheme,
this.currentTheme);
this.reloadLinkNodes(usedTheme);
}
if (themeDataset !== usedTheme) {
console.log('Reloading link nodes because theme changed', themeDataset, usedTheme, this.currentTheme);
this._currentTheme = usedTheme;
document.getElementsByTagName('html').item(0).dataset['theme'] = usedTheme;
this.reloadLinkNodes(usedTheme);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment