PeerTube/client/src/app/core/routing/redirect.service.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-03-01 13:57:29 +01:00
import { Injectable } from '@angular/core'
import { Router } from '@angular/router'
import { ServerService } from '../server'
@Injectable()
export class RedirectService {
// Default route could change according to the instance configuration
static INIT_DEFAULT_ROUTE = '/videos/trending'
static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
constructor (
private router: Router,
private serverService: ServerService
) {
// The config is first loaded from the cache so try to get the default route
const config = this.serverService.getConfig()
if (config && config.instance && config.instance.defaultClientRoute) {
RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
}
this.serverService.configLoaded
2018-05-31 11:35:01 +02:00
.subscribe(() => {
const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
2018-03-01 13:57:29 +01:00
2018-05-31 11:35:01 +02:00
if (defaultRouteConfig) {
RedirectService.DEFAULT_ROUTE = defaultRouteConfig
}
})
2018-03-01 13:57:29 +01:00
}
redirectToHomepage () {
console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
2018-05-31 18:12:15 +02:00
this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange: true })
2018-05-31 11:35:01 +02:00
.catch(() => {
console.error(
'Cannot navigate to %s, resetting default route to %s.',
RedirectService.DEFAULT_ROUTE,
RedirectService.INIT_DEFAULT_ROUTE
)
2018-03-01 13:57:29 +01:00
2018-05-31 11:35:01 +02:00
RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
2018-05-31 18:12:15 +02:00
return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange: true })
2018-05-31 11:35:01 +02:00
})
2018-03-01 13:57:29 +01:00
}
}