mesauth-angular 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,125 @@
1
+ import { Component, OnInit, OnDestroy, Output, EventEmitter, HostBinding } from '@angular/core';
2
+ import { NgIf } from '@angular/common';
3
+ import { MesAuthService } from './mes-auth.service';
4
+ import { ThemeService, Theme } from './theme.service';
5
+ import { Subject } from 'rxjs';
6
+ import { takeUntil } from 'rxjs/operators';
7
+
8
+ @Component({
9
+ selector: 'ma-notification-badge',
10
+ standalone: true,
11
+ imports: [NgIf],
12
+ template: `
13
+ <button class="notification-btn" (click)="onNotificationClick()" title="Notifications">
14
+ <span class="icon">🔔</span>
15
+ <span class="badge" *ngIf="unreadCount > 0">{{ unreadCount }}</span>
16
+ </button>
17
+ `,
18
+ styles: [`
19
+ :host {
20
+ --error-color: #f44336;
21
+ }
22
+
23
+ :host(.theme-dark) {
24
+ --error-color: #ef5350;
25
+ }
26
+
27
+ .notification-btn {
28
+ position: relative;
29
+ background: none;
30
+ border: none;
31
+ font-size: 24px;
32
+ cursor: pointer;
33
+ padding: 8px;
34
+ transition: opacity 0.2s;
35
+ }
36
+
37
+ .notification-btn:hover {
38
+ opacity: 0.7;
39
+ }
40
+
41
+ .icon {
42
+ display: inline-block;
43
+ }
44
+
45
+ .badge {
46
+ position: absolute;
47
+ top: 0;
48
+ right: 0;
49
+ background-color: var(--error-color);
50
+ color: white;
51
+ border-radius: 50%;
52
+ width: 20px;
53
+ height: 20px;
54
+ display: flex;
55
+ align-items: center;
56
+ justify-content: center;
57
+ font-size: 12px;
58
+ font-weight: bold;
59
+ }
60
+ `]
61
+ })
62
+ export class NotificationBadgeComponent implements OnInit, OnDestroy {
63
+ @Output() notificationClick = new EventEmitter<void>();
64
+ @HostBinding('class') get themeClass(): string {
65
+ return `theme-${this.currentTheme}`;
66
+ }
67
+
68
+ unreadCount = 0;
69
+ currentTheme: Theme = 'light';
70
+ private hasUser = false;
71
+ private destroy$ = new Subject<void>();
72
+
73
+ constructor(private authService: MesAuthService, private themeService: ThemeService) {}
74
+
75
+ ngOnInit() {
76
+ this.themeService.currentTheme$
77
+ .pipe(takeUntil(this.destroy$))
78
+ .subscribe(theme => {
79
+ this.currentTheme = theme;
80
+ });
81
+
82
+ this.authService.currentUser$
83
+ .pipe(takeUntil(this.destroy$))
84
+ .subscribe(user => {
85
+ this.hasUser = !!user;
86
+ if (!this.hasUser) {
87
+ this.unreadCount = 0;
88
+ return;
89
+ }
90
+ this.loadUnreadCount();
91
+ });
92
+
93
+ // Listen for new notifications
94
+ this.authService.notifications$
95
+ .pipe(takeUntil(this.destroy$))
96
+ .subscribe(() => {
97
+ if (this.hasUser) {
98
+ this.loadUnreadCount();
99
+ }
100
+ });
101
+ }
102
+
103
+ ngOnDestroy() {
104
+ this.destroy$.next();
105
+ this.destroy$.complete();
106
+ }
107
+
108
+ private loadUnreadCount() {
109
+ if (!this.hasUser) {
110
+ this.unreadCount = 0;
111
+ return;
112
+ }
113
+
114
+ this.authService.getUnreadCount().subscribe({
115
+ next: (response: any) => {
116
+ this.unreadCount = response.unreadCount || 0;
117
+ },
118
+ error: (err) => console.error('Error loading unread count:', err)
119
+ });
120
+ }
121
+
122
+ onNotificationClick() {
123
+ this.notificationClick.emit();
124
+ }
125
+ }