Advertisement
Guest User

Untitled

a guest
Apr 11th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import { collection, doc, onSnapshot, query, writeBatch } from "firebase/firestore";
  2. import { db } from "../../firebase/clientFirebaseApps";
  3.  
  4. export default function Notifications() {
  5. const { user } = useUser();
  6. const [notifications, setNotifications] = useState<P6Notification[]>([]);
  7. useEffect(() => {
  8. if (user) {
  9. const q = query(collection(db, "notifications"));
  10. const unsubscribe = onSnapshot(q, (querySnapshot) => {
  11. const ar: P6Notification[] = [];
  12. querySnapshot.docs.forEach((doc) => {
  13. ar.push({ id: doc.id, ...doc.data() } as P6Notification);
  14. });
  15. setNotifications(ar);
  16. });
  17.  
  18. return () => unsubscribe();
  19. } else {
  20. setNotifications([]);
  21. }
  22. }, [user]);
  23.  
  24. const handleMarkAllRead = async () => {
  25. // Remove the userId from the userIds array of all notifications
  26. const batch = writeBatch(db);
  27.  
  28. notifications.forEach((notification) => {
  29. const notificationRef = doc(db, "notifications", notification.id);
  30. batch.delete(notificationRef);
  31. });
  32.  
  33. await batch.commit();
  34.  
  35. // Update the state to remove the notifications that no longer have the userId
  36. setNotifications([]);
  37. };
  38. console.log(notifications)
  39. return (
  40. <div className="flex-auto">
  41. <div className="text-white">
  42. <div className="mt-5 bg-component text-white">
  43. <div className="flex items-end px-2.5 pt-5">
  44. <div className="flex flex-auto text-[24px] leading-7">Notifications</div>
  45. <div
  46. style={{ cursor: "pointer" }}
  47. className="text-[15px] leading-7 text-p6blue"
  48. onClick={handleMarkAllRead}
  49. >
  50. Mark All Read
  51. </div>
  52. </div>
  53. {notifications.map((notification) => (
  54. <Item key={notification.id} data={notification} />
  55. ))}
  56. </div>
  57. </div>
  58. </div>
  59. );
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement