]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
CommitLineData
79bd2632 1import { Component, OnDestroy, OnInit } from '@angular/core'
0626e7af
C
2import { ActivatedRoute } from '@angular/router'
3import { AccountService } from '@app/shared/account/account.service'
4import { Account } from '@app/shared/account/account.model'
79bd2632
C
5import { RestExtractor, UserService } from '@app/shared'
6import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
734a5ceb 7import { Subscription } from 'rxjs'
f8b2c1b4 8import { AuthService, Notifier, RedirectService } from '@app/core'
79bd2632 9import { User, UserRight } from '../../../../shared'
0626e7af
C
10
11@Component({
170726f5
C
12 templateUrl: './accounts.component.html',
13 styleUrls: [ './accounts.component.scss' ]
0626e7af 14})
3baf9be2 15export class AccountsComponent implements OnInit, OnDestroy {
6b738c7a 16 account: Account
79bd2632 17 user: User
0626e7af 18
734a5ceb
C
19 private routeSub: Subscription
20
0626e7af
C
21 constructor (
22 private route: ActivatedRoute,
79bd2632 23 private userService: UserService,
a51bad1a 24 private accountService: AccountService,
f8b2c1b4 25 private notifier: Notifier,
79bd2632
C
26 private restExtractor: RestExtractor,
27 private redirectService: RedirectService,
307902e2 28 private authService: AuthService
0626e7af
C
29 ) {}
30
31 ngOnInit () {
734a5ceb
C
32 this.routeSub = this.route.params
33 .pipe(
34 map(params => params[ 'accountId' ]),
35 distinctUntilChanged(),
36 switchMap(accountId => this.accountService.getAccount(accountId)),
79bd2632 37 tap(account => this.getUserIfNeeded(account)),
734a5ceb
C
38 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
39 )
79bd2632
C
40 .subscribe(
41 account => this.account = account,
42
f8b2c1b4 43 err => this.notifier.error(err.message)
79bd2632 44 )
734a5ceb 45 }
0626e7af 46
734a5ceb
C
47 ngOnDestroy () {
48 if (this.routeSub) this.routeSub.unsubscribe()
0626e7af 49 }
79bd2632
C
50
51 onUserChanged () {
52 this.getUserIfNeeded(this.account)
53 }
54
55 onUserDeleted () {
56 this.redirectService.redirectToHomepage()
57 }
58
59 private getUserIfNeeded (account: Account) {
60 if (!account.userId) return
61 if (!this.authService.isLoggedIn()) return
62
63 const user = this.authService.getUser()
64 if (user.hasRight(UserRight.MANAGE_USERS)) {
65 this.userService.getUser(account.userId)
66 .subscribe(
67 user => this.user = user,
68
f8b2c1b4 69 err => this.notifier.error(err.message)
79bd2632
C
70 )
71 }
72 }
0626e7af 73}