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