aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/root.component.ts
blob: 5a09e50d1d6cf4870b1d007e06242d9fc08c84f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Component, OnInit } from '@angular/core'
import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators'
import { ActivatedRoute, Router } from '@angular/router'
import { RestExtractor } from '@app/core'
import { ActorService } from '@app/shared/shared-main/account'
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'

@Component({
  selector: 'my-root',
  template: ''
})
export class RootComponent implements OnInit {
  constructor (
    private actorService: ActorService,
    private route: ActivatedRoute,
    private restExtractor: RestExtractor,
    private router: Router
  ) {
  }

  ngOnInit () {
    this.route.params
        .pipe(
          map(params => params[ 'actorName' ]),
          distinctUntilChanged(),
          switchMap(actorName => this.actorService.getActorType(actorName)),
          catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
            HttpStatusCode.BAD_REQUEST_400,
            HttpStatusCode.NOT_FOUND_404
          ]))
        )
        .subscribe(actorType => {
          const actorName = this.route.snapshot.params[ 'actorName' ]

          if (actorType === 'Account') {
            this.router.navigate([ `/a/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
          }

          if (actorType === 'VideoChannel') {
            this.router.navigate([ `/c/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
          }
        })
  }
}