From e98fb20ced81b0f3ca079da8b674ae97df75dfd7 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Tue, 10 Dec 2024 11:13:38 +0400 Subject: [PATCH] Refactor ProfileComponent and AuthService: remove unused imports, simplify isLoggedIn logic, and implement signal-based state management in NotificationsComponent --- .../components/profile/profile.component.ts | 2 - src/app/core/auth/auth.guard.ts | 10 +--- .../notifications.component.html | 45 ++--------------- .../notifications/notifications.component.ts | 29 ++++++----- src/app/services/auth.service.ts | 48 ++++++++++++++----- src/app/services/notification.service.ts | 2 +- 6 files changed, 59 insertions(+), 77 deletions(-) diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index da39b97..6179f4c 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -1,6 +1,5 @@ import { AngorCardComponent } from '@angor/components/card'; import { AngorConfigService } from '@angor/services/config'; -import { AngorConfirmationService } from '@angor/services/confirmation'; import { Clipboard } from '@angular/cdk/clipboard'; import { TextFieldModule } from '@angular/cdk/text-field'; import { CommonModule, NgClass } from '@angular/common'; @@ -149,7 +148,6 @@ export class ProfileComponent implements OnInit, OnDestroy { private _snackBar: MatSnackBar, private _dialog: MatDialog, private _angorConfigService: AngorConfigService, - private _angorConfirmationService: AngorConfirmationService, private _eventService: EventService, private _subscriptionService: SubscriptionService, private _clipboard: Clipboard, diff --git a/src/app/core/auth/auth.guard.ts b/src/app/core/auth/auth.guard.ts index 5b93b69..445cf89 100644 --- a/src/app/core/auth/auth.guard.ts +++ b/src/app/core/auth/auth.guard.ts @@ -1,15 +1,9 @@ import { inject } from '@angular/core'; -import { Router } from '@angular/router'; import { SignerService } from 'app/services/signer.service'; export const authGuard = () => { const signerService = inject(SignerService); - const router = inject(Router); - if (signerService.getPublicKey() !== '') { - return true; - } - - // Redirect to the login page - return router.parseUrl('/login'); + // Allow access to the page + return true; }; diff --git a/src/app/layout/common/notifications/notifications.component.html b/src/app/layout/common/notifications/notifications.component.html index 9ce3349..4815cc7 100644 --- a/src/app/layout/common/notifications/notifications.component.html +++ b/src/app/layout/common/notifications/notifications.component.html @@ -1,13 +1,13 @@ - - - @@ -177,7 +142,7 @@ } - @if (!notifications || !notifications.length) { + @if (!notifications() || !notifications().length) {
diff --git a/src/app/layout/common/notifications/notifications.component.ts b/src/app/layout/common/notifications/notifications.component.ts index f5f03bb..412cde8 100644 --- a/src/app/layout/common/notifications/notifications.component.ts +++ b/src/app/layout/common/notifications/notifications.component.ts @@ -11,6 +11,8 @@ import { ViewChild, ViewContainerRef, ViewEncapsulation, + inject, + signal, } from '@angular/core'; import { MatButton, MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; @@ -45,18 +47,16 @@ export class NotificationsComponent implements OnInit, OnDestroy { @ViewChild('notificationsPanel') private _notificationsPanel: TemplateRef; - notifications: NostrNotification[] = []; - unreadCount: number = 0; + notifications = signal([]); + unreadCount = signal(0); private _overlayRef: OverlayRef; - private _unsubscribeAll: Subject = new Subject(); + private _unsubscribeAll = new Subject(); - constructor( - private _notificationService: NotificationService, - private _changeDetectorRef: ChangeDetectorRef, - private _overlay: Overlay, - private _viewContainerRef: ViewContainerRef, - private _signerService: SignerService - ) {} + private _notificationService = inject(NotificationService); + private _changeDetectorRef = inject(ChangeDetectorRef); + private _overlay = inject(Overlay); + private _viewContainerRef = inject(ViewContainerRef); + private _signerService = inject(SignerService); ngOnInit(): void { const pubkey = this._signerService.getPublicKey(); @@ -66,7 +66,7 @@ export class NotificationsComponent implements OnInit, OnDestroy { .getNotificationObservable() .pipe(takeUntil(this._unsubscribeAll)) .subscribe((notifications: NostrNotification[]) => { - this.notifications = notifications; + this.notifications.set(notifications); this._changeDetectorRef.markForCheck(); }); @@ -74,7 +74,7 @@ export class NotificationsComponent implements OnInit, OnDestroy { .getNotificationCount() .pipe(takeUntil(this._unsubscribeAll)) .subscribe((count: number) => { - this.unreadCount = count; + this.unreadCount.set(count); this._changeDetectorRef.markForCheck(); }); }); @@ -111,11 +111,10 @@ export class NotificationsComponent implements OnInit, OnDestroy { markAllAsRead(): void { this._notificationService.markAllAsRead(); + this.notifications.set([]); // Clear all notifications + this._changeDetectorRef.markForCheck(); // Trigger change detection } - toggleRead(notification: NostrNotification): void { - notification.read = !notification.read; - } trackByFn(index: number, item: NostrNotification): string { return item.id; diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 0b32b63..de372fc 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,21 +1,47 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { SignerService } from './signer.service'; +import { AngorConfirmationService } from '@angor/services/confirmation'; @Injectable({ providedIn: 'root', }) export class AuthService { - constructor( - private signerService: SignerService, - private router: Router - ) {} + private signerService = inject(SignerService); + private router = inject(Router); - isLoggedIn() { - if (this.signerService.getPublicKey()) { - return true; - } - this.router.navigate(['/login']); - return false; + isLoggedIn(): boolean { + return !!this.signerService.getPublicKey(); + } + + promptLogin(): void { + const angorConfirmationService = inject(AngorConfirmationService); + const dialogRef = angorConfirmationService.open({ + title: 'Login Required', + message: 'You need to be logged in to perform this action. Would you like to login now?', + icon: { + show: true, + name: 'heroicons_solid:login', + color: 'primary', + }, + actions: { + confirm: { + show: true, + label: 'Login', + color: 'primary', + }, + cancel: { + show: true, + label: 'Cancel', + }, + }, + dismissible: true, + }); + + dialogRef.afterClosed().subscribe((result) => { + if (result === 'confirmed') { + this.router.navigate(['/login']); + } + }); } } diff --git a/src/app/services/notification.service.ts b/src/app/services/notification.service.ts index 4765448..c466170 100644 --- a/src/app/services/notification.service.ts +++ b/src/app/services/notification.service.ts @@ -173,7 +173,7 @@ export class NotificationService { read: true, }) ); - this.notificationSubject.next(updatedNotifications); + this.notificationSubject.next([]); // Clear all notifications this.notificationCount.next(0); const currentTimestamp = Math.floor(Date.now() / 1000); this.saveNotificationData(0, currentTimestamp);