Skip to content
Snippets Groups Projects
Unverified Commit 9d770bb7 authored by florimondmanca's avatar florimondmanca
Browse files

display progressbar on navigation

parent 7da6e00e
No related branches found
No related tags found
3 merge requests!22to be tested,!23Affichage d'une progress bar lors de la navigation,!26Release first-users version to production
#progress {
position: fixed;
top: 0;
left: 0;
width: 100vw;
}
<app-splash *ngIf="!active"></app-splash> <mat-progress-bar id="progress" mode="indeterminate" color="primary" *ngIf="loading"></mat-progress-bar>
<router-outlet></router-outlet> <router-outlet></router-outlet>
...@@ -18,8 +18,7 @@ import { LoaderService } from './core'; ...@@ -18,8 +18,7 @@ import { LoaderService } from './core';
}) })
export class AppComponent implements OnInit, OnDestroy { export class AppComponent implements OnInit, OnDestroy {
loading = true; loading = false;
active = false;
sub = new Subscription(); sub = new Subscription();
constructor( constructor(
...@@ -29,42 +28,17 @@ export class AppComponent implements OnInit, OnDestroy { ...@@ -29,42 +28,17 @@ export class AppComponent implements OnInit, OnDestroy {
private loaderService: LoaderService, private loaderService: LoaderService,
) { } ) { }
// Shows and hides the loading spinner during RouterEvent changes
navigationInterceptor(event: RouterEvent): void {
if (event instanceof NavigationStart) {
this.loading = true
}
if (event instanceof NavigationEnd) {
this.loading = false
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
if (event instanceof NavigationCancel) {
this.loading = false
}
if (event instanceof NavigationError) {
this.loading = false
}
}
ngOnInit() { ngOnInit() {
this.sub.add( this.sub.add(
this.title$().subscribe(title => this.titleService.setTitle(title)) this.title$().subscribe(title => this.titleService.setTitle(title))
); );
this.sub.add( this.sub.add(
this.loaderService.loading().subscribe(loading => { this.loaderService.loading().subscribe(loading => {
this.active = !loading; this.loading = loading;
}) })
); );
} }
private active$(): Observable<boolean> {
return this.router.events.pipe(
filter(event => event instanceof NavigationStart || event instanceof NavigationEnd),
map(event => event instanceof NavigationEnd),
);
}
private title$(): Observable<string> { private title$(): Observable<string> {
return this.router.events.pipe( return this.router.events.pipe(
// When a navigation finishes // When a navigation finishes
......
...@@ -24,7 +24,6 @@ import { SignupPageComponent } from './signup-page/signup-page.component'; ...@@ -24,7 +24,6 @@ import { SignupPageComponent } from './signup-page/signup-page.component';
// Services // Services
import { StudentService } from './signup-page/student.service'; import { StudentService } from './signup-page/student.service';
import { MessageService } from './core'; import { MessageService } from './core';
import { SplashComponent } from './splash/splash.component';
registerLocaleData(localeFR); registerLocaleData(localeFR);
...@@ -32,7 +31,6 @@ registerLocaleData(localeFR); ...@@ -32,7 +31,6 @@ registerLocaleData(localeFR);
declarations: [ declarations: [
AppComponent, AppComponent,
SignupPageComponent, SignupPageComponent,
SplashComponent,
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
......
...@@ -11,9 +11,9 @@ export class LoaderInterceptor implements HttpInterceptor { ...@@ -11,9 +11,9 @@ export class LoaderInterceptor implements HttpInterceptor {
constructor(private loaderService: LoaderService) { } constructor(private loaderService: LoaderService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.loaderService.loading$.next(true); // this.loaderService.loading$.next(true);
return next.handle(request).pipe( return next.handle(request).pipe(
tap(() => this.loaderService.loading$.next(false)), // tap(() => this.loaderService.loading$.next(false)),
); );
} }
......
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, NavigationCancel } from '@angular/router';
import { Observable, BehaviorSubject } from 'rxjs'; import { Observable, BehaviorSubject } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators'; import { map, debounceTime, tap, filter } from 'rxjs/operators';
@Injectable() @Injectable()
export class LoaderService { export class LoaderService {
public loading$: BehaviorSubject<boolean> = new BehaviorSubject(false); constructor(private router: Router) {}
public loading(): Observable<boolean> { public loading(): Observable<boolean> {
return this.loading$.asObservable().pipe( return this.router.events.pipe(
// Prevent having multiple false's or multiple true's filter(e => e instanceof NavigationStart || e instanceof NavigationEnd || e instanceof NavigationCancel),
distinctUntilChanged(), debounceTime(100), // don't mark fast navigation changes as navigating
map((e) => e instanceof NavigationStart),
); );
} }
......
<div id="splash">
<mat-progress-bar mode="indeterminate" color="primary"></mat-progress-bar>
</div>
#splash {
z-index: 9999999;
position: fixed;
top: 0;
left: 0;
width: 100vw;
i {
background: white;
border-radius: 50%;
width: 1.3em;
height: 1.3em;
display: flex;
justify-content: center;
align-items: center;
}
}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-splash',
templateUrl: './splash.component.html',
styleUrls: ['./splash.component.scss']
})
export class SplashComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment