Skip to content

Commit

Permalink
Display comments on posts
Browse files Browse the repository at this point in the history
All posts will still display the reply box underneath them.
Added default profile picture fallback to notification dropdowns.
  • Loading branch information
MaoShizhong committed Jan 2, 2024
1 parent 9be726d commit e35b4fd
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/components/header/FriendRequests.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef } from 'react';
import { Link, useParams } from 'react-router-dom';
import { DEFAULT_PROFILE_PICTURE } from '../../helpers/constants';
import { useCloseDropdown } from '../../helpers/hooks';
import { getFullNameFromDetails } from '../../helpers/util';
import dropdownStyles from './css/dropdown.module.css';
Expand Down Expand Up @@ -31,7 +32,7 @@ export function FriendRequests({ setIsOpen, button, friendRequests }) {
reloadDocument={request.handle === currentPage}
>
<img
src={request.profilePicture}
src={request.profilePicture ?? DEFAULT_PROFILE_PICTURE}
height="40"
width="40"
alt="friend request profile picture"
Expand Down
3 changes: 2 additions & 1 deletion src/components/header/Notifications.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef } from 'react';
import { Link, useParams } from 'react-router-dom';
import { DEFAULT_PROFILE_PICTURE } from '../../helpers/constants';
import { useCloseDropdown } from '../../helpers/hooks';
import { getFullNameFromDetails, getPostPreview, getRelativeTimestamp } from '../../helpers/util';
import dropdownStyles from './css/dropdown.module.css';
Expand Down Expand Up @@ -28,7 +29,7 @@ export function Notifications({ setIsOpen, button, ownHandle, notifications, set
onClick={() => setNotifications([])}
>
<img
src={notification.author.profilePicture}
src={notification.author.profilePicture ?? DEFAULT_PROFILE_PICTURE}
height="40"
width="40"
alt="notification profile picture"
Expand Down
60 changes: 60 additions & 0 deletions src/components/post/Comments.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Link, useOutletContext } from 'react-router-dom';
import { DEFAULT_PROFILE_PICTURE } from '../../helpers/constants';
import {
autoResizeTextarea,
getFullNameFromDetails,
getRelativeTimestamp,
} from '../../helpers/util';
import commentStyles from './css/comment.module.css';
import postStyles from './css/post.module.css';

export function Comments({ postID, comments }) {
const { user } = useOutletContext();

async function postComment(e) {
e.preventDefault();
}

return (
<div className={commentStyles.comments}>
{comments.map((comment) => (
<div key={comment._id} className={commentStyles.comment}>
<img
src={comment.author.profilePicture ?? DEFAULT_PROFILE_PICTURE}
alt="comment profile picture"
/>

<div className={commentStyles.content}>
<div className={commentStyles.top}>
<Link to={`/${comment.author.handle}`}>
{getFullNameFromDetails(comment.author.details)}
</Link>

{user._id === comment.author._id && (
<button className={commentStyles.delete}>Delete</button>
)}
</div>

<p>{comment.body}</p>

<div className={postStyles.timestamp}>
{getRelativeTimestamp(comment.timestamp)}
</div>
</div>
</div>
))}

<form onSubmit={postComment} className={commentStyles.reply}>
<label htmlFor={`reply_${postID}`}>Reply:</label>
<textarea
name="body"
id={`reply_${postID}`}
rows="1"
placeholder="Comment on this post..."
onInput={autoResizeTextarea}
></textarea>
<button className={commentStyles.postComment}>Post</button>
</form>
</div>
);
}
3 changes: 3 additions & 0 deletions src/components/post/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { PostMenuButton } from '../buttons/PostMenuButton';
import buttonStyles from '../buttons/css/button.module.css';
import { Loading } from '../loading/Loading';
import { Comments } from './Comments';
import { ThumbsUp } from './ThumbsUp';
import postStyles from './css/post.module.css';

Expand Down Expand Up @@ -141,6 +142,8 @@ export function Post({ post, setPosts }) {
</span>
)}
</div>

<Comments postID={post._id} comments={post.comments} />
</div>
</article>
);
Expand Down
68 changes: 68 additions & 0 deletions src/components/post/css/comment.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.comments {
display: flex;
flex-direction: column;

& > * {
background-color: var(--main-fade-vl);
padding: 6px;
}
}

.comment {
border-bottom: 1px solid var(--faint-l);
display: flex;
gap: 6px;

& > img {
--profile-pciture-size: 42px;

height: var(--profile-pciture-size);
width: var(--profile-pciture-size);
}
}

.content {
display: flex;
flex: 1;
flex-direction: column;
font-size: small;
gap: 4px;

& > p {
white-space: pre-wrap;
word-break: break-word;
}
}

.top {
display: flex;
justify-content: space-between;
}

.delete {
background-color: transparent;
font-size: x-small;

&:hover {
color: var(--main-d);
text-decoration: underline;
}
}

.reply {
align-items: center;
display: flex;
gap: 6px;

& > textarea {
flex: 1;
padding-inline: 4px;
resize: none;
}
}

.postComment {
align-self: flex-end;
composes: bold from '../../buttons/css/button.module.css';
font-size: small;
}
2 changes: 1 addition & 1 deletion src/global_stylesheets/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--main-l: #379580;
--main-fade-d: #0b201cd7;
--main-fade-l: #1c3b3860;
--main-fade-vl: #3a7a673d;
--main-fade-vl: rgba(56, 146, 119, 0.185);

--off-black: #080808;

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const autoResizeTextarea = (e) => {

textarea.style.height = 'auto';
// the +2 gets rid of the scrollbar
textarea.style.height = `${textarea.scrollHeight + 2}px`;
textarea.style.height = `${textarea.scrollHeight}px`;
};

export const checkFileDetails = (e, setFileError) => {
Expand Down

0 comments on commit e35b4fd

Please sign in to comment.