1
- import { useCallback , useContext , useEffect , useRef , useState } from "react" ;
2
- import { useNavigate , useParams , useLocation } from "@remix-run/react" ;
1
+ import { useCallback , useEffect , useState } from "react" ;
2
+ import { useNavigate , useParams , useLocation , useLoaderData } from "@remix-run/react" ;
3
3
import {
4
4
Edit as EditIcon ,
5
5
Save as SaveIcon ,
@@ -22,12 +22,11 @@ import {
22
22
Link as MUILink
23
23
} from '@mui/material' ;
24
24
25
- import UserContext from "./contexts/UserContext" ;
26
25
import { REDIRECT_REASONS } from "./constants/Constants" ;
27
26
import BlogPostsTab from "./BlogPostsTab" ;
28
27
import ProfileImage from "../../components/ProfileImage" ;
29
28
import FetchUserFollows from '../utils/FetchUserFollows' ;
30
- import api from '../../utils/api' ; // Updated import path to use new TypeScript version
29
+ import api from '../../utils/api' ;
31
30
32
31
function TabPanel ( props ) {
33
32
const { children, value, index, ...other } = props ;
@@ -57,7 +56,7 @@ function a11yProps(index) {
57
56
}
58
57
59
58
function ProfileDetail ( { initialUser } ) {
60
- const { user : authenticatedUser , isAuthenticated } = useContext ( UserContext ) ;
59
+ const { user : authenticatedUser } = useLoaderData ( ) ;
61
60
const { username } = useParams ( ) ;
62
61
const navigate = useNavigate ( ) ;
63
62
const location = useLocation ( ) ;
@@ -70,8 +69,8 @@ function ProfileDetail({ initialUser }) {
70
69
const [ followers , setFollowers ] = useState ( [ ] ) ;
71
70
const [ following , setFollowing ] = useState ( [ ] ) ;
72
71
73
- const isOwner = authenticatedUser && authenticatedUser . username === username ;
74
- const fileInputRef = useRef ( null ) ;
72
+ const isOwner = authenticatedUser ? .username === username ;
73
+ const isAuthenticated = ! ! authenticatedUser ;
75
74
76
75
const [ isEditingBio , setIsEditingBio ] = useState ( false ) ;
77
76
const [ bioInput , setBioInput ] = useState ( initialUser ?. bio || '' ) ;
@@ -89,30 +88,12 @@ function ProfileDetail({ initialUser }) {
89
88
}
90
89
} , [ authenticatedUser , username ] ) ;
91
90
92
- const handleImageUpload = async ( event ) => {
93
- const file = event . target . files [ 0 ] ;
94
- if ( ! file ) return ;
95
-
96
- const formData = new FormData ( ) ;
97
- formData . append ( "image" , file ) ;
98
-
99
- try {
100
- await api . put ( `/users/me/image/update/` , formData ) ;
101
- const response = await api . get ( `/users/${ username } /` ) ;
102
- setProfile ( response . data ) ;
103
- } catch ( error ) {
104
- console . error ( error ) ;
105
- }
106
- } ;
107
-
108
- const handleImageDelete = async ( ) => {
109
- try {
110
- await api . delete ( `/users/me/image/delete/` ) ;
111
- const response = await api . get ( `/users/${ username } /` ) ;
112
- setProfile ( response . data ) ;
113
- } catch ( error ) {
114
- console . error ( error ) ;
115
- }
91
+ const handleImageChange = async ( newImageUrl ) => {
92
+ // Update the profile state with the new image URL
93
+ setProfile ( prev => ( {
94
+ ...prev ,
95
+ profile_image : newImageUrl
96
+ } ) ) ;
116
97
} ;
117
98
118
99
const fetchFollowers = useCallback (
@@ -213,22 +194,14 @@ function ProfileDetail({ initialUser }) {
213
194
{ loading ? (
214
195
< Skeleton circle width = { 200 } height = { 200 } />
215
196
) : (
216
- < >
217
- < ProfileImage
218
- username = { username }
219
- imageUrl = { profile . profile_image }
220
- size = { 200 }
221
- isOwner = { isOwner }
222
- onImageClick = { ( ) => isOwner && fileInputRef . current ?. click ( ) }
223
- />
224
- < input
225
- type = "file"
226
- ref = { fileInputRef }
227
- onChange = { handleImageUpload }
228
- style = { { display : 'none' } }
229
- accept = "image/*"
230
- />
231
- </ >
197
+ < ProfileImage
198
+ imageSrc = { profile . profile_image }
199
+ username = { username }
200
+ width = { 200 }
201
+ height = { 200 }
202
+ showOptions = { isOwner }
203
+ onImageChange = { handleImageChange }
204
+ />
232
205
) }
233
206
</ Grid >
234
207
< Grid item xs = { 12 } md = { 8 } >
0 commit comments