1- import Head from ' next/head' ;
2- import { useEffect , useState } from ' react' ;
3- import { useRouter } from ' next/router' ;
4- import { Box , Container , CircularProgress } from ' @mui/material' ;
5- import { useAuth } from ' @/context/AuthContext' ;
6- import api from ' @/lib/api' ;
7- import Footer from ' @/components/Footer' ;
8- import ProfileHeader from ' @/components/profile/ProfileHeader' ;
9- import AccountInformationPanel from ' @/components/profile/AccountInformationPanel' ;
10- import LogoutButton from ' @/components/profile/LogoutButton' ;
11- import FavoritesSection from ' @/components/profile/FavoritesSection' ;
1+ import Head from " next/head" ;
2+ import { useEffect , useState } from " react" ;
3+ import { useRouter } from " next/router" ;
4+ import { Box , Container , CircularProgress } from " @mui/material" ;
5+ import { useAuth } from " @/context/AuthContext" ;
6+ import api from " @/lib/api" ;
7+ import Footer from " @/components/Footer" ;
8+ import ProfileHeader from " @/components/profile/ProfileHeader" ;
9+ import AccountInformationPanel from " @/components/profile/AccountInformationPanel" ;
10+ import LogoutButton from " @/components/profile/LogoutButton" ;
11+ import FavoritesSection from " @/components/profile/FavoritesSection" ;
1212
1313export default function Profile ( ) {
1414 const { isAuthenticated, loading : authLoading , logout } = useAuth ( ) ;
1515 const router = useRouter ( ) ;
1616 const [ user , setUser ] = useState ( null ) ;
1717 const [ loading , setLoading ] = useState ( true ) ;
18- const [ error , setError ] = useState ( '' ) ;
18+ const [ error , setError ] = useState ( "" ) ;
1919 const [ favorites , setFavorites ] = useState ( [ ] ) ;
2020
2121 useEffect ( ( ) => {
2222 if ( ! authLoading && ! isAuthenticated ) {
23- router . push ( '/' ) ;
23+ router . push ( "/" ) ;
2424 }
2525 } , [ isAuthenticated , authLoading , router ] ) ;
2626
2727 useEffect ( ( ) => {
2828 const fetchUser = async ( ) => {
2929 try {
3030 setLoading ( true ) ;
31- const response = await api . get ( '/api/v1/current/user' ) ;
32- setUser ( response . data . user ) ;
31+ const response = await api . get ( "/api/v1/current/user" ) ;
32+ const userData = response . data . user ;
33+ setUser ( userData ) ;
34+ if ( userData . preferences ?. favoriteMedia ) {
35+ setFavorites ( userData . preferences . favoriteMedia ) ;
36+ }
3337 } catch ( error ) {
34- console . error ( ' Error fetching user:' , error ) ;
35- setError ( ' Failed to load user profile' ) ;
38+ console . error ( " Error fetching user:" , error ) ;
39+ setError ( " Failed to load user profile" ) ;
3640 } finally {
3741 setLoading ( false ) ;
3842 }
@@ -48,22 +52,30 @@ export default function Profile() {
4852 // Refresh user data when page becomes visible (e.g., returning from subscription page)
4953 useEffect ( ( ) => {
5054 const handleVisibilityChange = ( ) => {
51- if ( document . visibilityState === 'visible' && isAuthenticated && ! loading ) {
55+ if (
56+ document . visibilityState === "visible" &&
57+ isAuthenticated &&
58+ ! loading
59+ ) {
5260 const fetchUser = async ( ) => {
5361 try {
54- const response = await api . get ( '/api/v1/current/user' ) ;
55- setUser ( response . data . user ) ;
62+ const response = await api . get ( "/api/v1/current/user" ) ;
63+ const userData = response . data . user ;
64+ setUser ( userData ) ;
65+ if ( userData . preferences ?. favoriteMedia ) {
66+ setFavorites ( userData . preferences . favoriteMedia ) ;
67+ }
5668 } catch ( error ) {
57- console . error ( ' Error refreshing user:' , error ) ;
69+ console . error ( " Error refreshing user:" , error ) ;
5870 }
5971 } ;
6072 fetchUser ( ) ;
6173 }
6274 } ;
6375
64- document . addEventListener ( ' visibilitychange' , handleVisibilityChange ) ;
76+ document . addEventListener ( " visibilitychange" , handleVisibilityChange ) ;
6577 return ( ) => {
66- document . removeEventListener ( ' visibilitychange' , handleVisibilityChange ) ;
78+ document . removeEventListener ( " visibilitychange" , handleVisibilityChange ) ;
6779 } ;
6880 } , [ isAuthenticated , loading ] ) ;
6981
@@ -73,23 +85,23 @@ export default function Profile() {
7385 // await api.put(`/api/v1/update/user/${user._id}`, updatedUser);
7486 setUser ( { ...user , ...updatedUser } ) ;
7587 } catch ( error ) {
76- console . error ( ' Error updating user:' , error ) ;
77- setError ( ' Failed to update profile' ) ;
88+ console . error ( " Error updating user:" , error ) ;
89+ setError ( " Failed to update profile" ) ;
7890 }
7991 } ;
8092
8193 if ( authLoading || loading ) {
8294 return (
8395 < Box
8496 sx = { {
85- minHeight : ' 100vh' ,
86- display : ' flex' ,
87- alignItems : ' center' ,
88- justifyContent : ' center' ,
89- backgroundColor : ' #000' ,
97+ minHeight : " 100vh" ,
98+ display : " flex" ,
99+ alignItems : " center" ,
100+ justifyContent : " center" ,
101+ backgroundColor : " #000" ,
90102 } }
91103 >
92- < CircularProgress sx = { { color : ' #ffd700' } } />
104+ < CircularProgress sx = { { color : " #ffd700" } } />
93105 </ Box >
94106 ) ;
95107 }
@@ -107,29 +119,29 @@ export default function Profile() {
107119
108120 < Box
109121 sx = { {
110- minHeight : ' 100vh' ,
111- backgroundColor : ' #000' ,
122+ minHeight : " 100vh" ,
123+ backgroundColor : " #000" ,
112124 pt : { xs : 10 , md : 12 } ,
113125 pb : 8 ,
114- position : ' relative' ,
126+ position : " relative" ,
115127 backgroundImage : 'url("/hero_bg.jpg")' ,
116- backgroundSize : ' cover' ,
117- backgroundPosition : ' center' ,
118- backgroundRepeat : ' no-repeat' ,
119- backgroundAttachment : ' fixed' ,
120- ' &::before' : {
128+ backgroundSize : " cover" ,
129+ backgroundPosition : " center" ,
130+ backgroundRepeat : " no-repeat" ,
131+ backgroundAttachment : " fixed" ,
132+ " &::before" : {
121133 content : '""' ,
122- position : ' absolute' ,
134+ position : " absolute" ,
123135 top : 0 ,
124136 left : 0 ,
125137 right : 0 ,
126138 bottom : 0 ,
127- backgroundColor : ' rgba(0, 0, 0, 0.3)' ,
139+ backgroundColor : " rgba(0, 0, 0, 0.3)" ,
128140 zIndex : 0 ,
129141 } ,
130142 } }
131143 >
132- < Container maxWidth = "lg" sx = { { position : ' relative' , zIndex : 1 } } >
144+ < Container maxWidth = "lg" sx = { { position : " relative" , zIndex : 1 } } >
133145 < ProfileHeader user = { user } />
134146
135147 < AccountInformationPanel user = { user } onUpdate = { handleUpdateUser } />
0 commit comments