source:
chapterx-frontend/src/store/notificationStore.ts@
acf690c
| Last change on this file since acf690c was b62cefc, checked in by , 4 months ago | |
|---|---|
|
|
| File size: 1.1 KB | |
| Line | |
|---|---|
| 1 | import { create } from 'zustand' |
| 2 | import { Notification } from '../types' |
| 3 | import { mockNotifications } from '../data/mockData' |
| 4 | |
| 5 | interface NotificationStore { |
| 6 | notifications: Notification[] |
| 7 | addNotification: (n: Omit<Notification, 'notification_id' | 'created_at' | 'is_read'>) => void |
| 8 | markAllRead: () => void |
| 9 | markRead: (id: number) => void |
| 10 | getUnreadCount: () => number |
| 11 | } |
| 12 | |
| 13 | export const useNotificationStore = create<NotificationStore>((set, get) => ({ |
| 14 | notifications: [...mockNotifications], |
| 15 | |
| 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 | |
| 29 | markAllRead: () => |
| 30 | set(state => ({ |
| 31 | notifications: state.notifications.map(n => ({ ...n, is_read: true })), |
| 32 | })), |
| 33 | |
| 34 | markRead: (id) => |
| 35 | set(state => ({ |
| 36 | notifications: state.notifications.map(n => |
| 37 | n.notification_id === id ? { ...n, is_read: true } : n |
| 38 | ), |
| 39 | })), |
| 40 | |
| 41 | getUnreadCount: () => get().notifications.filter(n => !n.is_read).length, |
| 42 | })) |
Note:
See TracBrowser
for help on using the repository browser.
