Ignore:
Timestamp:
03/24/26 22:13:36 (3 months ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
7fbb91c
Parents:
acf690c
Message:

Fixed reading lists,comments and likes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chapterx-frontend/src/store/notificationStore.ts

    racf690c r73b69b2  
    11import { create } from 'zustand'
     2import axios from 'axios'
    23import { Notification } from '../types'
    3 import { mockNotifications } from '../data/mockData'
     4
     5const API = 'https://localhost:7125/api'
     6
     7function 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}
    415
    516interface NotificationStore {
    617  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>
    1022  getUnreadCount: () => number
    1123}
    1224
    1325export const useNotificationStore = create<NotificationStore>((set, get) => ({
    14   notifications: [...mockNotifications],
     26  notifications: [],
    1527
    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  },
    2847
    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  },
    3360
    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) => {
    3574    set(state => ({
    3675      notifications: state.notifications.map(n =>
    3776        n.notification_id === id ? { ...n, is_read: true } : n
    3877      ),
    39     })),
     78    }))
     79    try {
     80      await axios.put(`${API}/notifications/${id}/read`, {}, { headers: getAuthHeaders() })
     81    } catch {
     82      // keep optimistic
     83    }
     84  },
    4085
    4186  getUnreadCount: () => get().notifications.filter(n => !n.is_read).length,
Note: See TracChangeset for help on using the changeset viewer.