Skip to content

Commit f010977

Browse files
committed
profile
1 parent 9139931 commit f010977

File tree

1 file changed

+20
-47
lines changed

1 file changed

+20
-47
lines changed

frontend/app/old-app/components/ProfileDetail.jsx

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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";
33
import {
44
Edit as EditIcon,
55
Save as SaveIcon,
@@ -22,12 +22,11 @@ import {
2222
Link as MUILink
2323
} from '@mui/material';
2424

25-
import UserContext from "./contexts/UserContext";
2625
import {REDIRECT_REASONS} from "./constants/Constants";
2726
import BlogPostsTab from "./BlogPostsTab";
2827
import ProfileImage from "../../components/ProfileImage";
2928
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';
3130

3231
function TabPanel(props) {
3332
const { children, value, index, ...other } = props;
@@ -57,7 +56,7 @@ function a11yProps(index) {
5756
}
5857

5958
function ProfileDetail({ initialUser }) {
60-
const { user: authenticatedUser, isAuthenticated } = useContext(UserContext);
59+
const { user: authenticatedUser } = useLoaderData();
6160
const { username } = useParams();
6261
const navigate = useNavigate();
6362
const location = useLocation();
@@ -70,8 +69,8 @@ function ProfileDetail({ initialUser }) {
7069
const [followers, setFollowers] = useState([]);
7170
const [following, setFollowing] = useState([]);
7271

73-
const isOwner = authenticatedUser && authenticatedUser.username === username;
74-
const fileInputRef = useRef(null);
72+
const isOwner = authenticatedUser?.username === username;
73+
const isAuthenticated = !!authenticatedUser;
7574

7675
const [isEditingBio, setIsEditingBio] = useState(false);
7776
const [bioInput, setBioInput] = useState(initialUser?.bio || '');
@@ -89,30 +88,12 @@ function ProfileDetail({ initialUser }) {
8988
}
9089
}, [authenticatedUser, username]);
9190

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+
}));
11697
};
11798

11899
const fetchFollowers = useCallback(
@@ -213,22 +194,14 @@ function ProfileDetail({ initialUser }) {
213194
{loading ? (
214195
<Skeleton circle width={200} height={200} />
215196
) : (
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+
/>
232205
)}
233206
</Grid>
234207
<Grid item xs={12} md={8}>

0 commit comments

Comments
 (0)