PWA final fix

This commit is contained in:
2023-06-24 15:35:11 +02:00
parent b858dd3e02
commit 324084fb1f
5 changed files with 31 additions and 55 deletions

View File

@@ -1,48 +1,42 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router'; import { RouterOutlet } from '@angular/router';
import { SwUpdate } from '@angular/service-worker'; import { SwUpdate, VersionReadyEvent } from '@angular/service-worker';
import { NotificationsService } from './services/notifications.service';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { NotificationsService } from './services/notifications.service';
import { filter } from 'rxjs';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
templateUrl: './app.component.html', templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'], styleUrls: ['./app.component.scss'],
standalone: true, standalone: true,
imports: [RouterOutlet] imports: [RouterOutlet]
}) })
export class AppComponent { export class AppComponent {
constructor( constructor(
// private readonly _swUpdate: SwUpdate, private readonly _swUpdate: SwUpdate,
//private _notifications: NotificationsService private _notifications: NotificationsService
) { ) {
/*
console.log('Start versionUpdates subscribe', this._swUpdate.isEnabled, environment.production);
if (this._swUpdate.isEnabled && environment.production) { if (this._swUpdate.isEnabled && environment.production) {
this._swUpdate.versionUpdates.subscribe((evt) => { this._swUpdate.versionUpdates
console.log('versionUpdates', evt); .pipe(filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'))
switch (evt.type) { .subscribe((evt) => {
case 'VERSION_READY': this._notifications.add({
this._swUpdate.activateUpdate().then(() => { text: "New version available. DiunaBI will restart in 10 seconds.",
duration: 10000,
dismiss: () => {
window.location.reload()
},
btn: 'Cancel',
action: () => {
this._notifications.add({ this._notifications.add({
text: "New version available. DiunaBI will restart in 10 seconds.", text: "Restart canceled.",
duration: 10000, btn: "Hide"
dismiss: () => { });
window.location.reload() }
}, })
btn: 'Cancel', });
action: () => {
this._notifications.add({
text: "Restart canceled.",
btn: "Hide"
});
}
})
});
break;
}
});
} }
*/
} }
} }

View File

@@ -19,15 +19,10 @@ export class AuthInterceptor implements HttpInterceptor {
) { } ) { }
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
console.log('Welcome to interceptor:', request.url);
console.log('IsAuth?', request.url.includes('/auth/apiToken'));
if (!request.url.includes('/auth/apiToken')) { if (!request.url.includes('/auth/apiToken')) {
console.log(this.auth$.expirationTime.format(), moment.utc().format());
if (this.auth$.expirationTime.isBefore(moment.utc())) { if (this.auth$.expirationTime.isBefore(moment.utc())) {
console.log('Need to refresh token');
return this.auth$.getAPITokenObservable().pipe( return this.auth$.getAPITokenObservable().pipe(
mergeMap(() => { mergeMap(() => {
console.log('New token is ready');
return next.handle(request.clone({ return next.handle(request.clone({
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`), headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
})); }));
@@ -43,7 +38,6 @@ export class AuthInterceptor implements HttpInterceptor {
}) })
); );
} else { } else {
console.log('TOken is fine');
return next.handle(request.clone({ return next.handle(request.clone({
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`), headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
})); }));

View File

@@ -34,8 +34,7 @@
Layers Layers
</a> </a>
<mat-divider></mat-divider> <mat-divider></mat-divider>
<a mat-list-item (click)="reloadApp()"> <a mat-list-item>
<mat-icon class="menu-icon">refresh</mat-icon>
{{appVersion}} {{appVersion}}
</a> </a>
</mat-nav-list> </mat-nav-list>

View File

@@ -1,7 +1,6 @@
import { Component, NgZone, ViewChild } from '@angular/core'; import { Component, NgZone, ViewChild } from '@angular/core';
import { MatSidenav, MatSidenavModule } from '@angular/material/sidenav'; import { MatSidenav, MatSidenavModule } from '@angular/material/sidenav';
import { NavigationStart, Router, RouterLink, RouterOutlet } from '@angular/router'; import { NavigationStart, Router, RouterLink, RouterOutlet } from '@angular/router';
import { SwUpdate } from '@angular/service-worker';
import * as moment from 'moment'; import * as moment from 'moment';
import packageInfo from 'package.json'; import packageInfo from 'package.json';
import { delay } from 'rxjs'; import { delay } from 'rxjs';
@@ -9,7 +8,6 @@ import { environment } from 'src/environments/environment';
import { AuthService } from '../auth/auth.service'; import { AuthService } from '../auth/auth.service';
import { DataService } from '../services/data.service'; import { DataService } from '../services/data.service';
import { DeviceService } from '../services/device.service'; import { DeviceService } from '../services/device.service';
import { NotificationsService } from '../services/notifications.service';
import { MatDividerModule } from '@angular/material/divider'; import { MatDividerModule } from '@angular/material/divider';
import { MatListModule } from '@angular/material/list'; import { MatListModule } from '@angular/material/list';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
@@ -38,13 +36,9 @@ export class MainViewComponent {
public data$: DataService, public data$: DataService,
public device$: DeviceService, public device$: DeviceService,
private router$: Router, private router$: Router,
private swUpdate$: SwUpdate,
private ngZone$: NgZone, private ngZone$: NgZone,
public auth$: AuthService, public auth$: AuthService,
) { ) {
this.swUpdate$.versionUpdates.subscribe(() => {
this.reloadApp();
});
this.router$.events.subscribe((event) => { this.router$.events.subscribe((event) => {
if (event instanceof NavigationStart && device$.isMobile()) { if (event instanceof NavigationStart && device$.isMobile()) {
this.snav?.close(); this.snav?.close();
@@ -58,13 +52,10 @@ export class MainViewComponent {
}); });
}); });
} }
reloadApp() {
document.location.reload();
}
logout() { logout() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
google.accounts.id.disableAutoSelect(); google.accounts.id.disableAutoSelect();
this.reloadApp(); window.location.reload();
} }
} }

View File

@@ -41,12 +41,10 @@ bootstrapApplication(AppComponent, {
}, },
provideAnimations(), provideAnimations(),
provideHttpClient(withInterceptorsFromDi()), provideHttpClient(withInterceptorsFromDi()),
/*
provideServiceWorker('ngsw-worker.js', { provideServiceWorker('ngsw-worker.js', {
enabled: !isDevMode(), enabled: !isDevMode(),
registrationStrategy: 'registerWhenStable:30000' registrationStrategy: 'registerWhenStable:30000'
}) })
*/
] ]
}) })
.catch(err => console.error(err)); .catch(err => console.error(err));