From a2630be349967803d70d7ced8f36761926ab3abe Mon Sep 17 00:00:00 2001 From: "luisa.bellitto" Date: Sun, 29 Jun 2025 19:30:39 +0200 Subject: [PATCH] get images to load in correct order --- .../backend/src/controllers/postController.ts | 1 + .../src/components/QuiltedImageList.tsx | 106 ++++++++++-------- code/frontend/src/pages/Profile.tsx | 2 +- code/frontend/src/types/UserProfile.ts | 2 +- 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/code/backend/src/controllers/postController.ts b/code/backend/src/controllers/postController.ts index 1b79d0b..941ad72 100644 --- a/code/backend/src/controllers/postController.ts +++ b/code/backend/src/controllers/postController.ts @@ -205,6 +205,7 @@ export const getUserPosts = async (req: Request, res: Response) => { username: username, }, }, + orderBy: { createdAt: "desc" }, }); if (!posts || posts.length === 0) { res.status(StatusCodes.NOT_FOUND).json({ diff --git a/code/frontend/src/components/QuiltedImageList.tsx b/code/frontend/src/components/QuiltedImageList.tsx index 2ad7748..1a8ae27 100644 --- a/code/frontend/src/components/QuiltedImageList.tsx +++ b/code/frontend/src/components/QuiltedImageList.tsx @@ -1,7 +1,7 @@ import ImageListItem from "@mui/material/ImageListItem"; import { StyledEngineProvider } from "@mui/material/styles"; import "./quiltedImageList.css"; -import { Box, Grid } from "@mui/material"; +import { Box, Grid, Skeleton } from "@mui/material"; import api from "../api/axios"; import { useEffect, useState } from "react"; import { UserProfile } from "../types/UserProfile"; @@ -10,43 +10,55 @@ import { useNavigate } from "react-router-dom"; export default function StandardImageList({ user }: { user: UserProfile }) { const navigate = useNavigate(); const [images, setImages] = useState< - { imageUrl: string; id: string; description: string }[] + { imageUrl: string; id: string; description: string; createdAt: string }[] >([]); useEffect(() => { - fetchUserPosts().then(console.log); - },[user]) + if (user.username != undefined) { + fetchUserPosts().then(console.log); + return; + } + }, [user.username]); + + useEffect(() => { + images.sort( + (a, b) => + new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() + ); + }, [user]); const fetchUserPosts = async () => { try { - if (images.length <= 0) { - console.log("Fetching user posts for:", user.username); - const response = await api.get(`/posts/getUserPosts/${user.username}`); - const posts = response.data.posts; - posts.map(async (post: { id: string; description: string }) => { - try { - 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); + const response = await api.get(`/posts/getUserPosts/${user.username}`); + const posts = response.data.posts; + const fetchedImages = await Promise.all( + posts.map( + async (post: { + id: string; + description: string; + createdAt: string; + }) => { + try { + const response = await api.get( + `/posts/getPost/{postId}?postId=${post.id}` + ); + if (response.data && response.data.images.length > 0) { + console.log(response.data); + return { + imageUrl: response.data.images[0].url, + id: post.id, + description: post.description || "", + createdAt: post.createdAt, + }; + } + } catch (error) { + console.error("Error fetching post images:", error); + } } - }); - } + ) + ); + console.log("Fetched images:", fetchedImages); + setImages(fetchedImages.filter((image) => image !== undefined)); } catch (error) { console.error("Error fetching user posts:", error); } @@ -56,19 +68,25 @@ export default function StandardImageList({ user }: { user: UserProfile }) { - {images.map((item, index) => ( - - {item.description} navigate("/feed", { replace: true }) - // anchor to post that was clicked - } - loading="lazy" - /> - - ))} + {images.map((item, index) => { + return ( + + {item.imageUrl ? ( + {item.description} navigate("/feed", { replace: true }) + // anchor to post that was clicked + } + loading="lazy" + /> + ) : ( + + )} + + ); + })} diff --git a/code/frontend/src/pages/Profile.tsx b/code/frontend/src/pages/Profile.tsx index eb1cee0..bbd67fc 100644 --- a/code/frontend/src/pages/Profile.tsx +++ b/code/frontend/src/pages/Profile.tsx @@ -21,7 +21,7 @@ function Profile() { const [userData, setUserData] = useState({ id: "", - username: "", + username: undefined, bio: "", profilePictureUrl: null, followers: 0, diff --git a/code/frontend/src/types/UserProfile.ts b/code/frontend/src/types/UserProfile.ts index ca438de..f7d5e70 100644 --- a/code/frontend/src/types/UserProfile.ts +++ b/code/frontend/src/types/UserProfile.ts @@ -1,6 +1,6 @@ export type UserProfile = { id: string; - username: string; + username: string | undefined; bio: string | undefined; profilePictureUrl: string | null; followers: number;