X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Frouting%2Fredirect.service.ts;h=e1db4097b87ff35934529538a2c86487ae22e9a8;hb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;hp=abe044d7306e007dbab9ef61084b345fdff0a53e;hpb=108a66f0dac7586f2f7871c6bb77f73cb924f2b3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/routing/redirect.service.ts b/client/src/app/core/routing/redirect.service.ts index abe044d73..e1db4097b 100644 --- a/client/src/app/core/routing/redirect.service.ts +++ b/client/src/app/core/routing/redirect.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core' -import { Router } from '@angular/router' +import { NavigationEnd, Router } from '@angular/router' import { ServerService } from '../server' @Injectable() @@ -8,6 +8,9 @@ export class RedirectService { static INIT_DEFAULT_ROUTE = '/videos/trending' static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE + private previousUrl: string + private currentUrl: string + constructor ( private router: Router, private serverService: ServerService @@ -18,31 +21,46 @@ export class RedirectService { RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute } + // Load default route this.serverService.configLoaded - .subscribe(() => { - const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute + .subscribe(() => { + const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute + + if (defaultRouteConfig) { + RedirectService.DEFAULT_ROUTE = defaultRouteConfig + } + }) + + // Track previous url + this.currentUrl = this.router.url + router.events.subscribe(event => { + if (event instanceof NavigationEnd) { + this.previousUrl = this.currentUrl + this.currentUrl = event.url + } + }) + } + + redirectToPreviousRoute () { + if (this.previousUrl) return this.router.navigateByUrl(this.previousUrl) - if (defaultRouteConfig) { - RedirectService.DEFAULT_ROUTE = defaultRouteConfig - } - }) + return this.redirectToHomepage() } - redirectToHomepage () { + redirectToHomepage (skipLocationChange = false) { console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE) - this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { replaceUrl: true }) - .catch(() => { - console.error( - 'Cannot navigate to %s, resetting default route to %s.', - RedirectService.DEFAULT_ROUTE, - RedirectService.INIT_DEFAULT_ROUTE - ) + this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange }) + .catch(() => { + console.error( + 'Cannot navigate to %s, resetting default route to %s.', + RedirectService.DEFAULT_ROUTE, + RedirectService.INIT_DEFAULT_ROUTE + ) - RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE - return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { replaceUrl: true }) - }) + RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE + return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange }) + }) } - }