diff --git a/webapp/src/app/shared/services/resolver-state.service.ts b/webapp/src/app/shared/services/resolver-state.service.ts new file mode 100644 index 00000000..89c92405 --- /dev/null +++ b/webapp/src/app/shared/services/resolver-state.service.ts @@ -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(null); + readonly previousUrl = signal(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); + } + }); + } +} \ No newline at end of file