UNPKG

nexus-react-core

Version:

A comprehensive React toolkit with services, hooks, and Redux store management

169 lines 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNotifications = void 0; const react_1 = require("react"); const apiCall_1 = require("./apiCall"); const useNotifications = (router) => { const [notifications, setNotifications] = (0, react_1.useState)([]); const [unreadCount, setUnreadCount] = (0, react_1.useState)(0); // Default notifications when API is not available const defaultNotifications = [ { id: "welcome", type: "welcome", message: "Welcome to D-GN! Get ready for an exciting adventure. Complete missions, earn rewards, and have fun!", isRead: false, createdAt: new Date().toISOString(), }, { id: "mission", type: "mission", message: "Complete your first mission to earn rewards!", isRead: false, createdAt: new Date().toISOString(), }, { id: "achievement-1", type: "Achievement", message: 'Congratulations! You\'ve earned the "Early Bird" achievement for being one of our first users!', isRead: false, createdAt: new Date(Date.now() - 3600000).toISOString(), // 1 hour ago data: { achievementName: "Early Bird", timestamp: new Date(Date.now() - 3600000).toISOString(), }, }, ]; // Fetch notifications from the API const fetchNotifications = async () => { try { const response = await (0, apiCall_1.apiCall)("/notifications", "GET"); if (response && Array.isArray(response)) { setNotifications(response); const unread = response.filter((notif) => !notif.isRead).length; setUnreadCount(unread); } else { console.log("API response not in expected format, using default notifications"); setNotifications(defaultNotifications); setUnreadCount(defaultNotifications.filter((notif) => !notif.isRead).length); } } catch (error) { console.log("Failed to fetch notifications, using defaults:", error.message); setNotifications(defaultNotifications); setUnreadCount(defaultNotifications.filter((notif) => !notif.isRead).length); } }; // Mark notification as read const markAsRead = async (notificationId) => { try { await (0, apiCall_1.apiCall)(`/notifications/${notificationId}/read`, "PUT"); setNotifications((prev) => prev.map((notif) => notif.id === notificationId ? { ...notif, isRead: true } : notif)); setUnreadCount((prev) => Math.max(0, prev - 1)); } catch (error) { console.error("Failed to mark notification as read:", error); // Update locally even if API call fails setNotifications((prev) => prev.map((notif) => notif.id === notificationId ? { ...notif, isRead: true } : notif)); setUnreadCount((prev) => Math.max(0, prev - 1)); } }; // Mark all notifications as read const markAllAsRead = async () => { try { await (0, apiCall_1.apiCall)("/notifications/read-all", "PUT"); setNotifications((prev) => prev.map((notif) => ({ ...notif, isRead: true }))); setUnreadCount(0); } catch (error) { console.error("Failed to mark all notifications as read:", error); // Update locally even if API call fails setNotifications((prev) => prev.map((notif) => ({ ...notif, isRead: true }))); setUnreadCount(0); } }; // Handle notification click (navigate based on type) const handleNotificationClick = (notification) => { // Mark as read first if (!notification.isRead) { markAsRead(notification.id); } // Navigate based on notification type if (router) { switch (notification.type) { case "mission": router.push("/missions"); break; case "achievement": router.push("/achievements"); break; case "reward": router.push("/rewards"); break; case "social": router.push("/social"); break; case "system": // Stay on current page for system notifications break; default: // Default behavior - you can customize this break; } } }; // Clear notification const clearNotification = async (notificationId) => { try { await (0, apiCall_1.apiCall)(`/notifications/${notificationId}`, "DELETE"); setNotifications((prev) => { const updated = prev.filter((notif) => notif.id !== notificationId); const unread = updated.filter((notif) => !notif.isRead).length; setUnreadCount(unread); return updated; }); } catch (error) { console.error("Failed to clear notification:", error); // Update locally even if API call fails setNotifications((prev) => { const updated = prev.filter((notif) => notif.id !== notificationId); const unread = updated.filter((notif) => !notif.isRead).length; setUnreadCount(unread); return updated; }); } }; // Clear all notifications const clearAllNotifications = async () => { try { await (0, apiCall_1.apiCall)("/notifications", "DELETE"); setNotifications([]); setUnreadCount(0); } catch (error) { console.error("Failed to clear all notifications:", error); // Update locally even if API call fails setNotifications([]); setUnreadCount(0); } }; // Initialize notifications on mount (0, react_1.useEffect)(() => { fetchNotifications(); }, []); return { notifications, unreadCount, fetchNotifications, markAsRead, markAllAsRead, handleNotificationClick, clearNotification, clearAllNotifications, refreshNotifications: fetchNotifications, }; }; exports.useNotifications = useNotifications; //# sourceMappingURL=useNotifications.js.map