@churchapps/apphelper 0.5.11 → 0.6.0

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.
Files changed (73) hide show
  1. package/dist/components/ErrorMessages.js +1 -1
  2. package/dist/components/ErrorMessages.js.map +1 -1
  3. package/dist/components/FormCardPayment.js +2 -2
  4. package/dist/components/FormCardPayment.js.map +1 -1
  5. package/dist/components/ImageEditor.d.ts.map +1 -1
  6. package/dist/components/ImageEditor.js +13 -8
  7. package/dist/components/ImageEditor.js.map +1 -1
  8. package/dist/components/PageHeader.js +1 -1
  9. package/dist/components/PageHeader.js.map +1 -1
  10. package/dist/components/QuestionEdit.js +2 -2
  11. package/dist/components/QuestionEdit.js.map +1 -1
  12. package/dist/components/SmallButton.js +1 -1
  13. package/dist/components/SmallButton.js.map +1 -1
  14. package/dist/components/gallery/GalleryModal.d.ts.map +1 -1
  15. package/dist/components/gallery/GalleryModal.js +2 -6
  16. package/dist/components/gallery/GalleryModal.js.map +1 -1
  17. package/dist/components/header/SecondaryMenu.js +2 -2
  18. package/dist/components/header/SecondaryMenu.js.map +1 -1
  19. package/dist/components/header/SiteHeader.d.ts.map +1 -1
  20. package/dist/components/header/SiteHeader.js +0 -3
  21. package/dist/components/header/SiteHeader.js.map +1 -1
  22. package/dist/components/notes/Note.js +1 -1
  23. package/dist/components/notes/Note.js.map +1 -1
  24. package/dist/components/wrapper/ChurchList.d.ts.map +1 -1
  25. package/dist/components/wrapper/ChurchList.js +0 -7
  26. package/dist/components/wrapper/ChurchList.js.map +1 -1
  27. package/dist/components/wrapper/NewPrivateMessage.d.ts.map +1 -1
  28. package/dist/components/wrapper/NewPrivateMessage.js +1 -2
  29. package/dist/components/wrapper/NewPrivateMessage.js.map +1 -1
  30. package/dist/components/wrapper/Notifications.js +1 -1
  31. package/dist/components/wrapper/Notifications.js.map +1 -1
  32. package/dist/components/wrapper/PrivateMessageDetails.d.ts.map +1 -1
  33. package/dist/components/wrapper/PrivateMessageDetails.js +0 -2
  34. package/dist/components/wrapper/PrivateMessageDetails.js.map +1 -1
  35. package/dist/components/wrapper/PrivateMessages.js +1 -1
  36. package/dist/components/wrapper/PrivateMessages.js.map +1 -1
  37. package/dist/components/wrapper/UserMenu.d.ts.map +1 -1
  38. package/dist/components/wrapper/UserMenu.js +0 -13
  39. package/dist/components/wrapper/UserMenu.js.map +1 -1
  40. package/dist/helpers/ErrorHelper.d.ts.map +1 -1
  41. package/dist/helpers/ErrorHelper.js +0 -2
  42. package/dist/helpers/ErrorHelper.js.map +1 -1
  43. package/dist/helpers/NotificationService.d.ts +0 -18
  44. package/dist/helpers/NotificationService.d.ts.map +1 -1
  45. package/dist/helpers/NotificationService.js +0 -65
  46. package/dist/helpers/NotificationService.js.map +1 -1
  47. package/dist/helpers/SocketHelper.d.ts.map +1 -1
  48. package/dist/helpers/SocketHelper.js +1 -53
  49. package/dist/helpers/SocketHelper.js.map +1 -1
  50. package/dist/helpers/UserHelper.d.ts.map +1 -1
  51. package/dist/helpers/UserHelper.js +0 -4
  52. package/dist/helpers/UserHelper.js.map +1 -1
  53. package/package.json +1 -1
  54. package/src/components/ErrorMessages.tsx +1 -1
  55. package/src/components/FormCardPayment.tsx +1 -1
  56. package/src/components/ImageEditor.tsx +167 -161
  57. package/src/components/PageHeader.tsx +2 -2
  58. package/src/components/QuestionEdit.tsx +2 -2
  59. package/src/components/SmallButton.tsx +1 -1
  60. package/src/components/gallery/GalleryModal.tsx +169 -173
  61. package/src/components/header/SecondaryMenu.tsx +2 -2
  62. package/src/components/header/SiteHeader.tsx +204 -207
  63. package/src/components/notes/Note.tsx +2 -2
  64. package/src/components/wrapper/ChurchList.tsx +145 -154
  65. package/src/components/wrapper/NewPrivateMessage.tsx +1 -2
  66. package/src/components/wrapper/Notifications.tsx +1 -1
  67. package/src/components/wrapper/PrivateMessageDetails.tsx +2 -6
  68. package/src/components/wrapper/PrivateMessages.tsx +1 -1
  69. package/src/components/wrapper/UserMenu.tsx +3 -17
  70. package/src/helpers/ErrorHelper.ts +41 -43
  71. package/src/helpers/NotificationService.ts +232 -296
  72. package/src/helpers/SocketHelper.ts +247 -296
  73. package/src/helpers/UserHelper.ts +2 -6
