11import { useNavigate } from "react-router-dom" ;
22import { apiUserLogout } from "src/api/AuthenticationApi" ;
33import { useAuth } from "src/hooks/AuthHooks" ;
4+ import { axiosInstance } from "src/api/AxiosConfig" ;
5+ import { useEffect , useState } from "react" ;
46
57const settings = {
68 Profile : "/profile" ,
@@ -10,13 +12,33 @@ const settings = {
1012export function NavBar ( ) {
1113 const Navigate = useNavigate ( ) ;
1214 const { isAuthenticated, setIsAuthenticated } = useAuth ( ) ;
15+ const [ profile_pic , setProfilePic ] = useState ( undefined ) ;
1316
1417 const logout = ( ) => {
1518 apiUserLogout ( ) ;
1619 setIsAuthenticated ( false ) ;
1720 Navigate ( "/" ) ;
1821 } ;
1922
23+ useEffect ( ( ) => {
24+ const fetchUser = async ( ) => {
25+ if ( isAuthenticated ) {
26+ try {
27+ const response = await axiosInstance . get ( "/user/data/" ) ;
28+ const fetchedProfilePic = response . data . profile_pic ;
29+ setProfilePic ( fetchedProfilePic ) ;
30+ } catch ( error ) {
31+ console . error ( "Error fetching user:" , error ) ;
32+ }
33+ } else {
34+ setProfilePic (
35+ "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png"
36+ ) ;
37+ }
38+ } ;
39+ fetchUser ( ) ;
40+ } , [ ] ) ;
41+
2042 return (
2143 < div data-theme = "night" className = "navbar bg-base-100" >
2244 < div className = "flex-1" >
@@ -32,14 +54,18 @@ export function NavBar() {
3254 < div className = "dropdown dropdown-end" >
3355 < label tabIndex = { 0 } className = "btn btn-ghost btn-circle avatar" >
3456 < div className = "w-10 rounded-full" >
35- < img src = "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png" />
57+ < img src = { profile_pic } />
3658 </ div >
3759 </ label >
3860 < ul
3961 tabIndex = { 0 }
40- className = "mt-3 z-[10] p-2 shadow menu menu-sm dropdown-content bg-base-100 rounded-box w-52" >
62+ className = "mt-3 z-[10] p-2 shadow menu menu-sm dropdown-content bg-base-100 rounded-box w-52"
63+ >
4164 < li >
42- < a onClick = { ( ) => Navigate ( settings . Profile ) } className = "justify-between" >
65+ < a
66+ onClick = { ( ) => Navigate ( settings . Profile ) }
67+ className = "justify-between"
68+ >
4369 Profile
4470 </ a >
4571 </ li >
@@ -53,10 +79,16 @@ export function NavBar() {
5379 </ div >
5480 ) : (
5581 < div className = "flex gap-2" >
56- < button className = "btn btn-outline btn-info" onClick = { ( ) => Navigate ( "/login" ) } >
82+ < button
83+ className = "btn btn-outline btn-info"
84+ onClick = { ( ) => Navigate ( "/login" ) }
85+ >
5786 Login
5887 </ button >
59- < button className = "btn btn-success" onClick = { ( ) => Navigate ( "/signup" ) } >
88+ < button
89+ className = "btn btn-success"
90+ onClick = { ( ) => Navigate ( "/signup" ) }
91+ >
6092 Sign Up
6193 </ button >
6294 </ div >
0 commit comments