]>
Commit | Line | Data |
---|---|---|
79bd2632 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0626e7af C |
2 | import { ActivatedRoute } from '@angular/router' |
3 | import { AccountService } from '@app/shared/account/account.service' | |
4 | import { Account } from '@app/shared/account/account.model' | |
79bd2632 C |
5 | import { RestExtractor, UserService } from '@app/shared' |
6 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' | |
734a5ceb | 7 | import { Subscription } from 'rxjs' |
f8b2c1b4 | 8 | import { AuthService, Notifier, RedirectService } from '@app/core' |
79bd2632 C |
9 | import { User, UserRight } from '../../../../shared' |
10 | import { 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 | 16 | export 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, | |
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 | ||
61 | private getUserIfNeeded (account: Account) { | |
62 | if (!account.userId) return | |
63 | if (!this.authService.isLoggedIn()) return | |
64 | ||
65 | const user = this.authService.getUser() | |
66 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
67 | this.userService.getUser(account.userId) | |
68 | .subscribe( | |
69 | user => this.user = user, | |
70 | ||
f8b2c1b4 | 71 | err => this.notifier.error(err.message) |
79bd2632 C |
72 | ) |
73 | } | |
74 | } | |
0626e7af | 75 | } |