PeerTube/client/src/app/+accounts/accounts.component.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Component, OnDestroy, OnInit } from '@angular/core'
2018-04-24 08:10:54 -05:00
import { ActivatedRoute } from '@angular/router'
import { AccountService } from '@app/shared/account/account.service'
import { Account } from '@app/shared/account/account.model'
import { RestExtractor, UserService } from '@app/shared'
import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
import { Subscription } from 'rxjs'
import { AuthService, Notifier, RedirectService } from '@app/core'
import { User, UserRight } from '../../../../shared'
2019-06-19 08:14:13 -05:00
import { I18n } from '@ngx-translate/i18n-polyfill'
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
2018-04-24 08:10:54 -05:00
@Component({
2018-04-25 09:56:13 -05:00
templateUrl: './accounts.component.html',
styleUrls: [ './accounts.component.scss' ]
2018-04-24 08:10:54 -05:00
})
export class AccountsComponent implements OnInit, OnDestroy {
2018-04-25 03:21:38 -05:00
account: Account
user: User
videoChannels: VideoChannel[]
2018-04-24 08:10:54 -05:00
private routeSub: Subscription
2018-04-24 08:10:54 -05:00
constructor (
private route: ActivatedRoute,
private userService: UserService,
2018-05-31 04:35:01 -05:00
private accountService: AccountService,
private videoChannelService: VideoChannelService,
private notifier: Notifier,
private restExtractor: RestExtractor,
private redirectService: RedirectService,
2019-06-19 08:14:13 -05:00
private authService: AuthService,
private i18n: I18n
2018-04-24 08:10:54 -05:00
) {}
ngOnInit () {
this.routeSub = this.route.params
.pipe(
map(params => params[ 'accountId' ]),
distinctUntilChanged(),
switchMap(accountId => this.accountService.getAccount(accountId)),
tap(account => {
this.account = account
this.getUserIfNeeded(account)
}),
switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
)
.subscribe(
videoChannels => this.videoChannels = videoChannels.data,
err => this.notifier.error(err.message)
)
}
2018-04-24 08:10:54 -05:00
ngOnDestroy () {
if (this.routeSub) this.routeSub.unsubscribe()
2018-04-24 08:10:54 -05:00
}
onUserChanged () {
this.getUserIfNeeded(this.account)
}
onUserDeleted () {
this.redirectService.redirectToHomepage()
}
2019-06-19 08:14:13 -05:00
activateCopiedMessage () {
this.notifier.success(this.i18n('Username copied'))
}
private getUserIfNeeded (account: Account) {
if (!account.userId) return
if (!this.authService.isLoggedIn()) return
const user = this.authService.getUser()
if (user.hasRight(UserRight.MANAGE_USERS)) {
this.userService.getUser(account.userId)
.subscribe(
user => this.user = user,
err => this.notifier.error(err.message)
)
}
}
2018-04-24 08:10:54 -05:00
}