Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhilgholap1304 committed May 29, 2024
1 parent 4f42453 commit 7ff81ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
Route,
useNavigate,
} from "react-router-dom";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { AuthContextProvider, useAuth } from "./context/AuthContext";
import checkInterConnection from "./utils/checkInternetConnection";
import Login from "./components/Login";
import Dashboard from "./components/Dashboard";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

const AuthRedirect = () => {
const { currentUid } = useAuth();
Expand Down Expand Up @@ -47,6 +48,9 @@ const App = () => {
/>
<Route path="/" element={<AuthRedirect />} />
</Routes>

{checkInterConnection()}

<ToastContainer
position="bottom-right"
autoClose={3000}
Expand All @@ -61,6 +65,7 @@ const App = () => {
transition:Bounce
/>
</AuthContextProvider>

</Router>
);
};
Expand Down
34 changes: 34 additions & 0 deletions src/utils/checkInternetConnection.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useEffect, useState } from "react";
import { toast } from "react-toastify";

const ErrorToast = ({ message }) => {
toast.error(message);
return null; // Returning null avoids unnecessary rendering
};

const checkInternetConnection = () => {
const [isConnected, setIsConnected] = useState(navigator.onLine);
const [prevConnected, setPrevConnected] = useState(navigator.onLine);

useEffect(() => {
const handleOnlineEvent = () => setIsConnected(true);
const handleOfflineEvent = () => setIsConnected(false);

window.addEventListener("online", handleOnlineEvent);
window.addEventListener("offline", handleOfflineEvent);

return () => {
window.removeEventListener("online", handleOnlineEvent);
window.removeEventListener("offline", handleOfflineEvent);
};
}, []);

if (!isConnected) {
return <ErrorToast message="No internet connection" />;
}


return null;
};

export default checkInternetConnection;

0 comments on commit 7ff81ec

Please sign in to comment.