1 import { ReplaySubject, Subscription } from 'rxjs'
2 import { filter } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core'
6 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
7 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
10 selector: 'my-quick-settings',
11 templateUrl: './quick-settings-modal.component.html'
13 export class QuickSettingsModalComponent implements OnInit, OnDestroy {
14 private static readonly QUERY_MODAL_NAME = 'quick-settings'
16 @ViewChild('modal', { static: true }) modal: NgbModal
19 userInformationLoaded = new ReplaySubject<boolean>(1)
21 private openedModal: NgbModalRef
23 private routeSub: Subscription
24 private loginSub: Subscription
25 private localStorageSub: Subscription
28 private modalService: NgbModal,
29 private userService: UserService,
30 private authService: AuthService,
31 private localStorageService: LocalStorageService,
32 private route: ActivatedRoute,
33 private router: Router
38 this.user = this.userService.getAnonymousUser()
40 this.localStorageSub = this.localStorageService.watch()
42 next: () => this.user = this.userService.getAnonymousUser()
45 this.userInformationLoaded.next(true)
47 this.loginSub = this.authService.loginChangedSource
48 .pipe(filter(status => status !== AuthStatus.LoggedIn))
51 this.user = this.userService.getAnonymousUser()
52 this.userInformationLoaded.next(true)
56 this.routeSub = this.route.queryParams.subscribe(params => {
57 if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) {
58 this.openedModal = this.modalService.open(this.modal, { centered: true })
60 this.openedModal.hidden.subscribe(() => this.setModalQuery('remove'))
66 if (this.routeSub) this.routeSub.unsubscribe()
67 if (this.loginSub) this.loginSub.unsubscribe()
68 if (this.localStorageSub) this.localStorageSub.unsubscribe()
72 return this.authService.isLoggedIn()
76 this.setModalQuery('add')
79 private setModalQuery (type: 'add' | 'remove') {
80 const modal = type === 'add'
81 ? QuickSettingsModalComponent.QUERY_MODAL_NAME
84 this.router.navigate([], { queryParams: { modal }, queryParamsHandling: 'merge' })