Retrieve comment pfp & name
This commit is contained in:
@@ -24,6 +24,11 @@ export default function CommentsModule() {
|
|||||||
|
|
||||||
let postId = id
|
let postId = id
|
||||||
|
|
||||||
|
// Function to get the profile picture URL
|
||||||
|
const getProfilePicture = (userId: string): string => {
|
||||||
|
return `${config.serverAddress}/users/profile-image/${userId}`;
|
||||||
|
};
|
||||||
|
|
||||||
const getComments = async () => {
|
const getComments = async () => {
|
||||||
instance
|
instance
|
||||||
.get(config.serverAddress + `/post/${postId}/getComments`).then((res) => {
|
.get(config.serverAddress + `/post/${postId}/getComments`).then((res) => {
|
||||||
@@ -47,21 +52,26 @@ export default function CommentsModule() {
|
|||||||
<div className="w-8/12 mx-auto">
|
<div className="w-8/12 mx-auto">
|
||||||
{commentList.length > 0 ? (
|
{commentList.length > 0 ? (
|
||||||
commentList.map((comment) => {
|
commentList.map((comment) => {
|
||||||
// console.log(comment); // Log each comment to verify its structure
|
const user = comment.user; // Get the user object from the comment
|
||||||
// // Check if `comment.user` is defined before accessing properties
|
|
||||||
// const user = comment.user || { firstName: "Unknown", lastName: "" };
|
if (!user) {
|
||||||
|
console.error("User object is undefined for comment:", comment);
|
||||||
|
return null; // Skip rendering this comment if user is undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const profilePictureUrl = getProfilePicture(user.id); // Get the user's profile picture
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col w-full bg-primary-50 dark:bg-primary-950 rounded-xl mb-2 p-3 mx-auto"
|
<div className="flex flex-col w-full bg-primary-50 dark:bg-primary-950 rounded-xl mb-2 p-3 mx-auto"
|
||||||
key={comment.id}>
|
key={comment.id}>
|
||||||
<div className="flex flex-row flex-shrink-0 w-full">
|
<div className="flex flex-row flex-shrink-0 w-full">
|
||||||
<div>
|
<div>
|
||||||
<Avatar
|
<Avatar
|
||||||
src="https://pbs.twimg.com/media/GOva9x5a0AAK8Bn?format=jpg&name=large"
|
src={profilePictureUrl}
|
||||||
size="md"
|
size="md"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col w-10/12 flex-grow text-sm ml-3">
|
<div className="flex flex-col w-10/12 flex-grow text-sm ml-3">
|
||||||
<div className="font-bold">Name</div>
|
<div className="font-bold">{user.firstName} {user.lastName}</div>
|
||||||
<div className="break-words whitespace-pre-wrap">{comment.content}</div>
|
<div className="break-words whitespace-pre-wrap">{comment.content}</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-shrink-0 ml-10">
|
<div className="flex-shrink-0 ml-10">
|
||||||
|
|||||||
@@ -22,10 +22,12 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
Comment.associate = (models) => {
|
Comment.associate = (models) => {
|
||||||
Comment.belongsTo(models.User, {
|
Comment.belongsTo(models.User, {
|
||||||
foreignKey: 'userId',
|
foreignKey: 'userId',
|
||||||
|
as: "user",
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
Comment.belongsTo(models.Post, {
|
Comment.belongsTo(models.Post, {
|
||||||
foreignKey: 'postId',
|
foreignKey: 'postId',
|
||||||
|
as: "post",
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
Comment.hasMany(models.Comment, {
|
Comment.hasMany(models.Comment, {
|
||||||
|
|||||||
@@ -309,6 +309,7 @@ router.get("/:id/getComments", async (req, res) => {
|
|||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: User,
|
model: User,
|
||||||
|
as: "user",
|
||||||
attributes: ["id", "firstName", "lastName"], // Specify the attributes you need
|
attributes: ["id", "firstName", "lastName"], // Specify the attributes you need
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user