aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+accounts/accounts.component.ts
blob: af0451e910099d3627c22b9329bc24bf1042f953 (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
import { Component, OnInit, OnDestroy } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { AccountService } from '@app/shared/account/account.service'
import { Account } from '@app/shared/account/account.model'
import { RestExtractor } from '@app/shared'
import { catchError, switchMap, distinctUntilChanged, map } from 'rxjs/operators'
import { Subscription } from 'rxjs'

@Component({
  templateUrl: './accounts.component.html',
  styleUrls: [ './accounts.component.scss' ]
})
export class AccountsComponent implements OnInit, OnDestroy {
  account: Account

  private routeSub: Subscription

  constructor (
    private route: ActivatedRoute,
    private accountService: AccountService,
    private restExtractor: RestExtractor
  ) {}

  ngOnInit () {
    this.routeSub = this.route.params
      .pipe(
        map(params => params[ 'accountId' ]),
        distinctUntilChanged(),
        switchMap(accountId => this.accountService.getAccount(accountId)),
        catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
      )
      .subscribe(account => this.account = account)
  }

  ngOnDestroy () {
    if (this.routeSub) this.routeSub.unsubscribe()
  }
}