2023-02-16 16:13:19 +01:00
|
|
|
import { ReplaySubject, Subscription } from 'rxjs'
|
2020-02-28 13:52:21 +01:00
|
|
|
import { filter } from 'rxjs/operators'
|
2023-02-16 16:13:19 +01:00
|
|
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2022-01-20 10:11:49 +01:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core'
|
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|
|
|
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
|
2020-02-28 13:52:21 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-quick-settings',
|
2021-04-14 16:39:37 +02:00
|
|
|
templateUrl: './quick-settings-modal.component.html'
|
2020-02-28 13:52:21 +01:00
|
|
|
})
|
2023-02-16 16:13:19 +01:00
|
|
|
export class QuickSettingsModalComponent implements OnInit, OnDestroy {
|
2022-01-20 10:11:49 +01:00
|
|
|
private static readonly QUERY_MODAL_NAME = 'quick-settings'
|
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
@ViewChild('modal', { static: true }) modal: NgbModal
|
|
|
|
|
|
|
|
user: User
|
|
|
|
userInformationLoaded = new ReplaySubject<boolean>(1)
|
|
|
|
|
|
|
|
private openedModal: NgbModalRef
|
|
|
|
|
2023-02-16 16:13:19 +01:00
|
|
|
private routeSub: Subscription
|
|
|
|
private loginSub: Subscription
|
|
|
|
private localStorageSub: Subscription
|
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
constructor (
|
|
|
|
private modalService: NgbModal,
|
|
|
|
private userService: UserService,
|
|
|
|
private authService: AuthService,
|
2022-01-20 10:11:49 +01:00
|
|
|
private localStorageService: LocalStorageService,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router
|
2020-02-28 13:52:21 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.user = this.userService.getAnonymousUser()
|
2023-02-16 16:13:19 +01:00
|
|
|
|
|
|
|
this.localStorageSub = this.localStorageService.watch()
|
2021-08-17 11:27:47 +02:00
|
|
|
.subscribe({
|
|
|
|
next: () => this.user = this.userService.getAnonymousUser()
|
|
|
|
})
|
2020-06-16 11:00:35 +02:00
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
this.userInformationLoaded.next(true)
|
|
|
|
|
2023-02-16 16:13:19 +01:00
|
|
|
this.loginSub = this.authService.loginChangedSource
|
2020-02-28 13:52:21 +01:00
|
|
|
.pipe(filter(status => status !== AuthStatus.LoggedIn))
|
2021-08-17 11:27:47 +02:00
|
|
|
.subscribe({
|
|
|
|
next: () => {
|
2020-02-28 13:52:21 +01:00
|
|
|
this.user = this.userService.getAnonymousUser()
|
|
|
|
this.userInformationLoaded.next(true)
|
|
|
|
}
|
2021-08-17 11:27:47 +02:00
|
|
|
})
|
2022-01-20 10:11:49 +01:00
|
|
|
|
2023-02-16 16:13:19 +01:00
|
|
|
this.routeSub = this.route.queryParams.subscribe(params => {
|
2022-01-20 10:11:49 +01:00
|
|
|
if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) {
|
|
|
|
this.openedModal = this.modalService.open(this.modal, { centered: true })
|
|
|
|
|
|
|
|
this.openedModal.hidden.subscribe(() => this.setModalQuery('remove'))
|
|
|
|
}
|
|
|
|
})
|
2020-02-28 13:52:21 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 16:13:19 +01:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
|
|
|
if (this.loginSub) this.loginSub.unsubscribe()
|
|
|
|
if (this.localStorageSub) this.localStorageSub.unsubscribe()
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
isUserLoggedIn () {
|
|
|
|
return this.authService.isLoggedIn()
|
|
|
|
}
|
|
|
|
|
|
|
|
show () {
|
2022-01-20 10:11:49 +01:00
|
|
|
this.setModalQuery('add')
|
2020-02-28 13:52:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 10:11:49 +01:00
|
|
|
private setModalQuery (type: 'add' | 'remove') {
|
|
|
|
const modal = type === 'add'
|
|
|
|
? QuickSettingsModalComponent.QUERY_MODAL_NAME
|
|
|
|
: null
|
|
|
|
|
|
|
|
this.router.navigate([], { queryParams: { modal }, queryParamsHandling: 'merge' })
|
2020-02-28 13:52:21 +01:00
|
|
|
}
|
|
|
|
}
|