2018-10-05 16:56:14 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core'
|
2018-04-24 15:10:54 +02:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
|
|
|
import { AccountService } from '@app/shared/account/account.service'
|
|
|
|
import { Account } from '@app/shared/account/account.model'
|
2018-10-05 16:56:14 +02:00
|
|
|
import { RestExtractor, UserService } from '@app/shared'
|
|
|
|
import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
|
2018-06-07 11:19:26 +02:00
|
|
|
import { Subscription } from 'rxjs'
|
2018-10-05 16:56:14 +02:00
|
|
|
import { NotificationsService } from 'angular2-notifications'
|
|
|
|
import { User, UserRight } from '../../../../shared'
|
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
|
|
|
import { AuthService, RedirectService } from '@app/core'
|
2018-04-24 15:10:54 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-04-25 16:56:13 +02:00
|
|
|
templateUrl: './accounts.component.html',
|
|
|
|
styleUrls: [ './accounts.component.scss' ]
|
2018-04-24 15:10:54 +02:00
|
|
|
})
|
2018-06-07 14:58:41 +02:00
|
|
|
export class AccountsComponent implements OnInit, OnDestroy {
|
2018-04-25 10:21:38 +02:00
|
|
|
account: Account
|
2018-10-05 16:56:14 +02:00
|
|
|
user: User
|
2018-04-24 15:10:54 +02:00
|
|
|
|
2018-06-07 11:19:26 +02:00
|
|
|
private routeSub: Subscription
|
|
|
|
|
2018-04-24 15:10:54 +02:00
|
|
|
constructor (
|
|
|
|
private route: ActivatedRoute,
|
2018-10-05 16:56:14 +02:00
|
|
|
private userService: UserService,
|
2018-05-31 11:35:01 +02:00
|
|
|
private accountService: AccountService,
|
2018-10-05 16:56:14 +02:00
|
|
|
private notificationsService: NotificationsService,
|
|
|
|
private restExtractor: RestExtractor,
|
|
|
|
private redirectService: RedirectService,
|
|
|
|
private authService: AuthService,
|
|
|
|
private i18n: I18n
|
2018-04-24 15:10:54 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
ngOnInit () {
|
2018-06-07 11:19:26 +02:00
|
|
|
this.routeSub = this.route.params
|
|
|
|
.pipe(
|
|
|
|
map(params => params[ 'accountId' ]),
|
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap(accountId => this.accountService.getAccount(accountId)),
|
2018-10-05 16:56:14 +02:00
|
|
|
tap(account => this.getUserIfNeeded(account)),
|
2018-06-07 11:19:26 +02:00
|
|
|
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
|
|
|
|
)
|
2018-10-05 16:56:14 +02:00
|
|
|
.subscribe(
|
|
|
|
account => this.account = account,
|
|
|
|
|
|
|
|
err => this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
)
|
2018-06-07 11:19:26 +02:00
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
|
2018-06-07 11:19:26 +02:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
2018-04-24 15:10:54 +02:00
|
|
|
}
|
2018-10-05 16:56:14 +02:00
|
|
|
|
|
|
|
onUserChanged () {
|
|
|
|
this.getUserIfNeeded(this.account)
|
|
|
|
}
|
|
|
|
|
|
|
|
onUserDeleted () {
|
|
|
|
this.redirectService.redirectToHomepage()
|
|
|
|
}
|
|
|
|
|
|
|
|
private getUserIfNeeded (account: Account) {
|
|
|
|
if (!account.userId) return
|
|
|
|
if (!this.authService.isLoggedIn()) return
|
|
|
|
|
|
|
|
const user = this.authService.getUser()
|
|
|
|
if (user.hasRight(UserRight.MANAGE_USERS)) {
|
|
|
|
this.userService.getUser(account.userId)
|
|
|
|
.subscribe(
|
|
|
|
user => this.user = user,
|
|
|
|
|
|
|
|
err => this.notificationsService.error(this.i18n('Error'), err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
}
|