]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+accounts/accounts.component.ts
Refactor my-subscribe-button to support full account subscription
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { AccountService } from '@app/shared/account/account.service'
4 import { Account } from '@app/shared/account/account.model'
5 import { RestExtractor, UserService } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier, RedirectService } from '@app/core'
9 import { User, UserRight } from '../../../../shared'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13
14 @Component({
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
17 })
18 export class AccountsComponent implements OnInit, OnDestroy {
19 account: Account
20 user: User
21 videoChannels: VideoChannel[]
22
23 private routeSub: Subscription
24
25 constructor (
26 private route: ActivatedRoute,
27 private userService: UserService,
28 private accountService: AccountService,
29 private videoChannelService: VideoChannelService,
30 private notifier: Notifier,
31 private restExtractor: RestExtractor,
32 private redirectService: RedirectService,
33 private authService: AuthService,
34 private i18n: I18n
35 ) {}
36
37 ngOnInit () {
38 this.routeSub = this.route.params
39 .pipe(
40 map(params => params[ 'accountId' ]),
41 distinctUntilChanged(),
42 switchMap(accountId => this.accountService.getAccount(accountId)),
43 tap(account => this.getUserIfNeeded(account)),
44 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
45 )
46 .subscribe(
47 account => {
48 this.account = account
49 this.videoChannelService.listAccountVideoChannels(account)
50 .subscribe(videoChannels => this.videoChannels = videoChannels.data)
51 },
52
53 err => this.notifier.error(err.message)
54 )
55 }
56
57 ngOnDestroy () {
58 if (this.routeSub) this.routeSub.unsubscribe()
59 }
60
61 onUserChanged () {
62 this.getUserIfNeeded(this.account)
63 }
64
65 onUserDeleted () {
66 this.redirectService.redirectToHomepage()
67 }
68
69 activateCopiedMessage () {
70 this.notifier.success(this.i18n('Username copied'))
71 }
72
73 private getUserIfNeeded (account: Account) {
74 if (!account.userId) return
75 if (!this.authService.isLoggedIn()) return
76
77 const user = this.authService.getUser()
78 if (user.hasRight(UserRight.MANAGE_USERS)) {
79 this.userService.getUser(account.userId)
80 .subscribe(
81 user => this.user = user,
82
83 err => this.notifier.error(err.message)
84 )
85 }
86 }
87 }