Skip to content

Commit a6a5fee

Browse files
authored
Merge pull request #12 from Tarik-Anowar/recruitifyv1
Recruitifyv1
2 parents ac52d12 + ffc0a01 commit a6a5fee

File tree

21 files changed

+629
-242
lines changed

21 files changed

+629
-242
lines changed

backend/controllers/postController.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,42 @@ exports.createPost = catchAsyncErrors(async (req, res) => {
2222
res.status(201).json(savedPost);
2323
console.log("successful.. + ");
2424

25-
2625
} catch (error) {
2726
console.error(error);
2827
res.status(500).json({ error: 'Internal server error' });
2928
}
3029
});
3130

32-
exports.getPost = catchAsyncErrors(async (req, res) => {
31+
32+
exports.getMyPost = catchAsyncErrors(async (req, res) => {
3333
try {
34-
const email = req.params.email;
35-
console.log("email== " + email);
36-
const author = await Userauth.findOne({ email: email });
37-
let posts;
38-
if (email) {
39-
posts = await Post.find({ 'author': author._id }).populate('author').sort({timestamp:-1});
40-
} else {
41-
posts = await Post.find().populate('author').sort({timestamp:-1});
34+
const userId = req.user._id;
35+
const posts = await Post.find({ 'author': userId })
36+
.populate({
37+
path: 'author',
38+
select: 'name email avatar'
39+
})
40+
.sort({ timestamp: -1 });
41+
42+
if (!posts || posts.length === 0) {
43+
return res.status(404).json({ error: 'Posts not found' });
4244
}
45+
res.status(200).json(posts);
46+
} catch (error) {
47+
console.error(error);
48+
res.status(500).json({ error: 'Internal server error' });
49+
}
50+
});
51+
52+
exports.getAllPost = catchAsyncErrors(async (req, res) => {
53+
try {
54+
const posts = await Post.find()
55+
.populate({
56+
path: 'author',
57+
select: 'name email avatar'
58+
})
59+
.sort({ timestamp: -1 });
4360

44-
console.log("posts == " + posts);
4561
if (!posts || posts.length === 0) {
4662
return res.status(404).json({ error: 'Posts not found' });
4763
}
@@ -54,7 +70,8 @@ exports.getPost = catchAsyncErrors(async (req, res) => {
5470

5571
exports.deletePost = catchAsyncErrors(async (req, res) => {
5672
try {
57-
const postId = req.params.id;
73+
const postId = req.params.id;
74+
console.log("psot id = "+postId);
5875

5976
if (!mongoose.isValidObjectId(postId)) {
6077
return res.status(400).json({ error: 'Invalid post ID' });
@@ -65,6 +82,12 @@ exports.deletePost = catchAsyncErrors(async (req, res) => {
6582
if (!post) {
6683
return res.status(404).json({ error: 'Post not found' });
6784
}
85+
86+
// Check if the user is the author of the post
87+
if (post.author.toString() !== req.user._id.toString()) {
88+
return res.status(403).json({ error: 'Unauthorized to delete this post' });
89+
}
90+
6891
await post.remove();
6992

7093
res.status(200).json({ message: 'Post deleted successfully' });

backend/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ process.on("unhandledRejection", (err) => {
9595
server.close(() => {
9696
process.exit(1);
9797
});
98-
});
98+
});

backend/routes/postRouter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const express = require("express");
22
const router = express.Router();
33

4-
const {createPost, getPost, deletePost} = require("../controllers/postController");
4+
const {createPost, deletePost, getMyPost, getAllPost} = require("../controllers/postController");
55
const { isAuthenticatedUser} = require("../middleware/auth");
66

77
router.route("/createpost").post(isAuthenticatedUser,createPost);
8-
router.route("/getposts/:email").get(isAuthenticatedUser,getPost);
9-
router.route("/deletepost:id").delete(isAuthenticatedUser,deletePost);
8+
router.route("/getMyPosts").get(isAuthenticatedUser,getMyPost);
9+
router.route("/getAllPosts").get(isAuthenticatedUser,getAllPost);
10+
router.route("/deletePost/:id").delete(isAuthenticatedUser,deletePost);
1011
// router.route("/savepost").post(isAuthenticatedUser,)
1112
// router.route("/editpost").post(isAuthenticatedUser)
1213

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@
5353
"last 1 safari version"
5454
]
5555
}
56-
}
56+
}

frontend/src/Dashboard/AppBar/AppBar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const MainContainer = styled("div")({
2020
const AppBar = () => {
2121
return (
2222
<MainContainer>
23-
<ChosenOptionLabel />
24-
<DropdownMenu />
23+
{/* <ChosenOptionLabel /> */}
24+
{/* <DropdownMenu /> */}
2525
</MainContainer>
2626
);
2727
};

frontend/src/Dashboard/Dashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const Dashboard = ({ isUserInRoom }) => {
4949
<SideBar />
5050
<FriendsSideBar />
5151
<Messenger />
52-
<AppBar />
52+
{/* <AppBar /> */}
5353
{isUserInRoom && <Room />}
5454
</Wrapper>
5555
);

frontend/src/Dashboard/Messenger/Messenger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const MainContainer = styled("div")({
99
backgroundColor: "#36393f",
1010
// marginTop: "48px",
1111
display: "flex",
12-
// height: "100%",
12+
height: "100vh",
1313
});
1414

1515
const Messenger = () => {

frontend/src/Dashboard/Messenger/MessengerContent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010

1111
const Wrapper = styled("div")({
1212
flexGrow: 1,
13+
height:'90vh'
1314
});
1415

1516
const MessengerContent = (props) => {

frontend/src/User/createPost.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// src/shared/components/CreatePost.js
2+
import React from 'react';
3+
import { CloseIcon } from '@chakra-ui/icons';
4+
import CteatepostTextsection from '../shared/components/CreatepostTextSection';
5+
// import './CreatePost.css';
6+
7+
8+
const CreatePost = ({ showModal, handleShowCreatePost }) => {
9+
return (
10+
showModal && (
11+
<div className="modal">
12+
<div className="modal-content">
13+
<CloseIcon onClick={handleShowCreatePost} className="close-button" />
14+
<br />
15+
<CteatepostTextsection setShowThis={handleShowCreatePost} />
16+
</div>
17+
</div>
18+
)
19+
);
20+
};
21+
22+
export default CreatePost;

frontend/src/User/myPosts.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { useState, useEffect } from 'react';
2+
import { useDispatch, useSelector } from 'react-redux';
3+
import { Avatar, Box, Button, IconButton, Typography } from '@mui/material';
4+
import { Chat as ChatIcon, ThumbUp as ThumbUpIcon, ThumbDown as ThumbDownIcon } from '@mui/icons-material';
5+
import DropdownMenu from '../shared/components/HamburgerDropdown';
6+
import { fetchMyPosts } from '../store/slices/postSlice';
7+
import usePostChange from './IsChange';
8+
import '../styles/posts.css';
9+
import CreatePost from './createPost';
10+
import CommentTextsection from '../shared/components/CommentTextSection';
11+
12+
const MyPost = () => {
13+
const { userInfo } = useSelector((state) => state.auth);
14+
const [showModal, setShowModal] = useState(false);
15+
const [showTextSection, setShowTextSection] = useState({});
16+
const dispatch = useDispatch();
17+
const myPosts = useSelector((state) => state.posts.myPosts);
18+
const [likes, setLikes] = useState({});
19+
const [likeMode, setLikeMode] = useState({});
20+
21+
useEffect(() => {
22+
dispatch(fetchMyPosts());
23+
}, [dispatch]);
24+
25+
const handleShowCreatePost = () => {
26+
setShowModal(!showModal);
27+
};
28+
29+
const handleToggleTextSection = (postId) => {
30+
setShowTextSection((prevState) => ({
31+
...prevState,
32+
[postId]: !prevState[postId],
33+
}));
34+
};
35+
36+
const handleToggleLike = (postId) => {
37+
setLikes((prevState) => {
38+
const newLikes = { ...prevState };
39+
const isLiked = likeMode[postId];
40+
41+
if (isLiked) {
42+
newLikes[postId] = Math.max(0, (newLikes[postId] || 0) - 1);
43+
} else {
44+
newLikes[postId] = (newLikes[postId] || 0) + 1;
45+
}
46+
47+
return newLikes;
48+
});
49+
setLikeMode((prevState) => ({
50+
...prevState,
51+
[postId]: !prevState[postId]
52+
}));
53+
};
54+
55+
return (
56+
<div className="posts-page">
57+
<Button
58+
variant="contained"
59+
color="primary"
60+
onClick={handleShowCreatePost}
61+
>
62+
Create Post
63+
</Button>
64+
<div className="posts">
65+
{myPosts && myPosts.map((post) => (
66+
<div key={post._id} className="post">
67+
<div className="post-owner">
68+
<Avatar
69+
src={post.author.avatar.filePath}
70+
alt={post.author.name}
71+
sx={{ width: 56, height: 56 }}
72+
/>
73+
<div className="username">
74+
<Typography variant="subtitle1">{post.author.name}</Typography>
75+
</div>
76+
<DropdownMenu postId={post._id} userEmail={post.author.email} />
77+
</div>
78+
<div className="post-content">
79+
<Typography variant="body1">{post.text}</Typography>
80+
</div>
81+
<Box sx={{ display: 'flex', alignItems: 'center', mt: 1 }}>
82+
<IconButton onClick={() => handleToggleLike(post._id)}>
83+
<ThumbUpIcon color={likeMode[post._id] ? 'primary' : 'action'} />
84+
</IconButton>
85+
<Typography variant="body2" sx={{ ml: 1 }}>
86+
{likes[post._id] || 0}
87+
</Typography>
88+
<IconButton onClick={() => handleToggleLike(post._id)}>
89+
<ThumbDownIcon />
90+
</IconButton>
91+
<IconButton onClick={() => handleToggleTextSection(post._id)}>
92+
<ChatIcon />
93+
</IconButton>
94+
</Box>
95+
{post.images && post.images.map((image, index) => (
96+
<Box key={index} sx={{ mt: 1 }}>
97+
<img
98+
src={image}
99+
alt="Sample"
100+
style={{ width: '150px', height: '150px', objectFit: 'cover', borderRadius: '8px' }}
101+
/>
102+
</Box>
103+
))}
104+
{showTextSection[post._id] && (
105+
<CommentTextsection
106+
setShowThis={() => handleToggleTextSection(post._id)}
107+
/>
108+
)}
109+
</div>
110+
))}
111+
<Typography variant="body2">No more Posts to show</Typography>
112+
</div>
113+
<CreatePost showModal={showModal} handleShowCreatePost={handleShowCreatePost} />
114+
</div>
115+
);
116+
};
117+
118+
export default MyPost;

0 commit comments

Comments
 (0)