added resolver state service
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { Injectable, signal, inject, computed } from '@angular/core';
|
||||
import { Router, RouterEvent, ActivatedRoute } from '@angular/router';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { ResolveEnd, ResolveStart } from '@angular/router';
|
||||
import {
|
||||
NavigationEnd,
|
||||
NavigationError,
|
||||
NavigationCancel,
|
||||
} from '@angular/router';
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ResolverStateService {
|
||||
private readonly router = inject(Router);
|
||||
|
||||
readonly resolvingUrl = signal<string | null>(null);
|
||||
readonly previousUrl = signal<string | null>(null);
|
||||
|
||||
readonly isResolving = computed(() => this.resolvingUrl() !== null);
|
||||
readonly isResolvingPersonsFromOutside = computed(() => {
|
||||
const from = this.previousUrl();
|
||||
const to = this.resolvingUrl();
|
||||
if (!to?.startsWith('/persons')) return false;
|
||||
return !from?.startsWith('/persons'); // came from outside
|
||||
});
|
||||
|
||||
constructor() {
|
||||
this.router.events.subscribe(event => {
|
||||
if (event instanceof ResolveStart) {
|
||||
this.previousUrl.set(this.router.url); // capture current before it changes
|
||||
this.resolvingUrl.set(event.url);
|
||||
}
|
||||
|
||||
if (
|
||||
event instanceof NavigationEnd ||
|
||||
event instanceof NavigationCancel ||
|
||||
event instanceof NavigationError
|
||||
) {
|
||||
this.resolvingUrl.set(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user