@@ -1,297 +1,233 @@
1
- import { SocketHelper } from "./SocketHelper";
2
- import { ApiHelper, UserContextInterface } from "@churchapps/helpers";
3
-
4
- export interface NotificationCounts {
5
- notificationCount: number;
6
- pmCount: number;
7
- }
8
-
9
- export class NotificationService {
10
- private static instance: NotificationService;
11
- private counts: NotificationCounts = { notificationCount: 0, pmCount: 0 };
12
- private listeners: Array<(counts: NotificationCounts) => void> = [];
13
- private isInitialized: boolean = false;
14
- private currentPersonId: string | null = null;
15
- private loadTimeout: any | null = null;
16
-
17
- private constructor() {}
18
-
19
- static getInstance(): NotificationService {
20
- if (!NotificationService.instance) {
21
- NotificationService.instance = new NotificationService();
22
- }
23
- return NotificationService.instance;
24
- }
25
-
26
- /**
27
- * Initialize the notification service with user context
28
- */
29
- /**
30
- * Initialize the notification service with user context
31
- */
32
- async initialize(context: UserContextInterface): Promise<void> {
33
- if (this.isInitialized) {
34
- console.log('🔔 NotificationService: Already initialized, skipping');
35
- return;
36
- }
37
-
38
- console.log('🔔 NotificationService: Starting initialization with context:', {
39
- hasUser: !!context?.user,
40
- hasPerson: !!context?.person,
41
- hasUserChurch: !!context?.userChurch,
42
- personId: context?.person?.id,
43
- churchId: context?.userChurch?.church?.id
44
- });
45
-
46
- try {
47
- // Store current person ID for conversation counting
48
- this.currentPersonId = context?.person?.id || null;
49
- console.log('👤 NotificationService: Set current person ID:', this.currentPersonId);
50
-
51
- // Initialize WebSocket connection
52
- console.log('🔌 NotificationService: Initializing SocketHelper...');
53
- await SocketHelper.init();
54
-
55
- // Set person/church context for websocket
56
- if (context?.person?.id && context?.userChurch?.church?.id) {
57
- console.log('🔗 NotificationService: Setting person/church context in SocketHelper');
58
- SocketHelper.setPersonChurch({
59
- personId: context.person.id,
60
- churchId: context.userChurch.church.id
61
- });
62
- } else {
63
- console.warn('⚠️ NotificationService: Missing person/church IDs, cannot set socket context');
64
- }
65
-
66
- // Register handlers for notification updates
67
- console.log('📋 NotificationService: Registering WebSocket handlers');
68
- this.registerWebSocketHandlers();
69
-
70
- // Load initial notification counts
71
- console.log('📊 NotificationService: Loading initial notification counts');
72
- await this.loadNotificationCounts();
73
-
74
- this.isInitialized = true;
75
- console.log('✅ NotificationService: Initialization complete');
76
-
77
- } catch (error) {
78
- console.error("❌ Failed to initialize NotificationService:", error);
79
- throw error;
80
- }
81
- }
82
-
83
- /**
84
- * Register websocket handlers for real-time notification updates
85
- */
86
- /**
87
- * Register websocket handlers for real-time notification updates
88
- */
89
- /**
90
- * Register websocket handlers for real-time notification updates
91
- */
92
- private registerWebSocketHandlers(): void {
93
- // Handler for new private messages
94
- SocketHelper.addHandler("privateMessage", "NotificationService-PM", (data: any) => {
95
- console.log('🔔 NotificationService: New private message received, updating counts');
96
- console.log('📨 Private message data:', data);
97
- console.log('🔄 NotificationService: About to call debouncedLoadNotificationCounts...');
98
- try {
99
- this.debouncedLoadNotificationCounts();
100
- console.log('✅ NotificationService: debouncedLoadNotificationCounts called successfully');
101
- } catch (error) {
102
- console.error(' NotificationService: Error calling debouncedLoadNotificationCounts:', error);
103
- }
104
- });
105
-
106
- // Handler for general notifications
107
- SocketHelper.addHandler("notification", "NotificationService-Notification", (data: any) => {
108
- console.log('🔔 NotificationService: New notification received, updating counts');
109
- console.log('📨 Notification data:', data);
110
- console.log('🔄 NotificationService: About to call debouncedLoadNotificationCounts...');
111
- try {
112
- this.debouncedLoadNotificationCounts();
113
- console.log('✅ NotificationService: debouncedLoadNotificationCounts called successfully');
114
- } catch (error) {
115
- console.error('❌ NotificationService: Error calling debouncedLoadNotificationCounts:', error);
116
- }
117
- });
118
-
119
- // Handler for message updates that could affect notification counts
120
- SocketHelper.addHandler("message", "NotificationService-MessageUpdate", (data: any) => {
121
- // Only update counts if the message update involves the current person
122
- if (data?.message?.personId === this.currentPersonId ||
123
- data?.notifyPersonId === this.currentPersonId) {
124
- console.log('🔔 NotificationService: Message update affecting current user, updating counts');
125
- console.log('📨 Message update data:', data);
126
- console.log('🔄 NotificationService: About to call debouncedLoadNotificationCounts...');
127
- try {
128
- this.debouncedLoadNotificationCounts();
129
- console.log('✅ NotificationService: debouncedLoadNotificationCounts called successfully');
130
- } catch (error) {
131
- console.error('❌ NotificationService: Error calling debouncedLoadNotificationCounts:', error);
132
- }
133
- } else {
134
- console.log('🔕 NotificationService: Message update not for current user, ignoring');
135
- console.log('📨 Message personId:', data?.message?.personId, 'Current personId:', this.currentPersonId);
136
- }
137
- });
138
-
139
- // Handler for reconnect events
140
- SocketHelper.addHandler("reconnect", "NotificationService-Reconnect", (data: any) => {
141
- console.log('🔔 NotificationService: WebSocket reconnected, refreshing counts');
142
- this.loadNotificationCounts(); // Don't debounce reconnect - need immediate update
143
- });
144
- }
145
-
146
- /**
147
- * Load notification counts from the API with debouncing
148
- */
149
- /**
150
- * Load notification counts from the API with debouncing
151
- */
152
- private debouncedLoadNotificationCounts(): void {
153
- console.log('⏰ NotificationService: Debounced load triggered');
154
-
155
- if (this.loadTimeout) {
156
- console.log('⏰ NotificationService: Clearing existing timeout');
157
- clearTimeout(this.loadTimeout);
158
- }
159
-
160
- this.loadTimeout = setTimeout(() => {
161
- console.log('⏰ NotificationService: Timeout expired, loading counts...');
162
- this.loadNotificationCounts();
163
- }, 300); // 300ms debounce
164
- }
165
-
166
- /**
167
- * Load notification counts from the API
168
- */
169
- /**
170
- * Load notification counts from the API
171
- */
172
- async loadNotificationCounts(): Promise<void> {
173
- console.log('📊 NotificationService: Loading notification counts from API...');
174
-
175
- try {
176
- // Use the unreadCount endpoint which returns both notification and PM counts
177
- console.log('🌐 NotificationService: Making API call to /notifications/unreadCount');
178
- const counts = await ApiHelper.get("/notifications/unreadCount", "MessagingApi");
179
- console.log('📊 NotificationService: API response:', counts);
180
-
181
- const newCounts = {
182
- notificationCount: counts?.notificationCount || 0,
183
- pmCount: counts?.pmCount || 0
184
- };
185
-
186
- console.log('🔄 NotificationService: Updating counts:', newCounts);
187
-
188
- // Update counts and notify listeners
189
- this.updateCounts(newCounts);
190
-
191
- } catch (error) {
192
- console.error("❌ Failed to load notification counts:", error);
193
- console.error("❌ Error details:", {
194
- message: error.message,
195
- status: error.status,
196
- response: error.response
197
- });
198
- // Don't throw - just log the error and keep existing counts
199
- }
200
- }
201
-
202
- /**
203
- * Update counts and notify all listeners
204
- */
205
- /**
206
- * Update counts and notify all listeners
207
- */
208
- private updateCounts(newCounts: NotificationCounts): void {
209
- console.log('🔔 NotificationService: updateCounts called with:', newCounts);
210
- console.log('🔔 NotificationService: Current counts:', this.counts);
211
- console.log('🔔 NotificationService: Number of listeners:', this.listeners.length);
212
-
213
- const countsChanged =
214
- this.counts.notificationCount !== newCounts.notificationCount ||
215
- this.counts.pmCount !== newCounts.pmCount;
216
-
217
- console.log('🔄 NotificationService: Counts changed?', countsChanged);
218
-
219
- if (countsChanged) {
220
- this.counts = { ...newCounts };
221
- console.log('✅ NotificationService: Counts updated, notifying listeners...');
222
-
223
- // Notify all listeners
224
- this.listeners.forEach((listener, index) => {
225
- try {
226
- console.log(`📢 NotificationService: Calling listener ${index + 1}/${this.listeners.length}`);
227
- listener(this.counts);
228
- } catch (error) {
229
- console.error(`❌ Error in notification listener ${index}:`, error);
230
- }
231
- });
232
- } else {
233
- console.log('⚪ NotificationService: Counts unchanged, not notifying listeners');
234
- }
235
- }
236
-
237
- /**
238
- * Subscribe to notification count changes
239
- */
240
- subscribe(listener: (counts: NotificationCounts) => void): () => void {
241
- this.listeners.push(listener);
242
-
243
- // Immediately call with current counts
244
- listener(this.counts);
245
-
246
- // Return unsubscribe function
247
- return () => {
248
- this.listeners = this.listeners.filter(l => l !== listener);
249
- };
250
- }
251
-
252
- /**
253
- * Get current notification counts
254
- */
255
- getCounts(): NotificationCounts {
256
- return { ...this.counts };
257
- }
258
-
259
- /**
260
- * Manually refresh notification counts
261
- */
262
- async refresh(): Promise<void> {
263
- await this.loadNotificationCounts();
264
- }
265
-
266
- /**
267
- * Cleanup the service
268
- */
269
- cleanup(): void {
270
- // Clear any pending timeout
271
- if (this.loadTimeout) {
272
- clearTimeout(this.loadTimeout);
273
- this.loadTimeout = null;
274
- }
275
-
276
- // Remove websocket handlers
277
- SocketHelper.removeHandler("NotificationService-PM");
278
- SocketHelper.removeHandler("NotificationService-Notification");
279
- SocketHelper.removeHandler("NotificationService-MessageUpdate");
280
- SocketHelper.removeHandler("NotificationService-Reconnect");
281
-
282
- // Clear listeners
283
- this.listeners = [];
284
-
285
- // Reset state
286
- this.counts = { notificationCount: 0, pmCount: 0 };
287
- this.currentPersonId = null;
288
- this.isInitialized = false;
289
- }
290
-
291
- /**
292
- * Check if service is initialized
293
- */
294
- isReady(): boolean {
295
- return this.isInitialized && SocketHelper.isConnected();
296
- }
1
+ import { SocketHelper } from "./SocketHelper";
2
+ import { ApiHelper, UserContextInterface } from "@churchapps/helpers";
3
+
4
+ export interface NotificationCounts {
5
+ notificationCount: number;
6
+ pmCount: number;
7
+ }
8
+
9
+ export class NotificationService {
10
+ private static instance: NotificationService;
11
+ private counts: NotificationCounts = { notificationCount: 0, pmCount: 0 };
12
+ private listeners: Array<(counts: NotificationCounts) => void> = [];
13
+ private isInitialized: boolean = false;
14
+ private currentPersonId: string | null = null;
15
+ private loadTimeout: any | null = null;
16
+
17
+ private constructor() {}
18
+
19
+ static getInstance(): NotificationService {
20
+ if (!NotificationService.instance) {
21
+ NotificationService.instance = new NotificationService();
22
+ }
23
+ return NotificationService.instance;
24
+ }
25
+
26
+ /**
27
+ * Initialize the notification service with user context
28
+ */
29
+ async initialize(context: UserContextInterface): Promise<void> {
30
+ if (this.isInitialized) {
31
+ return;
32
+ }
33
+
34
+ try {
35
+ // Store current person ID for conversation counting
36
+ this.currentPersonId = context?.person?.id || null;
37
+
38
+ // Initialize WebSocket connection
39
+ await SocketHelper.init();
40
+
41
+ // Set person/church context for websocket
42
+ if (context?.person?.id && context?.userChurch?.church?.id) {
43
+ SocketHelper.setPersonChurch({
44
+ personId: context.person.id,
45
+ churchId: context.userChurch.church.id
46
+ });
47
+ } else {
48
+ console.warn('⚠️ NotificationService: Missing person/church IDs, cannot set socket context');
49
+ }
50
+
51
+ // Register handlers for notification updates
52
+ this.registerWebSocketHandlers();
53
+
54
+ // Load initial notification counts
55
+ await this.loadNotificationCounts();
56
+
57
+ this.isInitialized = true;
58
+
59
+ } catch (error) {
60
+ console.error("❌ Failed to initialize NotificationService:", error);
61
+ throw error;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Register websocket handlers for real-time notification updates
67
+ */
68
+ private registerWebSocketHandlers(): void {
69
+ // Handler for new private messages
70
+ SocketHelper.addHandler("privateMessage", "NotificationService-PM", (data: any) => {
71
+ try {
72
+ this.debouncedLoadNotificationCounts();
73
+ } catch (error) {
74
+ console.error('❌ NotificationService: Error calling debouncedLoadNotificationCounts:', error);
75
+ }
76
+ });
77
+
78
+ // Handler for general notifications
79
+ SocketHelper.addHandler("notification", "NotificationService-Notification", (data: any) => {
80
+ try {
81
+ this.debouncedLoadNotificationCounts();
82
+ } catch (error) {
83
+ console.error('❌ NotificationService: Error calling debouncedLoadNotificationCounts:', error);
84
+ }
85
+ });
86
+
87
+ // Handler for message updates that could affect notification counts
88
+ SocketHelper.addHandler("message", "NotificationService-MessageUpdate", (data: any) => {
89
+ // Only update counts if the message update involves the current person
90
+ if (data?.message?.personId === this.currentPersonId ||
91
+ data?.notifyPersonId === this.currentPersonId) {
92
+ try {
93
+ this.debouncedLoadNotificationCounts();
94
+ } catch (error) {
95
+ console.error(' NotificationService: Error calling debouncedLoadNotificationCounts:', error);
96
+ }
97
+ }
98
+ });
99
+
100
+ // Handler for reconnect events
101
+ SocketHelper.addHandler("reconnect", "NotificationService-Reconnect", (data: any) => {
102
+ this.loadNotificationCounts(); // Don't debounce reconnect - need immediate update
103
+ });
104
+ }
105
+
106
+ /**
107
+ * Load notification counts from the API with debouncing
108
+ */
109
+ private debouncedLoadNotificationCounts(): void {
110
+
111
+ if (this.loadTimeout) {
112
+ clearTimeout(this.loadTimeout);
113
+ }
114
+
115
+ this.loadTimeout = setTimeout(() => {
116
+ this.loadNotificationCounts();
117
+ }, 300); // 300ms debounce
118
+ }
119
+
120
+ /**
121
+ * Load notification counts from the API
122
+ */
123
+ async loadNotificationCounts(): Promise<void> {
124
+
125
+ try {
126
+ // Use the unreadCount endpoint which returns both notification and PM counts
127
+ const counts = await ApiHelper.get("/notifications/unreadCount", "MessagingApi");
128
+
129
+ const newCounts = {
130
+ notificationCount: counts?.notificationCount || 0,
131
+ pmCount: counts?.pmCount || 0
132
+ };
133
+
134
+
135
+ // Update counts and notify listeners
136
+ this.updateCounts(newCounts);
137
+
138
+ } catch (error) {
139
+ console.error("❌ Failed to load notification counts:", error);
140
+ console.error(" Error details:", {
141
+ message: error.message,
142
+ status: error.status,
143
+ response: error.response
144
+ });
145
+ // Don't throw - just log the error and keep existing counts
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Update counts and notify all listeners
151
+ */
152
+ private updateCounts(newCounts: NotificationCounts): void {
153
+
154
+ const countsChanged =
155
+ this.counts.notificationCount !== newCounts.notificationCount ||
156
+ this.counts.pmCount !== newCounts.pmCount;
157
+
158
+
159
+ if (countsChanged) {
160
+ this.counts = { ...newCounts };
161
+
162
+ // Notify all listeners
163
+ this.listeners.forEach((listener, index) => {
164
+ try {
165
+ listener(this.counts);
166
+ } catch (error) {
167
+ console.error(`❌ Error in notification listener ${index}:`, error);
168
+ }
169
+ });
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Subscribe to notification count changes
175
+ */
176
+ subscribe(listener: (counts: NotificationCounts) => void): () => void {
177
+ this.listeners.push(listener);
178
+
179
+ // Immediately call with current counts
180
+ listener(this.counts);
181
+
182
+ // Return unsubscribe function
183
+ return () => {
184
+ this.listeners = this.listeners.filter(l => l !== listener);
185
+ };
186
+ }
187
+
188
+ /**
189
+ * Get current notification counts
190
+ */
191
+ getCounts(): NotificationCounts {
192
+ return { ...this.counts };
193
+ }
194
+
195
+ /**
196
+ * Manually refresh notification counts
197
+ */
198
+ async refresh(): Promise<void> {
199
+ await this.loadNotificationCounts();
200
+ }
201
+
202
+ /**
203
+ * Cleanup the service
204
+ */
205
+ cleanup(): void {
206
+ // Clear any pending timeout
207
+ if (this.loadTimeout) {
208
+ clearTimeout(this.loadTimeout);
209
+ this.loadTimeout = null;
210
+ }
211
+
212
+ // Remove websocket handlers
213
+ SocketHelper.removeHandler("NotificationService-PM");
214
+ SocketHelper.removeHandler("NotificationService-Notification");
215
+ SocketHelper.removeHandler("NotificationService-MessageUpdate");
216
+ SocketHelper.removeHandler("NotificationService-Reconnect");
217
+
218
+ // Clear listeners
219
+ this.listeners = [];
220
+
221
+ // Reset state
222
+ this.counts = { notificationCount: 0, pmCount: 0 };
223
+ this.currentPersonId = null;
224
+ this.isInitialized = false;
225
+ }
226
+
227
+ /**
228
+ * Check if service is initialized
229
+ */
230
+ isReady(): boolean {
231
+ return this.isInitialized && SocketHelper.isConnected();
232
+ }
297
233
  }