]>
Commit | Line | Data |
---|---|---|
6e060713 | 1 | import { ReplaySubject, Subscription } from 'rxjs' |
d3217560 | 2 | import { filter } from 'rxjs/operators' |
6e060713 | 3 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
efd4ff5f | 4 | import { ActivatedRoute, Router } from '@angular/router' |
67ed6552 | 5 | import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core' |
67ed6552 C |
6 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
7 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
d3217560 RK |
8 | |
9 | @Component({ | |
10 | selector: 'my-quick-settings', | |
266947e5 | 11 | templateUrl: './quick-settings-modal.component.html' |
d3217560 | 12 | }) |
6e060713 | 13 | export class QuickSettingsModalComponent implements OnInit, OnDestroy { |
efd4ff5f C |
14 | private static readonly QUERY_MODAL_NAME = 'quick-settings' |
15 | ||
d3217560 RK |
16 | @ViewChild('modal', { static: true }) modal: NgbModal |
17 | ||
18 | user: User | |
19 | userInformationLoaded = new ReplaySubject<boolean>(1) | |
20 | ||
21 | private openedModal: NgbModalRef | |
22 | ||
6e060713 C |
23 | private routeSub: Subscription |
24 | private loginSub: Subscription | |
25 | private localStorageSub: Subscription | |
26 | ||
d3217560 | 27 | constructor ( |
d3217560 RK |
28 | private modalService: NgbModal, |
29 | private userService: UserService, | |
30 | private authService: AuthService, | |
efd4ff5f C |
31 | private localStorageService: LocalStorageService, |
32 | private route: ActivatedRoute, | |
33 | private router: Router | |
d3217560 | 34 | ) { |
d3217560 RK |
35 | } |
36 | ||
37 | ngOnInit () { | |
38 | this.user = this.userService.getAnonymousUser() | |
6e060713 C |
39 | |
40 | this.localStorageSub = this.localStorageService.watch() | |
1378c0d3 C |
41 | .subscribe({ |
42 | next: () => this.user = this.userService.getAnonymousUser() | |
43 | }) | |
5c20a455 | 44 | |
d3217560 RK |
45 | this.userInformationLoaded.next(true) |
46 | ||
6e060713 | 47 | this.loginSub = this.authService.loginChangedSource |
d3217560 | 48 | .pipe(filter(status => status !== AuthStatus.LoggedIn)) |
1378c0d3 C |
49 | .subscribe({ |
50 | next: () => { | |
d3217560 RK |
51 | this.user = this.userService.getAnonymousUser() |
52 | this.userInformationLoaded.next(true) | |
53 | } | |
1378c0d3 | 54 | }) |
efd4ff5f | 55 | |
6e060713 | 56 | this.routeSub = this.route.queryParams.subscribe(params => { |
efd4ff5f C |
57 | if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) { |
58 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
59 | ||
60 | this.openedModal.hidden.subscribe(() => this.setModalQuery('remove')) | |
61 | } | |
62 | }) | |
d3217560 RK |
63 | } |
64 | ||
6e060713 C |
65 | ngOnDestroy () { |
66 | if (this.routeSub) this.routeSub.unsubscribe() | |
67 | if (this.loginSub) this.loginSub.unsubscribe() | |
68 | if (this.localStorageSub) this.localStorageSub.unsubscribe() | |
69 | } | |
70 | ||
d3217560 RK |
71 | isUserLoggedIn () { |
72 | return this.authService.isLoggedIn() | |
73 | } | |
74 | ||
75 | show () { | |
efd4ff5f | 76 | this.setModalQuery('add') |
d3217560 RK |
77 | } |
78 | ||
efd4ff5f C |
79 | private setModalQuery (type: 'add' | 'remove') { |
80 | const modal = type === 'add' | |
81 | ? QuickSettingsModalComponent.QUERY_MODAL_NAME | |
82 | : null | |
83 | ||
84 | this.router.navigate([], { queryParams: { modal }, queryParamsHandling: 'merge' }) | |
d3217560 RK |
85 | } |
86 | } |