| [b62cefc] | 1 | import { create } from 'zustand'
|
|---|
| [73b69b2] | 2 | import axios from 'axios'
|
|---|
| [b62cefc] | 3 | import { Notification } from '../types'
|
|---|
| [73b69b2] | 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 | }
|
|---|
| [b62cefc] | 15 |
|
|---|
| 16 | interface NotificationStore {
|
|---|
| 17 | notifications: Notification[]
|
|---|
| [73b69b2] | 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>
|
|---|
| [b62cefc] | 22 | getUnreadCount: () => number
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | export const useNotificationStore = create<NotificationStore>((set, get) => ({
|
|---|
| [73b69b2] | 26 | notifications: [],
|
|---|
| [b62cefc] | 27 |
|
|---|
| [73b69b2] | 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 | },
|
|---|
| 47 |
|
|---|
| 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 | },
|
|---|
| 60 |
|
|---|
| 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 | },
|
|---|
| [b62cefc] | 72 |
|
|---|
| [73b69b2] | 73 | markRead: async (id) => {
|
|---|
| [b62cefc] | 74 | set(state => ({
|
|---|
| 75 | notifications: state.notifications.map(n =>
|
|---|
| 76 | n.notification_id === id ? { ...n, is_read: true } : n
|
|---|
| 77 | ),
|
|---|
| [73b69b2] | 78 | }))
|
|---|
| 79 | try {
|
|---|
| 80 | await axios.put(`${API}/notifications/${id}/read`, {}, { headers: getAuthHeaders() })
|
|---|
| 81 | } catch {
|
|---|
| 82 | // keep optimistic
|
|---|
| 83 | }
|
|---|
| 84 | },
|
|---|
| [b62cefc] | 85 |
|
|---|
| 86 | getUnreadCount: () => get().notifications.filter(n => !n.is_read).length,
|
|---|
| 87 | }))
|
|---|