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'
13 templateUrl: './accounts.component.html',
14 styleUrls: [ './accounts.component.scss' ]
16 export class AccountsComponent implements OnInit, OnDestroy {
20 private routeSub: Subscription
23 private route: ActivatedRoute,
24 private userService: UserService,
25 private accountService: AccountService,
26 private notifier: Notifier,
27 private restExtractor: RestExtractor,
28 private redirectService: RedirectService,
29 private authService: AuthService,
34 this.routeSub = this.route.params
36 map(params => params[ 'accountId' ]),
37 distinctUntilChanged(),
38 switchMap(accountId => this.accountService.getAccount(accountId)),
39 tap(account => this.getUserIfNeeded(account)),
40 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
43 account => this.account = account,
45 err => this.notifier.error(err.message)
50 if (this.routeSub) this.routeSub.unsubscribe()
54 this.getUserIfNeeded(this.account)
58 this.redirectService.redirectToHomepage()
61 activateCopiedMessage () {
62 this.notifier.success(this.i18n('Username copied'))
65 private getUserIfNeeded (account: Account) {
66 if (!account.userId) return
67 if (!this.authService.isLoggedIn()) return
69 const user = this.authService.getUser()
70 if (user.hasRight(UserRight.MANAGE_USERS)) {
71 this.userService.getUser(account.userId)
73 user => this.user = user,
75 err => this.notifier.error(err.message)