]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / redirect.service.ts
CommitLineData
901637bb 1import { Injectable } from '@angular/core'
dae5ca24 2import { NavigationEnd, Router } from '@angular/router'
901637bb
C
3import { ServerService } from '../server'
4
5@Injectable()
6export class RedirectService {
7 // Default route could change according to the instance configuration
8 static INIT_DEFAULT_ROUTE = '/videos/trending'
9 static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
10
dae5ca24
C
11 private previousUrl: string
12 private currentUrl: string
13
901637bb
C
14 constructor (
15 private router: Router,
16 private serverService: ServerService
17 ) {
18 // The config is first loaded from the cache so try to get the default route
ba430d75
C
19 const tmpConfig = this.serverService.getTmpConfig()
20 if (tmpConfig && tmpConfig.instance && tmpConfig.instance.defaultClientRoute) {
21 RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute
901637bb
C
22 }
23
dae5ca24 24 // Load default route
ba430d75
C
25 this.serverService.getConfig()
26 .subscribe(config => {
27 const defaultRouteConfig = config.instance.defaultClientRoute
901637bb 28
a51bad1a
C
29 if (defaultRouteConfig) {
30 RedirectService.DEFAULT_ROUTE = defaultRouteConfig
31 }
32 })
dae5ca24
C
33
34 // Track previous url
35 this.currentUrl = this.router.url
36 router.events.subscribe(event => {
37 if (event instanceof NavigationEnd) {
38 this.previousUrl = this.currentUrl
39 this.currentUrl = event.url
40 }
41 })
42 }
43
44 redirectToPreviousRoute () {
b247a132 45 const exceptions = [
f003ef1b
C
46 '/verify-account',
47 '/reset-password'
b247a132
C
48 ]
49
50 if (this.previousUrl) {
51 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
52 if (!isException) return this.router.navigateByUrl(this.previousUrl)
53 }
dae5ca24
C
54
55 return this.redirectToHomepage()
901637bb
C
56 }
57
7cf26f43 58 redirectToHomepage (skipLocationChange = false) {
901637bb
C
59 console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
60
7cf26f43 61 this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
a51bad1a
C
62 .catch(() => {
63 console.error(
64 'Cannot navigate to %s, resetting default route to %s.',
65 RedirectService.DEFAULT_ROUTE,
66 RedirectService.INIT_DEFAULT_ROUTE
67 )
901637bb 68
a51bad1a 69 RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
7cf26f43 70 return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
a51bad1a 71 })
901637bb
C
72
73 }
901637bb 74}