diff --git a/src/components/notifications/Notification.js b/src/components/notifications/Notification.js index cd22a18..72f98ad 100644 --- a/src/components/notifications/Notification.js +++ b/src/components/notifications/Notification.js @@ -1,7 +1,66 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; const Notifications = () => { - return
; + const [notifications, setNotifications] = useState([]); + + useEffect(() => { + // fetch notifications from backend + try { + const userString = localStorage.getItem("user"); // Change the key to 'user' + console.log("User String:", userString); // Debugging + if (!userString) { + console.error("No user data found"); + return; + } + + const user = JSON.parse(userString); + const token = user.token; + console.log("Token:", token); // Debugging + + fetch(`http://localhost:5005/api/notifications/`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(user.userId), + }) + .then((response) => response.json()) + .then((data) => { + console.log("Notifications:", data.notifications); + setNotifications(data.notifications); + }) + .catch((err) => console.error("Error fetching notifications:", err)); + } catch (err) { + console.error("Error:", err); + } + }, []); + + return ( +
+ {(!notifications || notifications.length) === 0 ? ( +

No new notifications

+ ) : ( + notifications && + notifications.map((notification, index) => ( +
+
+ + Parcel Arrived + + + {" "} + {notification.message} + +
+
+ )) + )} +
+ ); }; -export default Notifications; +export default Notifications; \ No newline at end of file