File size: 4,780 Bytes
ab0678b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { create } from 'zustand';
import * as api from '../services/api';
import { socket } from '../services/socket';

export type AppNotification = {
  id: string | number;
  type: 'order' | 'offer' | 'system' | 'wallet';
  icon: string;
  title: string;
  description: string;
  time: string;
  isUnread: boolean;
  color?: string;
  link?: string;
  createdAt?: string;
};

interface NotificationState {
  notifications: AppNotification[];
  unreadCount: number;
  isLoading: boolean;
  fetchNotifications: () => Promise<void>;
  markAsRead: (id: string | number) => Promise<void>;
  markAllAsRead: () => Promise<void>;
  deleteNotification: (id: string | number) => Promise<void>;
  clearAll: () => Promise<void>;
  // Socket.io integration
  initSocket: (userId: string | number) => void;
  cleanupSocket: (userId: string | number) => void;
  // For optimistic updates or local-only notifications if needed
  addLocalNotification: (notification: any) => void;
}

export const useNotificationStore = create<NotificationState>((set) => ({
  notifications: [],
  unreadCount: 0,
  isLoading: false,

  initSocket: (userId: string | number) => {
    const eventName = `user_notification_${userId}`;
    
    // Remove existing listener if any to avoid duplicates
    socket.off(eventName);
    
    socket.on(eventName, (newNotif: any) => {
      const formattedNotif: AppNotification = {
        id: newNotif.id,
        type: newNotif.type,
        icon: newNotif.icon,
        title: newNotif.title,
        description: newNotif.description,
        time: new Date(newNotif.createdAt).toLocaleTimeString('ar-IQ', { hour: '2-digit', minute: '2-digit' }),
        isUnread: true,
        color: newNotif.color,
        link: newNotif.link,
        createdAt: newNotif.createdAt
      };

      set((state) => ({
        notifications: [formattedNotif, ...state.notifications],
        unreadCount: state.unreadCount + 1
      }));
    });
  },

  cleanupSocket: (userId: string | number) => {
    socket.off(`user_notification_${userId}`);
  },

  fetchNotifications: async () => {
    set({ isLoading: true });
    try {
      const data = await api.fetchUserNotifications();
      const notifications = data.map((n: any) => ({
        id: n.id,
        type: n.type,
        icon: n.icon,
        title: n.title,
        description: n.description,
        time: new Date(n.createdAt).toLocaleTimeString('ar-IQ', { hour: '2-digit', minute: '2-digit' }),
        isUnread: !n.isRead,
        color: n.color,
        link: n.link,
        createdAt: n.createdAt
      }));
      const unreadCount = notifications.filter((n: any) => n.isUnread).length;
      set({ notifications, unreadCount, isLoading: false });
    } catch (error) {
      console.error('Failed to fetch notifications:', error);
      set({ isLoading: false });
    }
  },

  markAsRead: async (id: string | number) => {
    try {
      await api.markUserNotificationAsRead(id);
      set((state) => {
        const updated = state.notifications.map((n) =>
          n.id === id ? { ...n, isUnread: false } : n
        );
        return {
          notifications: updated,
          unreadCount: Math.max(0, state.unreadCount - 1)
        };
      });
    } catch (error) {
      console.error('Failed to mark notification as read:', error);
    }
  },

  markAllAsRead: async () => {
    try {
      await api.markAllUserNotificationsAsRead();
      set((state) => ({
        notifications: state.notifications.map((n) => ({ ...n, isUnread: false })),
        unreadCount: 0
      }));
    } catch (error) {
      console.error('Failed to mark all notifications as read:', error);
    }
  },

  deleteNotification: async (id: string | number) => {
    try {
      await api.deleteUserNotification(id);
      set((state) => {
        const n = state.notifications.find((notif) => notif.id === id);
        return {
          notifications: state.notifications.filter((notif) => notif.id !== id),
          unreadCount: n?.isUnread ? Math.max(0, state.unreadCount - 1) : state.unreadCount
        };
      });
    } catch (error) {
      console.error('Failed to delete notification:', error);
    }
  },

  clearAll: async () => {
    try {
      await api.clearAllUserNotifications();
      set({ notifications: [], unreadCount: 0 });
    } catch (error) {
      console.error('Failed to clear notifications:', error);
    }
  },

  addLocalNotification: (notification) => {
    const newNotification: AppNotification = {
      ...notification,
      id: `local-${Math.random().toString(36).substring(7)}`,
      isUnread: true,
      time: 'الآن',
    };
    set((state) => ({
      notifications: [newNotification, ...state.notifications],
      unreadCount: state.unreadCount + 1,
    }));
  },
}));