Changeset 73b69b2 for chapterx-frontend/src/store/notificationStore.ts
- Timestamp:
- 03/24/26 22:13:36 (3 months ago)
- Branches:
- main
- Children:
- 7fbb91c
- Parents:
- acf690c
- File:
-
- 1 edited
-
chapterx-frontend/src/store/notificationStore.ts (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
chapterx-frontend/src/store/notificationStore.ts
racf690c r73b69b2 1 1 import { create } from 'zustand' 2 import axios from 'axios' 2 3 import { Notification } from '../types' 3 import { mockNotifications } from '../data/mockData' 4 5 const API = 'https://localhost:7125/api' 6 7 function getAuthHeaders() { 8 try { 9 const token = JSON.parse(localStorage.getItem('chapterx-auth') || '{}')?.state?.token 10 return token ? { Authorization: `Bearer ${token}` } : {} 11 } catch { 12 return {} 13 } 14 } 4 15 5 16 interface NotificationStore { 6 17 notifications: Notification[] 7 addNotification: (n: Omit<Notification, 'notification_id' | 'created_at' | 'is_read'>) => void 8 markAllRead: () => void 9 markRead: (id: number) => void 18 fetchUserNotifications: (userId: number) => Promise<void> 19 addNotification: (n: { recipientUserId: number; type: string; content: string; link?: string }) => Promise<void> 20 markAllRead: () => Promise<void> 21 markRead: (id: number) => Promise<void> 10 22 getUnreadCount: () => number 11 23 } 12 24 13 25 export const useNotificationStore = create<NotificationStore>((set, get) => ({ 14 notifications: [ ...mockNotifications],26 notifications: [], 15 27 16 addNotification: (n) => 17 set(state => ({ 18 notifications: [ 19 { 20 ...n, 21 notification_id: Date.now(), 22 created_at: new Date().toISOString(), 23 is_read: false, 24 }, 25 ...state.notifications, 26 ], 27 })), 28 fetchUserNotifications: async (userId) => { 29 try { 30 const res = await axios.get(`${API}/notifications/user/${userId}`, { headers: getAuthHeaders() }) 31 const data: any[] = res.data ?? [] 32 const notifications: Notification[] = data.map(n => ({ 33 notification_id: n.id, 34 user_id: userId, 35 type: n.type ?? 'info', 36 title: n.type ?? 'Notification', 37 message: n.content, 38 link: n.link, 39 is_read: n.isRead, 40 created_at: n.createdAt, 41 })) 42 set({ notifications }) 43 } catch { 44 // keep existing 45 } 46 }, 28 47 29 markAllRead: () => 30 set(state => ({ 31 notifications: state.notifications.map(n => ({ ...n, is_read: true })), 32 })), 48 addNotification: async ({ recipientUserId, type, content, link }) => { 49 try { 50 await axios.post(`${API}/notifications`, { 51 content, 52 recipientUserId, 53 type, 54 link, 55 }, { headers: getAuthHeaders() }) 56 } catch { 57 // silent — notification is for recipient, not current user 58 } 59 }, 33 60 34 markRead: (id) => 61 markAllRead: async () => { 62 const unread = get().notifications.filter(n => !n.is_read) 63 set(state => ({ notifications: state.notifications.map(n => ({ ...n, is_read: true })) })) 64 try { 65 await Promise.all(unread.map(n => 66 axios.put(`${API}/notifications/${n.notification_id}/read`, {}, { headers: getAuthHeaders() }) 67 )) 68 } catch { 69 // keep optimistic 70 } 71 }, 72 73 markRead: async (id) => { 35 74 set(state => ({ 36 75 notifications: state.notifications.map(n => 37 76 n.notification_id === id ? { ...n, is_read: true } : n 38 77 ), 39 })), 78 })) 79 try { 80 await axios.put(`${API}/notifications/${id}/read`, {}, { headers: getAuthHeaders() }) 81 } catch { 82 // keep optimistic 83 } 84 }, 40 85 41 86 getUnreadCount: () => get().notifications.filter(n => !n.is_read).length,
Note:
See TracChangeset
for help on using the changeset viewer.
