diff --git a/frontend/src/components/NotificationsList.tsx b/frontend/src/components/NotificationsList.tsx index b341b89..2b7ae6d 100644 --- a/frontend/src/components/NotificationsList.tsx +++ b/frontend/src/components/NotificationsList.tsx @@ -1,25 +1,38 @@ +import { Spinner } from "react-activity"; import useNotifications from "../hooks/useNotifications"; import NotificationCard from "./NotificationCard"; const NotificationsList = () => { - const { notifications, haveNotification } = useNotifications(); + const { notifications, haveNotification, loading } = useNotifications(); return ( -
+

Notifications

-
+
{haveNotification ? ( notifications?.map((notification) => ( - +
+ +
)) ) : (

- No Notifications + {loading ? ( + + ) : ( + "No Notifications" + )}

)}
diff --git a/frontend/src/hooks/useNotifications.ts b/frontend/src/hooks/useNotifications.ts index d819d09..1f74820 100644 --- a/frontend/src/hooks/useNotifications.ts +++ b/frontend/src/hooks/useNotifications.ts @@ -7,32 +7,40 @@ const useNotifications = () => { const [notifications, setNotifications] = useState(); const [haveNotification, setHaveNotification] = useState(false); const { auth, navigate, refresh } = useFetchWithAuthExports(); + const [loading, setLoading] = useState(); useEffect(() => { + setLoading(true); const getNotifications = async () => { - const response = await fetchWithAuth( - navigate, - refresh, - auth, - "/notification/get-by-user", - "get" - ); - - if (response.status === 404) { - setHaveNotification(false); - return; + try { + const response = await fetchWithAuth( + navigate, + refresh, + auth, + "/notification/get-by-user", + "get" + ); + + if (response.status === 404) { + setHaveNotification(false); + return; + } + + setHaveNotification(true); + + const noticationsAPI = await response.json(); + setNotifications(noticationsAPI); + } catch (error) { + alert(error); + } finally { + setLoading(false); } - - setHaveNotification(true); - - const noticationsAPI = await response.json(); - setNotifications(noticationsAPI); }; getNotifications(); }, []); - return { notifications, haveNotification }; + return { notifications, haveNotification, loading }; }; export default useNotifications; diff --git a/frontend/src/pages/HomePage.tsx b/frontend/src/pages/HomePage.tsx index 026d10b..5f992cf 100644 --- a/frontend/src/pages/HomePage.tsx +++ b/frontend/src/pages/HomePage.tsx @@ -2,17 +2,18 @@ import Header from "../components/Header"; import { useNavigate } from "react-router-dom"; import useUser from "../hooks/useUser"; import NotificationsList from "../components/NotificationsList"; +import Loading from "../components/Loading"; const HomePage = () => { const navigate = useNavigate(); - const { data: data } = useUser(); - - console.log(data); + const { data: data, loading } = useUser(); const handleRegisterButton = () => { navigate("/register-driver"); }; - + if (loading) { + return ; + } return (