]>
Commit | Line | Data |
---|---|---|
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' | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-quick-settings', | |
11 | templateUrl: './quick-settings-modal.component.html' | |
12 | }) | |
13 | export class QuickSettingsModalComponent implements OnInit, OnDestroy { | |
14 | private static readonly QUERY_MODAL_NAME = 'quick-settings' | |
15 | ||
16 | @ViewChild('modal', { static: true }) modal: NgbModal | |
17 | ||
18 | user: User | |
19 | userInformationLoaded = new ReplaySubject<boolean>(1) | |
20 | ||
21 | private openedModal: NgbModalRef | |
22 | ||
23 | private routeSub: Subscription | |
24 | private loginSub: Subscription | |
25 | private localStorageSub: Subscription | |
26 | ||
27 | constructor ( | |
28 | private modalService: NgbModal, | |
29 | private userService: UserService, | |
30 | private authService: AuthService, | |
31 | private localStorageService: LocalStorageService, | |
32 | private route: ActivatedRoute, | |
33 | private router: Router | |
34 | ) { | |
35 | } | |
36 | ||
37 | ngOnInit () { | |
38 | this.user = this.userService.getAnonymousUser() | |
39 | ||
40 | this.localStorageSub = this.localStorageService.watch() | |
41 | .subscribe({ | |
42 | next: () => this.user = this.userService.getAnonymousUser() | |
43 | }) | |
44 | ||
45 | this.userInformationLoaded.next(true) | |
46 | ||
47 | this.loginSub = this.authService.loginChangedSource | |
48 | .pipe(filter(status => status !== AuthStatus.LoggedIn)) | |
49 | .subscribe({ | |
50 | next: () => { | |
51 | this.user = this.userService.getAnonymousUser() | |
52 | this.userInformationLoaded.next(true) | |
53 | } | |
54 | }) | |
55 | ||
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 }) | |
59 | ||
60 | this.openedModal.hidden.subscribe(() => this.setModalQuery('remove')) | |
61 | } | |
62 | }) | |
63 | } | |
64 | ||
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 | ||
71 | isUserLoggedIn () { | |
72 | return this.authService.isLoggedIn() | |
73 | } | |
74 | ||
75 | show () { | |
76 | this.setModalQuery('add') | |
77 | } | |
78 | ||
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' }) | |
85 | } | |
86 | } |