11import { API_BASE_URL } from "./api" ;
2- import React , { useContext , useEffect } from "react" ;
2+ import React , { useContext , useEffect , useState } from "react" ;
33import { BrowserRouter as Router , Routes , Route } from "react-router-dom" ;
44import Dashboard from "./components/Dashboard/Dashboard" ;
55import Login from "./components/Login" ;
@@ -20,6 +20,7 @@ import DescriptionDashboard from "./components/DescriptionDetailPage";
2020import DescriptionBill from "./components/MedicalDescriptions" ;
2121import DoctorDashboard from "./components/Doctor/DoctorDashboard" ;
2222import AppointmentDetail from "./components/Doctor/AppointmentDetail" ; // new detail page
23+ import OfflineAnimation from "./components/OfflineAnimation" ;
2324
2425const App = ( ) => {
2526 const {
@@ -33,6 +34,28 @@ const App = () => {
3334 setDoctor
3435 } = useContext ( Context ) ;
3536
37+ const [ isOnline , setIsOnline ] = useState ( navigator . onLine ) ;
38+
39+ useEffect ( ( ) => {
40+ const handleOnline = ( ) => {
41+ setIsOnline ( true ) ;
42+ // Reload the page to ensure everything is re-initialized correctly
43+ window . location . reload ( ) ;
44+ } ;
45+ const handleOffline = ( ) => {
46+ setIsOnline ( false ) ;
47+ } ;
48+
49+ window . addEventListener ( 'online' , handleOnline ) ;
50+ window . addEventListener ( 'offline' , handleOffline ) ;
51+
52+ // Cleanup listeners on component unmount
53+ return ( ) => {
54+ window . removeEventListener ( 'online' , handleOnline ) ;
55+ window . removeEventListener ( 'offline' , handleOffline ) ;
56+ } ;
57+ } , [ ] ) ;
58+
3659 useEffect ( ( ) => {
3760 const fetchAdmin = async ( ) => {
3861 try {
@@ -62,9 +85,15 @@ const App = () => {
6285 }
6386 } ;
6487
65- fetchAdmin ( ) ;
66- fetchDoctor ( ) ;
67- } , [ isAuthenticated , isDoctorAuthenticated ] ) ;
88+ if ( isOnline ) {
89+ fetchAdmin ( ) ;
90+ fetchDoctor ( ) ;
91+ }
92+ } , [ isOnline , isAuthenticated , isDoctorAuthenticated , setAdmin , setDoctor , setIsAuthenticated , setIsDoctorAuthenticated ] ) ;
93+
94+ if ( ! isOnline ) {
95+ return < OfflineAnimation /> ;
96+ }
6897
6998 return (
7099 < Router >
0 commit comments