Skip to content

Commit

Permalink
Merge pull request #4 from shrushti2000/profile
Browse files Browse the repository at this point in the history
Profile ,Bookmark
  • Loading branch information
shrushti2000 authored Jun 12, 2022
2 parents 3deb298 + 657508c commit 367129f
Show file tree
Hide file tree
Showing 23 changed files with 769 additions and 209 deletions.
18 changes: 14 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { useEffect } from "react";
import { Profiler, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import "./App.css";
import { Header } from "./components";
import { getAllBokmarkedPosts, getAllPosts, getUserPost } from "./features/postSlice";
import { getAllUser } from "./features/userSlice";
import logo from "./logo.png";
import { FeedPage } from "./pages";
import { FeedPage, UserProfile } from "./pages";
import { BookmarkPage } from "./pages/BookmarkPage/BookmarkPage";
import { Landingpage } from "./pages/Landingpage/Landingpage";
import { Signin } from "./pages/Signin/Signin";
import { Signup } from "./pages/Signup/Signup";
import { SuggestedUserProfile } from "./pages/SuggestedUserProfile/SuggestedUserProfile";
function App() {
const { token } = useSelector((state) => state.authentication);
const dispatch=useDispatch()
const {allUsers}=useSelector(state=>state.user)

const {allPosts}=useSelector(state=>state.post)
const {user}=useSelector(state=>state.authentication)
useEffect(()=>{
if(token){
dispatch(getAllUser())
dispatch(getAllPosts())
dispatch(getAllBokmarkedPosts())
dispatch(getUserPost(user.username))
}


},[token,allUsers])
},[token])
return (
<>
<BrowserRouter>
Expand All @@ -29,6 +36,9 @@ function App() {
<Route path="/signin" element={<Signin />} />
<Route path="/signup" element={<Signup />} />
<Route path="/feedpage" element={<FeedPage />} />
<Route path="/profile" element={<UserProfile/>}/>
<Route path="/bookmark" element={<BookmarkPage/>}/>
<Route path="/user-profile/:username" element={<SuggestedUserProfile/>}/>
</Routes>
</BrowserRouter>
</>
Expand Down
30 changes: 30 additions & 0 deletions src/Services/bookmarkService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import axios from "axios";

export const getAllBookmarkService = (token) =>
axios.get("/api/users/bookmark", {
headers: {
authorization: token,
},
});

export const addBookmarkService = (postId, token) =>
axios.post(
`/api/users/bookmark/${postId}`,
{},
{
headers: {
authorization: token,
},
}
);

export const removeBookmarkService = (postId, token) =>
axios.post(
`/api/users/remove-bookmark/${postId}`,
{},
{
headers: {
authorization: token,
},
}
);
8 changes: 4 additions & 4 deletions src/Services/commentService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

export const addCommentService = (postId, commentData, token) => {
export const addCommentService = (postId, commentData, token) =>
axios.post(
`/api/comments/add/${postId}`,
{
Expand All @@ -12,9 +12,9 @@ export const addCommentService = (postId, commentData, token) => {
},
}
);
};

export const editCommentService = (postId, commentId, commentData, token) => {

export const editCommentService = (postId, commentId, commentData, token) =>
axios.post(
`/api/comments/edit/${postId}/${commentId}`,
{
Expand All @@ -26,7 +26,7 @@ export const editCommentService = (postId, commentId, commentData, token) => {
},
}
);
};


export const deleteCommentService = (postId, commentId, token) =>
axios.post(
Expand Down
19 changes: 9 additions & 10 deletions src/Services/postService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import axios from "axios"

export const getAllPostService=()=>axios.get("/api/posts");
export const getUserPostService=(username)=>axios.get(`/api/posts/user/${username}`)
export const addPostService=(postData,token)=>{
export const addPostService=(postData,token)=>
axios.post("/api/posts",{postData},{headers:{
authorization:token
}})
}
export const editPostService=(postData,token)=>{

export const editPostService=(postData,token)=>
axios.post(`/api/posts/edit/${postData._id}`,{
postData
},{
Expand All @@ -16,28 +16,27 @@ export const editPostService=(postData,token)=>{
}
})

}

export const deleteUserPostService=(postId,token)=>{

export const deleteUserPostService=(postId,token)=>
axios.delete(`/api/posts/${postId}`,{
headers:{
authorization:token
}
})
}

export const likePostService=(postId,token)=>{

export const likePostService=(postId,token)=>
axios.post(`/api/posts/like/${postId}`,{},{
headers:{
authorization:token
}
})
}

export const dislikePostService=(postId,token)=>{

export const dislikePostService=(postId,token)=>
axios.post(`/api/posts/dislike/${postId}`,{},{
headers:{
authorization:token
}
})
}
19 changes: 10 additions & 9 deletions src/Services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import axios from "axios"

export const getAllUserService=()=>axios.get("/api/users")

export const updateUserService=(token,userData)=>{
return axios.post("/api/users/edit",{userData},{
export const updateUserService=(token,userData)=>
axios.post("/api/users/edit",{userData},{
headers:{
authorization:token
}
})
}

export const followUserService=(token,userId)=>{
return axios.post(`/api/users/follow/${userId}`,{},{

export const followUserService=(token,userId)=>
axios.post(`/api/users/follow/${userId}`,{},{
headers:{
authorization:token
}
})
}
export const unfollowUserService=(token,userId)=>{
return axios.post(`/api/users/unfollow/${userId}`,{},{



export const unfollowUserService=(token,userId)=>
axios.post(`/api/users/unfollow/${userId}`,{},{
headers:{
authorization:token
}
})
}
48 changes: 13 additions & 35 deletions src/backend/controllers/PostController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ export const createPostHandler = function (schema, request) {
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The username you entered is not Registered. Not Found error"],
}
);
}
Expand All @@ -86,6 +84,8 @@ export const createPostHandler = function (schema, request) {
likedBy: [],
dislikedBy: [],
},
comments: [],
bookmark: [],
username: user.username,
createdAt: formatDate(),
updatedAt: formatDate(),
Expand Down Expand Up @@ -116,9 +116,7 @@ export const editPostHandler = function (schema, request) {
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The username you entered is not Registered. Not Found error"],
}
);
}
Expand Down Expand Up @@ -161,24 +159,16 @@ export const likePostHandler = function (schema, request) {
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The username you entered is not Registered. Not Found error"],
}
);
}
const postId = request.params.postId;
const post = schema.posts.findBy({ _id: postId }).attrs;
if (post.likes.likedBy.some((currUser) => currUser._id === user._id)) {
return new Response(
400,
{},
{ errors: ["Cannot like a post that is already liked. "] }
);
return new Response(400, {}, { errors: ["Cannot like a post that is already liked. "] });
}
post.likes.dislikedBy = post.likes.dislikedBy.filter(
(currUser) => currUser._id !== user._id
);
post.likes.dislikedBy = post.likes.dislikedBy.filter((currUser) => currUser._id !== user._id);
post.likes.likeCount += 1;
post.likes.likedBy.push(user);
this.db.posts.update({ _id: postId }, { ...post, updatedAt: formatDate() });
Expand Down Expand Up @@ -207,20 +197,14 @@ export const dislikePostHandler = function (schema, request) {
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The username you entered is not Registered. Not Found error"],
}
);
}
const postId = request.params.postId;
let post = schema.posts.findBy({ _id: postId }).attrs;
if (post.likes.likeCount === 0) {
return new Response(
400,
{},
{ errors: ["Cannot decrement like less than 0."] }
);
return new Response(400, {}, { errors: ["Cannot decrement like less than 0."] });
}
if (post.likes.dislikedBy.some((currUser) => currUser._id === user._id)) {
return new Response(
Expand All @@ -230,9 +214,7 @@ export const dislikePostHandler = function (schema, request) {
);
}
post.likes.likeCount -= 1;
const updatedLikedBy = post.likes.likedBy.filter(
(currUser) => currUser._id !== user._id
);
const updatedLikedBy = post.likes.likedBy.filter((currUser) => currUser._id !== user._id);
post.likes.dislikedBy.push(user);
post = { ...post, likes: { ...post.likes, likedBy: updatedLikedBy } };
this.db.posts.update({ _id: postId }, { ...post, updatedAt: formatDate() });
Expand Down Expand Up @@ -260,9 +242,7 @@ export const deletePostHandler = function (schema, request) {
404,
{},
{
errors: [
"The username you entered is not Registered. Not Found error",
],
errors: ["The username you entered is not Registered. Not Found error"],
}
);
}
Expand All @@ -273,9 +253,7 @@ export const deletePostHandler = function (schema, request) {
400,
{},
{
errors: [
"Cannot delete a Post doesn't belong to the logged in User.",
],
errors: ["Cannot delete a Post doesn't belong to the logged in User."],
}
);
}
Expand All @@ -290,4 +268,4 @@ export const deletePostHandler = function (schema, request) {
}
);
}
};
};
16 changes: 9 additions & 7 deletions src/backend/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getBookmarkPostsHandler = function (schema, request) {
* send POST Request at /api/users/bookmark/:postId/
* */

export const bookmarkPostHandler = function (schema, request) {
export const bookmarkPostHandler = function (schema, request) {
const { postId } = request.params;
const post = schema.posts.findBy({ _id: postId }).attrs;
const user = requiresAuth.call(this, request);
Expand All @@ -122,7 +122,7 @@ export const bookmarkPostHandler = function (schema, request) {
);
}
const isBookmarked = user.bookmarks.some(
(currPost) => currPost._id === postId
(currPostId) => currPostId === postId
);
if (isBookmarked) {
return new Response(
Expand All @@ -131,7 +131,7 @@ export const bookmarkPostHandler = function (schema, request) {
{ errors: ["This Post is already bookmarked"] }
);
}
user.bookmarks.push(post);
user.bookmarks.push(postId);
this.db.users.update(
{ _id: user._id },
{ ...user, updatedAt: formatDate() }
Expand All @@ -148,12 +148,13 @@ export const bookmarkPostHandler = function (schema, request) {
}
};


/**
* This handler handles adding a post to user's bookmarks in the db.
* send POST Request at /api/users/remove-bookmark/:postId/
* */

export const removePostFromBookmarkHandler = function (schema, request) {
export const removePostFromBookmarkHandler = function (schema, request) {
const { postId } = request.params;
let user = requiresAuth.call(this, request);
try {
Expand All @@ -169,13 +170,13 @@ export const removePostFromBookmarkHandler = function (schema, request) {
);
}
const isBookmarked = user.bookmarks.some(
(currPost) => currPost._id === postId
(currPostId) => currPostId === postId
);
if (!isBookmarked) {
return new Response(400, {}, { errors: ["Post not bookmarked yet"] });
}
const filteredBookmarks = user.bookmarks.filter(
(currPost) => currPost._id !== postId
(currPostId) => currPostId !== postId
);
user = { ...user, bookmarks: filteredBookmarks };
this.db.users.update(
Expand All @@ -194,6 +195,7 @@ export const removePostFromBookmarkHandler = function (schema, request) {
}
};


/**
* This handler handles follow action.
* send POST Request at /api/users/follow/:followUserId/
Expand Down Expand Up @@ -318,4 +320,4 @@ export const unfollowUserHandler = function (schema, request) {
}
);
}
};
};
Loading

0 comments on commit 367129f

Please sign in to comment.