Skip to content

Update AllPosts.js and myPosts.js #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions frontend/src/User/myPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ import { fetchMyPosts } from "../store/slices/postSlice";
import "../styles/posts.css";
import CreatePost from "./createPost";
import CommentTextsection from "../shared/components/CommentTextSection";
import Error from "../shared/components/Error";

const MyPost = () => {
const { userInfo } = useSelector((state) => state.auth);
const [showModal, setShowModal] = useState(false);
const [showTextSection, setShowTextSection] = useState({});
const dispatch = useDispatch();
const myPosts = useSelector((state) => state.posts.myPosts);
const errorMessage = useSelector((state) => state.posts.errorMessage);
const [likes, setLikes] = useState({});
const [likeMode, setLikeMode] = useState({});

useEffect(() => {
dispatch(fetchMyPosts());
}, [dispatch]);
console.log(myPosts);

const handleShowCreatePost = () => {
setShowModal(!showModal);
};
Expand All @@ -39,29 +36,16 @@ const MyPost = () => {
};

const handleToggleLike = (postId) => {
setLikes((prevState) => {
const newLikes = { ...prevState };
const isLiked = likeMode[postId];

if (isLiked) {
newLikes[postId] = Math.max(0, (newLikes[postId] || 0) - 1);
} else {
newLikes[postId] = (newLikes[postId] || 0) + 1;
}

return newLikes;
});
setLikeMode((prevState) => ({
setLikes((prevState) => ({
...prevState,
[postId]: !prevState[postId],
[postId]: (prevState[postId] || 0) + (likes[postId] ? -1 : 1),
}));
};

return (
<div className="posts-page">
<Button
variant="contained"
color="primary"
className="create-post-button"
onClick={handleShowCreatePost}
>
Create Post
Expand All @@ -87,19 +71,14 @@ const MyPost = () => {
<div className="post-content">
<Typography variant="body1">{post.text}</Typography>
</div>
<Box sx={{ display: "flex", alignItems: "center", mt: 1 }}>
<IconButton onClick={() => handleToggleLike(post._id)}>
<ThumbUpIcon
color={likeMode[post._id] ? "primary" : "action"}
/>
<Box sx={{ display: "flex", alignItems: "center", mt: 1 }} className="post-actions">
<IconButton onClick={() => handleToggleLike(post._id)} className="like-button">
<ThumbUpIcon color={likes[post._id] ? "primary" : "action"} />
</IconButton>
<Typography variant="body2" sx={{ ml: 1 }}>
{likes[post._id] || 0}
</Typography>
<IconButton onClick={() => handleToggleLike(post._id)}>
<ThumbDownIcon />
</IconButton>
<IconButton onClick={() => handleToggleTextSection(post._id)}>
<IconButton onClick={() => handleToggleTextSection(post._id)} className="comment-button">
<ChatIcon />
</IconButton>
</Box>
Expand Down
177 changes: 81 additions & 96 deletions frontend/src/shared/components/AllPosts.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,91 @@
import { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Avatar, Box, IconButton, Typography } from '@mui/material';
import { Chat as ChatIcon, ThumbUp as ThumbUpIcon, ThumbDown as ThumbDownIcon } from '@mui/icons-material';
import DropdownMenu from './HamburgerDropdown';
import { fetchAllPosts } from '../../store/slices/postSlice';
import '../../styles/posts.css';
import CommentTextsection from './CommentTextSection';
import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Avatar, Box, IconButton, Typography } from "@mui/material";
import { Chat as ChatIcon, ThumbUp as ThumbUpIcon, ThumbDown as ThumbDownIcon } from "@mui/icons-material";
import DropdownMenu from "./HamburgerDropdown";
import { fetchAllPosts } from "../../store/slices/postSlice";
import "../../styles/posts.css";
import CommentTextsection from "./CommentTextSection";

const AllPost = () => {
const dispatch = useDispatch();
const allPosts = useSelector((state) => state.posts.allPosts);
const [likes, setLikes] = useState({});
const [likeMode, setLikeMode] = useState({});
const [showTextSection, setShowTextSection] = useState({});
const dispatch = useDispatch();
const allPosts = useSelector((state) => state.posts.allPosts);
const [likes, setLikes] = useState({});
const [showTextSection, setShowTextSection] = useState({});

useEffect(() => {
dispatch(fetchAllPosts());
}, [dispatch]);
useEffect(() => {
dispatch(fetchAllPosts());
}, [dispatch]);

const handleToggleTextSection = (postId) => {
setShowTextSection((prevState) => ({
...prevState,
[postId]: !prevState[postId],
}));
};
const handleToggleTextSection = (postId) => {
setShowTextSection((prevState) => ({
...prevState,
[postId]: !prevState[postId],
}));
};

const handleToggleLike = (postId) => {
setLikes((prevState) => {
const newLikes = { ...prevState };
const isLiked = likeMode[postId];
const handleToggleLike = (postId) => {
setLikes((prevState) => ({
...prevState,
[postId]: (prevState[postId] || 0) + (likes[postId] ? -1 : 1),
}));
};

if (isLiked) {
newLikes[postId] = Math.max(0, (newLikes[postId] || 0) - 1);
} else {
newLikes[postId] = (newLikes[postId] || 0) + 1;
}

return newLikes;
});
setLikeMode((prevState) => ({
...prevState,
[postId]: !prevState[postId]
}));
};

const { userInfo } = useSelector((state) => state.auth);

return (
<div className="posts-page">
<div className="posts">
{allPosts && allPosts.map((post) => (
post && post.author ? ( // Check if post and post.author exist
<div key={post._id} className="post">
<div className="post-owner">
<Avatar
src={post.author.avatar.filePath}
alt={post.author.name}
sx={{ width: 56, height: 56 }} // Make sure this is consistent with CSS
/>
<div className="username">
<Typography variant="subtitle1" className="post-owner-username">{post.author.name}</Typography>
</div>
<DropdownMenu postId={post._id} userEmail={post.author.email} />
</div>
<div className="post-content">
<Typography variant="body1">{post.text}</Typography>
</div>
<Box sx={{ display: 'flex', alignItems: 'center', mt: 1 }} className="post-actions">
<IconButton onClick={() => handleToggleLike(post._id)} className="like-button">
<ThumbUpIcon />
</IconButton>
<Typography variant="body2" sx={{ ml: 1 }}>
{likes[post._id] || 0}
</Typography>
<IconButton onClick={() => handleToggleLike(post._id)} className="dislike-button">
<ThumbDownIcon />
</IconButton>
<IconButton onClick={() => handleToggleTextSection(post._id)} className="comment-button">
<ChatIcon />
</IconButton>
</Box>
{post.images && post.images.map((image, index) => (
<Box key={index} sx={{ mt: 1 }}>
<img
src={image}
alt="Sample"
style={{ width: '150px', height: '150px', objectFit: 'cover', borderRadius: '8px' }}
/>
</Box>
))}
{showTextSection[post._id] && (
<CommentTextsection
setShowThis={() => handleToggleTextSection(post._id)}
/>
)}
</div>
) : null // Render nothing if post or post.author is missing
return (
<div className="posts-page">
<div className="posts">
{allPosts && allPosts.length > 0 ? (
allPosts.map((post) => (
post && post.author ? ( // Ensure post and author exist
<div key={post._id} className="post">
<div className="post-owner">
<Avatar
src={post.author.avatar.filePath}
alt={post.author.name}
sx={{ width: 56, height: 56 }}
/>
<div className="username">
<Typography variant="subtitle1" className="post-owner-username">{post.author.name}</Typography>
</div>
<DropdownMenu postId={post._id} userEmail={post.author.email} />
</div>
<div className="post-content">
<Typography variant="body1">{post.text}</Typography>
</div>
<Box sx={{ display: 'flex', alignItems: 'center', mt: 1 }} className="post-actions">
<IconButton onClick={() => handleToggleLike(post._id)} className="like-button">
<ThumbUpIcon color={likes[post._id] ? "primary" : "action"} />
</IconButton>
<Typography variant="body2" sx={{ ml: 1 }}>
{likes[post._id] || 0}
</Typography>
<IconButton onClick={() => handleToggleTextSection(post._id)} className="comment-button">
<ChatIcon />
</IconButton>
</Box>
{post.images && post.images.map((image, index) => (
<Box key={index} sx={{ mt: 1 }}>
<img
src={image}
alt="Sample"
style={{ width: '150px', height: '150px', objectFit: 'cover', borderRadius: '8px' }}
/>
</Box>
))}
<Typography variant="body2">No more Posts to show</Typography>
</div>
</div>
);
{showTextSection[post._id] && (
<CommentTextsection
setShowThis={() => handleToggleTextSection(post._id)}
/>
)}
</div>
) : null // Render nothing if post or post.author is missing
))
) : (
<Typography variant="body2">No more Posts to show</Typography>
)}
</div>
</div>
);
};

export default AllPost;