Skip to content

Commit

Permalink
created individual post and comment components
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-sayeed committed Apr 15, 2024
1 parent 6d063e8 commit 1074c8b
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
padding: 15px;
margin-bottom: 10px;
background-color: #fff;
border: 1px solid #ccc; /* Add border around the comment */
border: 1px solid #ccc;
border-radius: 8px;
display: flex;
}
Expand Down
63 changes: 63 additions & 0 deletions frontend/src/components/Comment/Comment.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from "react";
import { LikeCommentButton } from "../Like/Like";
import "./Comment.css";

const Comment = ({ comments, loggedInUser, fetchComments }) => {
const addOrdinalSuffix = (day) => {
if (day === 1 || day === 21 || day === 31) return day + "st";
if (day === 2 || day === 22) return day + "nd";
if (day === 3 || day === 23) return day + "rd";
return day + "th";
};

const formatDate = (createdAt) => {
const date = new Date(createdAt);
const day = addOrdinalSuffix(date.getDate());
const month = date.toLocaleString("en-GB", { month: "short" });
const time = date.toLocaleString("en-GB", {
hour: "numeric",
minute: "numeric",
});

return `${day} ${month} ${date.getFullYear()} at ${time}`;
};

return (
<div>
<div>
{comments.map((comment) => (
<div className="comment-container" key={comment._id}>
<div className="comment-content">
<div className="comment-user">
<img
className="comment-profilePicture"
src={comment.user.profilePicture}
alt={comment.user.fullName}
/>
<div className="comment-date-and-name">
<div className="comment-fullName">
{comment.user.fullName}
</div>
<div className="comment-date">
{formatDate(comment.createdAt)}
</div>
</div>
</div>
<div className="comment-text">{comment.comment_text}</div>
<div className="comment-like-button">
<LikeCommentButton
commentId={comment._id}
isLiked={comment.likedBy.includes(loggedInUser)}
updateCommentLikeFeed={fetchComments}
/>
</div>
<div className="comment-like-count">{comment.likedBy.length}</div>
</div>
</div>
))}
</div>
</div>
);
};

export default Comment;
66 changes: 10 additions & 56 deletions frontend/src/components/Comment/Comments.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useState, useEffect } from "react";
import { getComments } from "../../services/comments";
import { LikeCommentButton } from "../Like/Like";
import Comment from "./Comment";
import CreateComment from "./CreateComment";
import "./Comments.css";

const Comments = ({ postId }) => {
const [commentData, setCommentData] = useState([]);
const [comments, setComments] = useState([]);

const loggedInUser = localStorage.getItem("userId");

Expand All @@ -15,7 +14,7 @@ const Comments = ({ postId }) => {
getComments(postId, token) // Fetch comments for the specific post
.then((data) => {
console.log("Received comment data:", data);
setCommentData(data.comments);
setComments(data.comments);
})
.catch((error) => console.error("Error fetching comments:", error));
}
Expand All @@ -25,62 +24,17 @@ const Comments = ({ postId }) => {
fetchComments();
}, []);

const addOrdinalSuffix = (day) => {
if (day === 1 || day === 21 || day === 31) return day + "st";
if (day === 2 || day === 22) return day + "nd";
if (day === 3 || day === 23) return day + "rd";
return day + "th";
};

return (
<div>
<>
<div className="create-comment">
<CreateComment postId={postId} updateCommentFeed={fetchComments} />
</div>
<div>
{commentData.map((comment) => {
const date = new Date(comment.createdAt);
const day = addOrdinalSuffix(date.getDate());
const month = date.toLocaleString("en-GB", { month: "short" });
const time = date.toLocaleString("en-GB", {
hour: "numeric",
minute: "numeric",
});

const formattedDate = `${day} ${month} ${date.getFullYear()} at ${time}`;

return (
<div className="comment-container" key={comment._id}>
<div className="comment-content">
<div className="comment-user">
<img
className="comment-profilePicture"
src={comment.user.profilePicture}
/>
<div className="comment-date-and-name">
<div className="comment-fullName">
{comment.user.fullName}
</div>
<div className="comment-date">{formattedDate}</div>
</div>
</div>
<div className="comment-text">{comment.comment_text}</div>
<div className="comment-like-button">
<LikeCommentButton
commentId={comment._id}
isLiked={comment.likedBy.includes(loggedInUser)}
updateCommentLikeFeed={fetchComments}
/>
</div>
<div className="comment-like-count">
{comment.likedBy.length}
</div>
</div>
</div>
);
})}
</div>
</div>
<Comment
comments={comments}
loggedInUser={loggedInUser}
fetchComments={fetchComments}
/>
</>
);
};

Expand Down
19 changes: 7 additions & 12 deletions frontend/src/components/NavBar/Navbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
width: 100%;
display: flex;
justify-content: space-between; /* Add this to push the profile picture to the left and links to the right */
align-items: center; /* Center vertically */
align-items: center;
padding: 5px;
background-color: #1b83a4;
z-index: 999;
Expand Down Expand Up @@ -36,14 +36,9 @@
}

