From 8b3fad6a530f6de7163a2bdc2f9c2592cfbdc3a3 Mon Sep 17 00:00:00 2001 From: Rykkel <220993G@mymail.nyp.edu.sg> Date: Thu, 1 Aug 2024 05:33:26 +0800 Subject: [PATCH] Retrieve profile picture --- client/src/components/CommentsModule.tsx | 23 ++++++++++++++--------- client/src/pages/CommunityPage.tsx | 11 +++++++++-- client/src/pages/PostPage.tsx | 12 +++++++++++- server/routes/post.js | 8 +++++++- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/client/src/components/CommentsModule.tsx b/client/src/components/CommentsModule.tsx index fb9dc93..547135f 100644 --- a/client/src/components/CommentsModule.tsx +++ b/client/src/components/CommentsModule.tsx @@ -3,19 +3,20 @@ import instance from "../security/http"; import config from "../config"; import { useParams } from "react-router-dom"; import { useEffect, useState } from "react"; -import { Avatar, Button, Dropdown, DropdownItem, DropdownMenu, DropdownTrigger } from "@nextui-org/react"; +import { Avatar, Button, Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, User } from "@nextui-org/react"; import { ChatBubbleOvalLeftEllipsisIcon, EllipsisHorizontalIcon, HandThumbsUpIcon } from "../icons"; interface Comment { id: string; content: string; + user: User; // Make user optional } -// type User = { -// id: string; -// firstName: string; -// lastName: string; -// }; +interface User { + id: string; + firstName: string; + lastName: string; +} export default function CommentsModule() { const { id } = useParams(); @@ -27,6 +28,7 @@ export default function CommentsModule() { instance .get(config.serverAddress + `/post/${postId}/getComments`).then((res) => { setCommentList(res.data); + console.log(res.data); }); }; useEffect(() => { @@ -45,21 +47,24 @@ export default function CommentsModule() {
{commentList.length > 0 ? ( commentList.map((comment) => { + // console.log(comment); // Log each comment to verify its structure + // // Check if `comment.user` is defined before accessing properties + // const user = comment.user || { firstName: "Unknown", lastName: "" }; return (
-
+
-
+
Name
{comment.content}
-
+
diff --git a/client/src/pages/CommunityPage.tsx b/client/src/pages/CommunityPage.tsx index bef03a5..4a79c90 100644 --- a/client/src/pages/CommunityPage.tsx +++ b/client/src/pages/CommunityPage.tsx @@ -46,6 +46,7 @@ type User = { id: string; firstName: string; lastName: string; + }; export default function CommunityPage() { @@ -57,7 +58,6 @@ export default function CommunityPage() { const [userInformation, setUserInformation] = useState>({}); const [currentUserInfo, setCurrentUserInfo] = useState(null); - let accessToken = localStorage.getItem("accessToken"); if (!accessToken) { return ( @@ -89,6 +89,7 @@ export default function CommunityPage() { const fetchUserInformation = async (userId: string) => { try { const user = await retrieveUserInformationById(userId); + setUserInformation((prevMap) => ({ ...prevMap, [userId]: user, @@ -168,6 +169,11 @@ export default function CommunityPage() { navigate(`post/${id}`); }; + // Get pfp from server directly(no need convert blob to url) + const getProfilePicture = (userId: string): string => { + return `${config.serverAddress}/users/profile-image/${userId}`; + }; + return (
@@ -180,6 +186,7 @@ export default function CommunityPage() {
{communityList.map((post) => { + const profilePictureUrl = getProfilePicture(post.userId); return (
e.stopPropagation()}>
diff --git a/client/src/pages/PostPage.tsx b/client/src/pages/PostPage.tsx index 18c8b5e..def1649 100644 --- a/client/src/pages/PostPage.tsx +++ b/client/src/pages/PostPage.tsx @@ -77,6 +77,11 @@ const PostPage: React.FC = () => { } }, [post]); + useEffect(() => { + // Scroll to the top of the page when the component mounts + window.scrollTo(0, 0); + }, []); + if (!post) { return (
@@ -102,6 +107,11 @@ const PostPage: React.FC = () => { } }; + const getProfilePicture = (userId: string): string => { + return `${config.serverAddress}/users/profile-image/${userId}`; + }; + const profilePictureUrl = post ? getProfilePicture(post.userId) : ""; + return (
@@ -124,7 +134,7 @@ const PostPage: React.FC = () => { >
diff --git a/server/routes/post.js b/server/routes/post.js index ea4ee03..130d6f1 100644 --- a/server/routes/post.js +++ b/server/routes/post.js @@ -1,6 +1,6 @@ const express = require('express'); const router = express.Router(); -const { Post, Comment } = require('../models'); +const { Post, Comment, User } = require('../models'); const { Op, where } = require("sequelize"); const yup = require("yup"); const multer = require("multer"); @@ -184,6 +184,12 @@ router.get("/:id/getComments", async (req, res) => { let condition = { where: { postId: id }, + include: [ + { + model: User, + attributes: ['id', 'firstName', 'lastName'] // Specify the attributes you need + } + ], order: [['createdAt', 'DESC']] };