]>
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 | private i18n: I18n | |
31 | ) {} | |
32 | ||
33 | ngOnInit () { | |
34 | this.routeSub = this.route.params | |
35 | .pipe( | |
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 ])) | |
41 | ) | |
42 | .subscribe( | |
43 | account => this.account = account, | |
44 | ||
45 | err => this.notifier.error(err.message) | |
46 | ) | |
47 | } | |
48 | ||
49 | ngOnDestroy () { | |
50 | if (this.routeSub) this.routeSub.unsubscribe() | |
51 | } | |
52 | ||
53 | onUserChanged () { | |
54 | this.getUserIfNeeded(this.account) | |
55 | } | |
56 | ||
57 | onUserDeleted () { | |
58 | this.redirectService.redirectToHomepage() | |
59 | } | |
60 | ||
61 | activateCopiedMessage () { | |
62 | this.notifier.success(this.i18n('Username copied')) | |
63 | } | |
64 | ||
65 | private getUserIfNeeded (account: Account) { | |
66 | if (!account.userId) return | |
67 | if (!this.authService.isLoggedIn()) return | |
68 | ||
69 | const user = this.authService.getUser() | |
70 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
71 | this.userService.getUser(account.userId) | |
72 | .subscribe( | |
73 | user => this.user = user, | |
74 | ||
75 | err => this.notifier.error(err.message) | |
76 | ) | |
77 | } | |
78 | } | |
79 | } |