Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anandhex committed Apr 28, 2020
1 parent cf45446 commit 44123b0
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 55 deletions.
8 changes: 7 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ class App extends Component {
<Route
exact
path="/user/:id"
render={(routeParams) => <MeSection {...routeParams} />}
render={(routeParams) => (
<MeSection
currentUser={this.props.user}
{...routeParams}
isNotUser={true}
/>
)}
/>
<Route
exact
Expand Down
37 changes: 36 additions & 1 deletion client/src/Pages/Home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,42 @@ class Welcome extends Component {
this.setState({ isLoading: false });
}
}

async componentDidUpdate(prevProps, prevState) {
if (prevProps.isRecommended !== this.props.isRecommended) {
let resp;
try {
this.setState({ isLoading: true });
let api = API_BASE_URL + "users";
const headers = jwt.getAuthHeader();
if (this.props.user) {
resp = await axios.get(api + "/getFriendRecommendation", {
headers,
});
this.setState({ friends: resp.data.data });
api =
api +
`/${jwt.getId()}/posts/getRecommendPost${
this.props.isRecommended ? "?recommend=true" : ""
}`;
resp = await axios.get(api, { headers });
this.setState({ posts: resp.data.data.posts });
} else {
api = API_BASE_URL + "users/posts";
resp = await axios.get(api);
this.setState({ posts: resp.data.data.posts });
}
} catch (err) {
console.log(err);
if (!err.response) {
toast.error("Something went wrong!");
} else {
toast.error(err.response.data.message);
}
} finally {
this.setState({ isLoading: false });
}
}
}
addFriend = async (e, id) => {
try {
e.stopPropagation();
Expand Down
91 changes: 91 additions & 0 deletions client/src/Pages/Me/DashbBoard/Follower/Follower.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { Component } from "react";
import { API_BASE_URL } from "../../../../util/apiUtil";
import axios from "axios";
import jwt from "../../../../util/jwt";
import { getUserProfileImage } from "../../../../util/commonMethods";
import Loader from "../../../../Components/Loader/Loader";
import { toast } from "react-toastify";
export class Follower extends Component {
constructor(props) {
super(props);
this.state = {
friends: [],
isLoading: false,
};
}
async componentDidMount() {
const api = API_BASE_URL + `users/getFollowers/${jwt.getId()}`;
const headers = jwt.getAuthHeader();
try {
const resp = await axios.get(api, { headers });
this.setState({ friends: resp.data.data.data });
} catch (err) {
console.log(err);
if (!err.response) {
toast.error("Something went wrong!");
} else {
toast.error(err.response.data.message);
}
}
}
showUserProfile = (e, id) => {
e.stopPropagation();
this.props.history.push(`/user/${id}`);
};
removeFriend = async (e, id) => {
e.stopPropagation();
this.setState({ isLoading: true });
const headers = jwt.getAuthHeader();
const api = API_BASE_URL + `users/friends/${id}`;
try {
const resp = await axios.delete(api, { headers });
this.setState({
friends: resp.data.data.user.friends,
});
this.props.setUser(resp.data.data.user);
toast.info("Removed friend!");
} catch (err) {
console.log(err);
if (!err.response) {
toast.error("Something went wrong!");
} else {
toast.error(err.response.data.message);
}
} finally {
this.setState({ isLoading: false });
}
};
renderFriends = () => {
return this.state.friends.map((friend) => (
<div
key={friend && friend._id}
className="friend-single"
onClick={(e) => this.showUserProfile(e, friend._id)}
>
<div className="friend-image">
<img
src={getUserProfileImage(friend && friend.profile_img)}
alt="profile"
/>
</div>
<div className="friend-username">{friend && friend.username}</div>
</div>
));
};
render() {
return (
<>
{this.state.isLoading ? <Loader /> : ""}
<div className="page">
{this.state.friends.length ? (
<div className="friends-container">{this.renderFriends()}</div>
) : (
<div className="empty-friends">You have no followers </div>
)}
</div>
</>
);
}
}

export default Follower;
4 changes: 4 additions & 0 deletions client/src/Pages/Me/DashbBoard/MeSection/MeSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export class MeSection extends Component {
</>
);
};
renderFollowButton = () => {
console.log(this.props);
};
handleCategory = (e, category) => {
e.stopPropagation();
this.props.history.push(`/specficPostCategory/${category}`);
Expand Down Expand Up @@ -219,6 +222,7 @@ export class MeSection extends Component {
<div className="dashboard-count dashboard-dislikes">1</div>
</div> */}
</div>
{this.props.isNotUser ? this.renderFollowButton() : ""}
<div className="dashboard-top-posts-container">
<div className="dashboard-top-posts-header">Top Posts</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions client/src/Pages/Me/Me.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Friends from "./DashbBoard/Friends/Friends";
import Posts from "./DashbBoard/Posts/Posts";
import Saved from "./DashbBoard/Saved/Saved";
import Error from "../Error/Error";
import { Follower } from "./DashbBoard/Follower/Follower";

export class Me extends Component {
constructor(props) {
Expand Down Expand Up @@ -58,6 +59,8 @@ export class Me extends Component {
return <Posts user={this.props.user} {...this.props} />;
case "Saved":
return <Saved user={this.props.user} {...this.props} />;
case "Followers":
return <Follower user={this.props.user} {...this.props} />;
default:
return <Error />;
}
Expand Down Expand Up @@ -88,6 +91,9 @@ export class Me extends Component {
<div id="Friends" onClick={(e) => this.setSelectedSection(e)}>
Friends
</div>
<div id="Followers" onClick={(e) => this.setSelectedSection(e)}>
Follower
</div>
<div id="Posts" onClick={(e) => this.setSelectedSection(e)}>
Posts
</div>
Expand Down
13 changes: 13 additions & 0 deletions client/src/Pages/ShowPost/ShowPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class ShowPost extends Component {
commentText: "",
});
toast.info("Comment added!");
const message = resp.data.data.message;
if (message) {
toast.error(message);
}
} catch (err) {
console.log(err);
if (!err.response) {
Expand Down Expand Up @@ -101,6 +105,11 @@ export class ShowPost extends Component {
isEdit: false,
});
toast.info("Comment edited!");
const message = resp.data.data.message;
console.log(message);
if (message) {
toast.error(message);
}
} catch (err) {
if (!err.response) {
toast.error("Something went wrong!");
Expand Down Expand Up @@ -290,6 +299,10 @@ export class ShowPost extends Component {
editPostContent: null,
});
toast.info("Updated the post!");
const message = resp.data.data.message;
if (message) {
toast.error(message);
}
} catch (err) {
console.log(err);
if (!err.response) {
Expand Down
54 changes: 30 additions & 24 deletions server/controllers/commentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,47 @@ exports.addComment = catchAsync(async (req, res, next) => {
req.body.userId = req.user._id;
req.body.postId = req.params.postId;
const resp = await axios.get(
`http://127.0.0.1:5001/predict-sentiment?comment=${req.body.commentText}`
`http://127.0.0.1:5001/predict-sentiment?comment=${encodeURI(
req.body.commentText
)}`
);
if (resp.data.Sentiment === 'Positive') {
const comment = await Comment.create(req.body);
let post = await Post.findById(req.body.postId);
post.comments = [...post.comments, comment._id];
post = await Post.findByIdAndUpdate(post._id, post, { new: true }).populate(
'comments'
);
res.status(201).json({ status: 'success', data: { data: post } });
} else {
return next(new AppError('Comment is inappropriate', 403));
const comment = await Comment.create(req.body);
let post = await Post.findById(req.body.postId);
post.comments = [...post.comments, comment._id];
post = await Post.findByIdAndUpdate(post._id, post, { new: true }).populate(
'comments'
);
let message;
if (resp.data.Sentiment === 'Negative') {
message = 'Please refrain from typing such post/comments';
}
res.status(201).json({ status: 'success', data: { data: post, message } });
});

exports.updateComment = catchAsync(async (req, res, next) => {
let comment = await Comment.findOne({ _id: req.params.commentId });
if (comment) {
if (req.user._id.equals(comment.userId)) {
const resp = await axios.get(
`http://127.0.0.1:5001/predict-sentiment?comment=${req.body.commentText}`
`http://127.0.0.1:5001/predict-sentiment?comment=${encodeURI(
req.body.commentText
)}`
);
if (resp.data.Sentiment === 'Positive') {
comment = await Comment.findByIdAndUpdate(
req.params.commentId,
req.body,
{
new: true
}
);
let post = await Post.findById(comment.postId).populate('comments');
res.status(200).json({ status: 'success', data: { data: post } });
} else {
return next(new AppError('Comment is inappropriate', 403));
let message;
if (resp.data.Sentiment === 'Negative') {
message = 'Please refrain from typing such post/comments';
}
comment = await Comment.findByIdAndUpdate(
req.params.commentId,
req.body,
{
new: true
}
);
let post = await Post.findById(comment.postId).populate('comments');
res
.status(200)
.json({ status: 'success', data: { data: post, message } });
} else {
next(new AppError('Unauthorized. Please login to update the post', 403));
}
Expand Down
Loading

0 comments on commit 44123b0

Please sign in to comment.