]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Better placement for help tooltip
[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'
ee1d0dfb 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,
ee1d0dfb
C
29 private authService: AuthService,
30 private i18n: I18n
0626e7af
C
31 ) {}
32
33 ngOnInit () {
734a5ceb
C
34 this.routeSub = this.route.params
35 .pipe(
36 map(params => params[ 'accountId' ]),
37 distinctUntilChanged(),
38 switchMap(accountId => this.accountService.getAccount(accountId)),
79bd2632 39 tap(account => this.getUserIfNeeded(account)),
734a5ceb
C
40 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
41 )
79bd2632
C
42 .subscribe(
43 account => this.account = account,
44
f8b2c1b4 45 err => this.notifier.error(err.message)
79bd2632 46 )
734a5ceb 47 }
0626e7af 48
734a5ceb
C
49 ngOnDestroy () {
50 if (this.routeSub) this.routeSub.unsubscribe()
0626e7af 51 }
79bd2632
C
52
53 onUserChanged () {
54 this.getUserIfNeeded(this.account)
55 }
56
57 onUserDeleted () {
58 this.redirectService.redirectToHomepage()
59 }
60
ee1d0dfb
C
61 activateCopiedMessage () {
62 this.notifier.success(this.i18n('Username copied'))
63 }
64
79bd2632
C
65 private getUserIfNeeded (account: Account) {
66 if (!account.userId) return
67 if (!this.authService.isLoggedIn()) return
68
69 const user = this.authService.getUser()
70 if (user.hasRight(UserRight.MANAGE_USERS)) {
71 this.userService.getUser(account.userId)
72 .subscribe(
73 user => this.user = user,
74
f8b2c1b4 75 err => this.notifier.error(err.message)
79bd2632
C
76 )
77 }
78 }
0626e7af 79}