]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | @Component({ | |
13 | templateUrl: './accounts.component.html', | |
14 | styleUrls: [ './accounts.component.scss' ] | |
15 | }) | |
16 | export class AccountsComponent implements OnInit, OnDestroy { | |
17 | account: Account | |
18 | user: User | |
19 | ||
20 | private routeSub: Subscription | |
21 | ||
22 | constructor ( | |
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 | |
30 | ) {} | |
31 | ||
32 | ngOnInit () { | |
33 | this.routeSub = this.route.params | |
34 | .pipe( | |
35 | map(params => params[ 'accountId' ]), | |
36 | distinctUntilChanged(), | |
37 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
38 | tap(account => this.getUserIfNeeded(account)), | |
39 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) | |
40 | ) | |
41 | .subscribe( | |
42 | account => this.account = account, | |
43 | ||
44 | err => this.notifier.error(err.message) | |
45 | ) | |
46 | } | |
47 | ||
48 | ngOnDestroy () { | |
49 | if (this.routeSub) this.routeSub.unsubscribe() | |
50 | } | |
51 | ||
52 | onUserChanged () { | |
53 | this.getUserIfNeeded(this.account) | |
54 | } | |
55 | ||
56 | onUserDeleted () { | |
57 | this.redirectService.redirectToHomepage() | |
58 | } | |
59 | ||
60 | private getUserIfNeeded (account: Account) { | |
61 | if (!account.userId) return | |
62 | if (!this.authService.isLoggedIn()) return | |
63 | ||
64 | const user = this.authService.getUser() | |
65 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
66 | this.userService.getUser(account.userId) | |
67 | .subscribe( | |
68 | user => this.user = user, | |
69 | ||
70 | err => this.notifier.error(err.message) | |
71 | ) | |
72 | } | |
73 | } | |
74 | } |