.navbar__right a:hover {
color: #ffffff; /* Change text color on hover */
background-color: rgba(
255,
255,
255,
0.1
); /* Change background color on hover */
border-color: #fff; /* Change border color on hover */
color: #ffffff;
background-color: rgba(255, 255, 255, 0.1);
border-color: #fff;
}

.navbar__right a {
Expand All @@ -59,11 +54,11 @@
}

.navbar_profile_pic {
width: 45px; /* Adjust size as needed */
height: 45px; /* Adjust size as needed */
width: 45px;
height: 45px;
border-radius: 50%;

margin-left: 10px; /* Add margin between image and text */
margin-left: 10px;
margin-top: 5px;
margin-bottom: 5px;
}
3 changes: 1 addition & 2 deletions frontend/src/components/Post/CreatePost.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
float: right;
}

/* Hover state */
.image-button:hover {
background-color: #ffffff; /* Change background color on hover */
background-color: #ffffff;
}
File renamed without changes.
66 changes: 66 additions & 0 deletions frontend/src/components/Post/Post.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useEffect } from "react";
import { LikePostButton } from "../Like/Like";
import Comments from "../Comment/Comments";
import "./Post.css";

const Post = ({ posts, loggedInUser, fetchPosts }) => {
const addOrdinalSuffix = (day) => {
if (day === 1 || day === 21 || day === 31) return day + "st";
if (day === 2 || day === 22) return day + "nd";
if (day === 3 || day === 23) return day + "rd";
return day + "th";
};

const formatDate = (createdAt) => {
const date = new Date(createdAt);
const day = addOrdinalSuffix(date.getDate());
const month = date.toLocaleString("en-GB", { month: "short" });
const time = date.toLocaleString("en-GB", {
hour: "numeric",
minute: "numeric",
});

return `${day} ${month} ${date.getFullYear()} at ${time}`;
};

return (
<>
<div>
{posts.map((post) => (
<div className="post" key={post._id}>
<div className="post-border">
<div className="post-header-container">
<img
className="post-profile_picture"
src={post.user.profilePicture}
alt="Profile"
/>
<div>
<div className="post-user-fullName">{post.user.fullName}</div>
<div className="post-date">{formatDate(post.createdAt)}</div>
</div>
</div>
{post.image && (
<img className="post_image" src={post.image} alt="Post" />
)}
<div className="post-message">{post.message}</div>
<div className="post-like-button">
<LikePostButton
postId={post._id}
isLiked={post.likedBy.includes(loggedInUser)}
updatePostLikeFeed={fetchPosts}
/>
</div>
<div className="post-like-counter">{post.likedBy.length}</div>
</div>
<div>
<Comments postId={post._id} />
</div>
</div>
))}
</div>
</>
);
};

export default Post;
63 changes: 7 additions & 56 deletions frontend/src/components/Post/Posts.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { useState, useEffect } from "react";
import { getPosts } from "../../services/posts";
import { LikePostButton } from "../Like/Like";
import CreatePost from "./CreatePost";
import Post from "./Post";
import { useNavigate } from "react-router-dom";
import Comments from "../Comment/Comments";
import "./Posts.css";

const Posts = () => {
const [posts, setPosts] = useState([]);
Expand Down Expand Up @@ -32,67 +30,20 @@ const Posts = () => {
fetchPosts();
}, []);

const addOrdinalSuffix = (day) => {
if (day === 1 || day === 21 || day === 31) return day + "st";
if (day === 2 || day === 22) return day + "nd";
if (day === 3 || day === 23) return day + "rd";
return day + "th";
};

return (
<>
<div className="createpost">
<CreatePost updatePostFeed={fetchPosts} />
</div>
<div>
{posts.map((post) => {
const date = new Date(post.createdAt);
const day = addOrdinalSuffix(date.getDate());
const month = date.toLocaleString("en-GB", { month: "short" });
const time = date.toLocaleString("en-GB", {
hour: "numeric",
minute: "numeric",
});

const formattedDate = `${day} ${month} ${date.getFullYear()} at ${time}`;

return (
<div className="post" key={post._id}>
<div className="post-border">
<div className="post-header-container">
<img
className="post-profile_picture"
src={post.user.profilePicture}
alt="Profile"
/>
<div>
<div className="post-user-fullName">
{post.user.fullName}
</div>
<div className="post-date">{formattedDate}</div>
</div>
</div>
{post.image && (
<img className="post_image" src={post.image} alt="Post" />
)}
<div className="post-message">{post.message}</div>
<div className="post-like-button">
<LikePostButton
postId={post._id}
isLiked={post.likedBy.includes(loggedInUser)}
updatePostLikeFeed={fetchPosts}
/>
</div>
<div className="post-like-counter">{post.likedBy.length}</div>
</div>
<div>
<Comments postId={post._id} />
</div>
</div>
);
})}
<Post
posts={posts}
loggedInUser={loggedInUser}
fetchPosts={fetchPosts}
/>
</div>
</>
);
};

export default Posts;
Loading

0 comments on commit 1074c8b

Please sign in to comment.