mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-07 06:08:54 +00:00
get image and profile data from database
This commit is contained in:
parent
e0d967e4f3
commit
629a472f5e
5 changed files with 139 additions and 94 deletions
|
@ -15,10 +15,11 @@ import CloseIcon from "@mui/icons-material/Close";
|
||||||
import EditSquareIcon from "@mui/icons-material/EditSquare";
|
import EditSquareIcon from "@mui/icons-material/EditSquare";
|
||||||
import "./changeAvatarDialog.css";
|
import "./changeAvatarDialog.css";
|
||||||
import ButtonRotkehlchen from "./ButtonRotkehlchen";
|
import ButtonRotkehlchen from "./ButtonRotkehlchen";
|
||||||
import { useFilePicker } from "use-file-picker";
|
|
||||||
import Username from "./Username";
|
import Username from "./Username";
|
||||||
import "./username.css";
|
import "./username.css";
|
||||||
import api from "../api/axios";
|
import api from "../api/axios";
|
||||||
|
import { ChangeEvent, useState } from "react";
|
||||||
|
import { UserProfile } from "../types/UserProfile";
|
||||||
|
|
||||||
const BootstrapDialog = styled(Dialog)(({ theme }) => ({
|
const BootstrapDialog = styled(Dialog)(({ theme }) => ({
|
||||||
"& .MuiDialogContent-root": {
|
"& .MuiDialogContent-root": {
|
||||||
|
@ -32,52 +33,50 @@ const BootstrapDialog = styled(Dialog)(({ theme }) => ({
|
||||||
export default function AvatarDialog({
|
export default function AvatarDialog({
|
||||||
ownAccount,
|
ownAccount,
|
||||||
username,
|
username,
|
||||||
|
setUserData,
|
||||||
|
imageUrl,
|
||||||
}: {
|
}: {
|
||||||
ownAccount: boolean;
|
ownAccount: boolean;
|
||||||
username: string;
|
username: string;
|
||||||
|
setUserData: React.Dispatch<React.SetStateAction<UserProfile | null>>;
|
||||||
|
imageUrl?: string | null;
|
||||||
}) {
|
}) {
|
||||||
const [profilePicture, setProfilePicture] = React.useState<string | null>(
|
const [file, setFile] = useState<File>();
|
||||||
null
|
|
||||||
);
|
const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
if (e.target.files) {
|
||||||
|
setFile(e.target.files[0]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const saveProfilePicture = async () => {
|
const saveProfilePicture = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await api.post("/profile/updateProfilePicture", {
|
const formData = new FormData();
|
||||||
profilePicture: profilePicture,
|
console.log("Saving profile picture:", file);
|
||||||
});
|
if (file) {
|
||||||
setProfilePicture(response.data.data.profilePicture);
|
formData.append("image", file);
|
||||||
console.log(
|
const response = await api.post(
|
||||||
"Profile picture saved successfully:",
|
"/profile/uploadProfilePicture",
|
||||||
response.data.data.profilePicture
|
formData
|
||||||
);
|
);
|
||||||
|
console.log("Profile picture saved:", response.data);
|
||||||
|
setUserData((prevData) => (prevData ?{
|
||||||
|
...prevData,
|
||||||
|
profilePictureUrl: response.data.url
|
||||||
|
} : null));
|
||||||
|
}
|
||||||
setOpen(false); // Close the dialog after saving
|
setOpen(false); // Close the dialog after saving
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error saving profile picture:", error);
|
console.error("Error saving profile picture:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const { openFilePicker, filesContent, loading, clear } = useFilePicker({
|
|
||||||
accept: ".png, .jpg, .jpeg",
|
|
||||||
multiple: false,
|
|
||||||
readAs: "DataURL",
|
|
||||||
limitFilesConfig: { max: 1 },
|
|
||||||
});
|
|
||||||
|
|
||||||
const setImageURL = ({ newImage = false }: { newImage: boolean }) => {
|
|
||||||
if (newImage) {
|
|
||||||
return filesContent[0].content;
|
|
||||||
}
|
|
||||||
// TODO: If no image is selected, return the image already in the database or undefined
|
|
||||||
return undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
const handleClickOpen = () => {
|
const handleClickOpen = () => {
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
};
|
};
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
clear(); // Reset the selected image when closing
|
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,11 +89,7 @@ export default function AvatarDialog({
|
||||||
alt="Username"
|
alt="Username"
|
||||||
// current code does not work yet
|
// current code does not work yet
|
||||||
// TODO: If no image is selected, return the image already in the database or undefined
|
// TODO: If no image is selected, return the image already in the database or undefined
|
||||||
src={
|
src={imageUrl ? imageUrl : undefined}
|
||||||
filesContent.length > 0
|
|
||||||
? setImageURL({ newImage: true })
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
U
|
U
|
||||||
</Avatar>
|
</Avatar>
|
||||||
|
@ -135,30 +130,34 @@ export default function AvatarDialog({
|
||||||
objectFit: "cover",
|
objectFit: "cover",
|
||||||
maxWidth: "30rem",
|
maxWidth: "30rem",
|
||||||
maxHeight: "30rem",
|
maxHeight: "30rem",
|
||||||
|
marginBottom: "1rem",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{filesContent.map((file) => (
|
<img
|
||||||
<img
|
alt={file ? file.name : "Profile Picture"}
|
||||||
alt={file.name}
|
src={file ? URL.createObjectURL(file) : imageUrl || ""}
|
||||||
src={file.content}
|
style={{
|
||||||
style={{
|
maxWidth: "30rem",
|
||||||
maxWidth: "30rem",
|
maxHeight: "30rem",
|
||||||
maxHeight: "30rem",
|
width: "100%",
|
||||||
width: "100%",
|
height: "100%",
|
||||||
height: "100%",
|
objectFit: "cover",
|
||||||
objectFit: "cover",
|
}}
|
||||||
}}
|
></img>
|
||||||
></img>
|
|
||||||
))}
|
|
||||||
</Box>
|
</Box>
|
||||||
{ownAccount && (
|
{ownAccount && (
|
||||||
<div className="change-avatar-button">
|
<div className="change-avatar-button">
|
||||||
<IconButton
|
<label htmlFor="profile-picture">
|
||||||
aria-label="upload picture"
|
<EditSquareIcon className="edit-icon" />
|
||||||
onClick={() => openFilePicker()}
|
<input
|
||||||
>
|
type="file"
|
||||||
<EditSquareIcon className="edit-icon" />
|
id="profile-picture"
|
||||||
</IconButton>
|
name="profile-picture"
|
||||||
|
accept=".png, .jpg, .jpeg"
|
||||||
|
onChange={handleFileChange}
|
||||||
|
style={{ display: "none" }}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|
|
@ -3,44 +3,66 @@ import { StyledEngineProvider } from "@mui/material/styles";
|
||||||
import "./quiltedImageList.css";
|
import "./quiltedImageList.css";
|
||||||
import { Box, Grid } from "@mui/material";
|
import { Box, Grid } from "@mui/material";
|
||||||
import api from "../api/axios";
|
import api from "../api/axios";
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { UserProfile } from "../types/UserProfile";
|
import { UserProfile } from "../types/UserProfile";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
export default function StandardImageList({user} : {user: UserProfile}) {
|
export default function StandardImageList({ user }: { user: UserProfile }) {
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [itemData, setData] = useState<{ img: string; title: string }[]>([]);
|
const [images, setImages] = useState<
|
||||||
|
{ imageUrl: string; id: string; description: string }[]
|
||||||
|
>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchUserPosts().then(console.log);
|
||||||
|
},[user])
|
||||||
|
|
||||||
const fetchUserPosts = async () => {
|
const fetchUserPosts = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await api.get(`/profile/posts/${user.username}`);
|
if (images.length <= 0) {
|
||||||
const posts = response.data.data;
|
console.log("Fetching user posts for:", user.username);
|
||||||
const images = posts.map((post: { imageUrl: string; title: string }) => ({
|
const response = await api.get(`/posts/getUserPosts/${user.username}`);
|
||||||
img: post.imageUrl,
|
const posts = response.data.posts;
|
||||||
title: post.title,
|
posts.map(async (post: { id: string; description: string }) => {
|
||||||
}));
|
try {
|
||||||
setData(images);
|
await api
|
||||||
|
.get(`/posts/getPost/{postId}?postId=${post.id}`)
|
||||||
|
.then((response) => {
|
||||||
|
if (response.data) {
|
||||||
|
setImages((prevImages) => [
|
||||||
|
...prevImages,
|
||||||
|
{
|
||||||
|
imageUrl: response.data.images[0].url,
|
||||||
|
id: post.id,
|
||||||
|
description: post.description || "",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error("Error fetching post image:", error);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing post:", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching user posts:", error);
|
console.error("Error fetching user posts:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchUserPosts().then(console.log);
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledEngineProvider injectFirst>
|
<StyledEngineProvider injectFirst>
|
||||||
<Box className="box">
|
<Box className="box">
|
||||||
<Grid container spacing={1} className="image-list">
|
<Grid container spacing={1} className="image-list">
|
||||||
{itemData.map((item, index) => (
|
{images.map((item, index) => (
|
||||||
<ImageListItem key={index}>
|
<ImageListItem key={index}>
|
||||||
<img
|
<img
|
||||||
srcSet={`${item.img}?w=164&h=164&fit=crop&auto=format&dpr=2 2x`}
|
src={item.imageUrl}
|
||||||
src={`${item.img}?w=164&h=164&fit=crop&auto=format`}
|
alt={item.description}
|
||||||
alt={item.title}
|
onClick={
|
||||||
onClick={() =>
|
() => navigate("/feed", { replace: true })
|
||||||
navigate("/feed", { replace: true })
|
|
||||||
// anchor to post that was clicked
|
// anchor to post that was clicked
|
||||||
}
|
}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
|
@ -52,4 +74,3 @@ export default function StandardImageList({user} : {user: UserProfile}) {
|
||||||
</StyledEngineProvider>
|
</StyledEngineProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,10 @@
|
||||||
border-radius: 1rem;
|
border-radius: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.edit-icon {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
@media screen and (min-width: 768px) {
|
@media screen and (min-width: 768px) {
|
||||||
.profile-avatar {
|
.profile-avatar {
|
||||||
width: 5rem;
|
width: 5rem;
|
||||||
|
|
|
@ -22,18 +22,18 @@ function Profile() {
|
||||||
const [userData, setUserData] = useState<UserProfile | null>({
|
const [userData, setUserData] = useState<UserProfile | null>({
|
||||||
id: "",
|
id: "",
|
||||||
username: "",
|
username: "",
|
||||||
bio: "default",
|
bio: "",
|
||||||
profilePictureUrl: null,
|
profilePictureUrl: null,
|
||||||
followers: 0,
|
followers: 0,
|
||||||
following: 0,
|
following: 0,
|
||||||
// posts: 0,
|
posts: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const userProfile = async () => {
|
const userProfile = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await api.get(`/profile/get/${username}`);
|
const response = await api.get(`/profile/get/${username}`);
|
||||||
setUserData(response.data.data);
|
setUserData(response.data.data);
|
||||||
return
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
navigate("/", { replace: true }); /* replace to 404 page */
|
navigate("/", { replace: true }); /* replace to 404 page */
|
||||||
console.error("Error fetching user profile:", error);
|
console.error("Error fetching user profile:", error);
|
||||||
|
@ -42,42 +42,63 @@ function Profile() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const ownAccount = username === user?.username;
|
const ownAccount = username === user?.username;
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
userProfile();
|
||||||
|
}, []);
|
||||||
|
|
||||||
userProfile();
|
const setBio = (bio: string) => {
|
||||||
}, []);
|
setUserData((prevData) => {
|
||||||
|
if (prevData) {
|
||||||
const setBio = (bio: string) => {
|
return { ...prevData, bio: bio };
|
||||||
setUserData((prevData) => {
|
}
|
||||||
if (prevData) {
|
return prevData;
|
||||||
return { ...prevData, bio: bio };
|
});
|
||||||
|
};
|
||||||
|
function handleFollowUser() {
|
||||||
|
// TODO: implement follow user functionality
|
||||||
|
if (user) {
|
||||||
|
api.post(`follower/follow/${username}`)
|
||||||
}
|
}
|
||||||
return prevData;
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<StyledEngineProvider injectFirst>
|
<StyledEngineProvider injectFirst>
|
||||||
<div className="profile-display">
|
<div className="profile-display">
|
||||||
<div className="user-info blue-background">
|
<div className="user-info blue-background">
|
||||||
<ChangeAvatarDialog ownAccount={ownAccount} username={userData?.username || ""} />
|
<ChangeAvatarDialog
|
||||||
<Bio ownAccount={ownAccount} bioText={userData?.bio} setBio={setBio} />
|
ownAccount={ownAccount}
|
||||||
|
username={userData?.username || ""}
|
||||||
|
setUserData={setUserData}
|
||||||
|
imageUrl={userData?.profilePictureUrl}
|
||||||
|
/>
|
||||||
|
<Bio
|
||||||
|
ownAccount={ownAccount}
|
||||||
|
bioText={userData?.bio}
|
||||||
|
setBio={setBio}
|
||||||
|
/>
|
||||||
<Divider variant="middle" className="divider" />
|
<Divider variant="middle" className="divider" />
|
||||||
{/* TODO: Change data to data from Database */}
|
{/* TODO: Change data to data from Database */}
|
||||||
<div className="numeral-data body-bold">
|
<div className="numeral-data body-bold">
|
||||||
<div className="data">
|
<div className="data">
|
||||||
<span aria-label="current-post-number">{userData?.following}</span>
|
<span aria-label="current-post-number">{userData?.posts}</span>
|
||||||
<span className="data-label title-h1">Posts</span>
|
<span className="data-label title-h1">Posts</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="data">
|
<div className="data">
|
||||||
<span aria-label="current-follower-number">{userData?.followers}</span>
|
<span aria-label="current-follower-number">
|
||||||
|
{userData?.followers}
|
||||||
|
</span>
|
||||||
<span className="data-label title-h1">Followers</span>
|
<span className="data-label title-h1">Followers</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="data">
|
<div className="data">
|
||||||
<span aria-label="current-following-number">{userData?.following}</span>
|
<span aria-label="current-following-number">
|
||||||
|
{userData?.following}
|
||||||
|
</span>
|
||||||
<span className="data-label title-h1">Following</span>
|
<span className="data-label title-h1">Following</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<RotkehlchenButton style="primary" label="Follow" type="button" />
|
{!ownAccount && (
|
||||||
|
<RotkehlchenButton style="primary" label="Follow" type="button" onClick={handleFollowUser} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{userData && <QuiltedImageList user={userData} />}
|
{userData && <QuiltedImageList user={userData} />}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -5,5 +5,5 @@ export type UserProfile = {
|
||||||
profilePictureUrl: string | null;
|
profilePictureUrl: string | null;
|
||||||
followers: number;
|
followers: number;
|
||||||
following: number;
|
following: number;
|
||||||
// posts: number;
|
posts: number;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue