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