From 776f680ca58d066e24284b009eb9a28ced03a6ea Mon Sep 17 00:00:00 2001
From: omagdy7 <omar.professional8777@gmail.com>
Date: Fri, 22 Dec 2023 23:10:26 +0200
Subject: Made the Driver dashboard respond to requests at realtime

---
 driver/src/pages/Home.tsx | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

(limited to 'driver/src/pages/Home.tsx')

diff --git a/driver/src/pages/Home.tsx b/driver/src/pages/Home.tsx
index 37727e7..bd003b6 100644
--- a/driver/src/pages/Home.tsx
+++ b/driver/src/pages/Home.tsx
@@ -9,6 +9,7 @@ import { Toaster } from "@/components/ui/toaster"
 import { useEffect, useState } from "react"
 import { Navigate } from "react-router-dom"
 import { fetchRideRequests } from "@/utils/fetchRideRequests"
+import { collection, onSnapshot } from "firebase/firestore"
 
 interface IDriver {
   uid: string,
@@ -93,12 +94,19 @@ export default function Home() {
     async function fetchData() {
       const data: IDriver | null | undefined = await fetchUserDetails(user?.uid);
       const rideReqs = await fetchRideRequests()
-      console.log("RideRequests:", rideReqs)
       setDriverData(data)
       setRideRequests(rideReqs)
-      console.log("Length: ", rideRequests?.length)
     }
     fetchData()
+    // Firestore real-time listener for changes
+    const unsubscribe = onSnapshot(collection(db, "RideRequest"), () => {
+      // Call fetchData when changes occur in the specified collection
+      fetchData();
+    });
+
+    return () => {
+      unsubscribe()
+    }
   }, [auth.currentUser, db]);
 
   return (
@@ -117,7 +125,6 @@ export default function Home() {
             <div className="flex gap-4 items-center">
               <Button onClick={
                 () => {
-                  console.log("Loging out")
                   setIsLoggedIn(false)
                   auth.signOut()
                 }
-- 
cgit v1.2.3

