From 2e1832a0bf12fd88102dbfd33efb5d963df59ad0 Mon Sep 17 00:00:00 2001 From: Andrew Trieu Date: Sun, 23 Jul 2023 18:56:27 +0300 Subject: [PATCH] Testing and debugging --- README.md | 2 ++ client/src/components/Friend.jsx | 10 +++++---- client/src/components/ProfilePhoto.jsx | 2 +- client/src/scenes/homePage/index.jsx | 2 +- client/src/scenes/loginPage/Form.jsx | 2 +- client/src/scenes/navbar/index.jsx | 2 +- client/src/scenes/profilePage/index.jsx | 2 +- client/src/scenes/widgets/AdsWidget.jsx | 2 +- client/src/scenes/widgets/ContentWidget.jsx | 8 +++---- client/src/scenes/widgets/FeedWidget.jsx | 4 ++-- client/src/scenes/widgets/FriendList.jsx | 24 +++++++++++---------- client/src/scenes/widgets/PostWidget.jsx | 6 +++--- client/src/scenes/widgets/UserWidget.jsx | 16 +++++++------- server/controllers/auth.js | 4 ++-- server/controllers/users.js | 5 ++++- server/data/mock.js | 22 +++++++++---------- 16 files changed, 61 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index a03fb55..9ac623a 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,5 @@ Table: | 21.07.2023 | Each post on home page | 20 | | 22.07.2023 | Ads and friend list | 10 | | 23.07.2023 | Profile page | 5 | +| 24.07.2023 | Testing and debugging | 3 | + diff --git a/client/src/components/Friend.jsx b/client/src/components/Friend.jsx index d9d6785..f98ddcd 100644 --- a/client/src/components/Friend.jsx +++ b/client/src/components/Friend.jsx @@ -6,12 +6,12 @@ import { setFriends } from "state"; import FlexBetween from "./FlexBetween"; import ProfilePhoto from "./ProfilePhoto"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const Friend = ({ friendId, userName, subtitle, profilePicturePath }) => { const dispatch = useDispatch(); const navigate = useNavigate(); - const { _id } = useSelector((state) => state.user); + const userId = useSelector((state) => state.user.user._id); const token = useSelector((state) => state.token); const friends = useSelector((state) => state.user.friends); const { palette } = useTheme(); @@ -20,10 +20,12 @@ const Friend = ({ friendId, userName, subtitle, profilePicturePath }) => { const main = palette.neutral.main; const medium = palette.neutral.medium; - const isFriend = friends.find((friend) => friend._id === friendId); + if (!friends) return null; + + const isFriend = friends.some((friend) => friend._id === friendId) || false; const handleFriend = async () => { - const response = await fetch(`${baseUrl}/users/${_id}/${friendId}`, { + const response = await fetch(`${baseUrl}/users/${userId}/${friendId}`, { method: "PATCH", headers: { Authorization: `Bearer ${token}`, diff --git a/client/src/components/ProfilePhoto.jsx b/client/src/components/ProfilePhoto.jsx index 898cb83..0361825 100644 --- a/client/src/components/ProfilePhoto.jsx +++ b/client/src/components/ProfilePhoto.jsx @@ -1,6 +1,6 @@ import { Box } from "@mui/material"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const ProfilePhoto = ({ image, size = "60px" }) => { return ( diff --git a/client/src/scenes/homePage/index.jsx b/client/src/scenes/homePage/index.jsx index c7bf5a5..17b2750 100644 --- a/client/src/scenes/homePage/index.jsx +++ b/client/src/scenes/homePage/index.jsx @@ -9,7 +9,7 @@ import FriendList from "scenes/widgets/FriendList"; const HomePage = () => { const isNotMobile = useMediaQuery("(min-width: 1000px)"); - const { _id, profilePicturePath } = useSelector((state) => state.user); + const { _id, profilePicturePath } = useSelector((state) => state.user.user); return ( diff --git a/client/src/scenes/loginPage/Form.jsx b/client/src/scenes/loginPage/Form.jsx index eabdc99..0acc20e 100644 --- a/client/src/scenes/loginPage/Form.jsx +++ b/client/src/scenes/loginPage/Form.jsx @@ -16,7 +16,7 @@ import { setLogin } from "state"; import Dropzone from "react-dropzone"; import FlexBetween from "components/FlexBetween"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const registerSchema = yup.object().shape({ firstName: yup.string().required("First name is required"), diff --git a/client/src/scenes/navbar/index.jsx b/client/src/scenes/navbar/index.jsx index df9a8a1..0beadb3 100644 --- a/client/src/scenes/navbar/index.jsx +++ b/client/src/scenes/navbar/index.jsx @@ -29,7 +29,7 @@ const Navbar = () => { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const dispatch = useDispatch(); const navigate = useNavigate(); - const user = useSelector((state) => state.user); + const user = useSelector((state) => state.user.user); const isNotMobile = useMediaQuery("(min-width: 1000px)"); const theme = useTheme(); diff --git a/client/src/scenes/profilePage/index.jsx b/client/src/scenes/profilePage/index.jsx index dddd10b..101e6dd 100644 --- a/client/src/scenes/profilePage/index.jsx +++ b/client/src/scenes/profilePage/index.jsx @@ -8,7 +8,7 @@ import PostWidget from "scenes/widgets/PostWidget"; import FeedWidget from "scenes/widgets/FeedWidget"; import UserWidget from "scenes/widgets/UserWidget"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const ProfilePage = () => { const [user, setUser] = useState(null); diff --git a/client/src/scenes/widgets/AdsWidget.jsx b/client/src/scenes/widgets/AdsWidget.jsx index 21c5fda..9395dd2 100644 --- a/client/src/scenes/widgets/AdsWidget.jsx +++ b/client/src/scenes/widgets/AdsWidget.jsx @@ -2,7 +2,7 @@ import { Typography, useTheme } from "@mui/material"; import FlexBetween from "components/FlexBetween"; import WidgetWrapper from "components/WidgetWrapper"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const AdsWidget = () => { const { palette } = useTheme(); diff --git a/client/src/scenes/widgets/ContentWidget.jsx b/client/src/scenes/widgets/ContentWidget.jsx index b85a832..fab3127 100644 --- a/client/src/scenes/widgets/ContentWidget.jsx +++ b/client/src/scenes/widgets/ContentWidget.jsx @@ -10,9 +10,9 @@ import Friend from "components/Friend"; import WidgetWrapper from "components/WidgetWrapper"; import { useState } from "react"; import { useDispatch, useSelector } from "react-redux"; -import { setPosts } from "state"; +import { setPost } from "state"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const ContentWidget = ({ postId, @@ -28,7 +28,7 @@ const ContentWidget = ({ const [isComments, setIsComments] = useState(false); const dispatch = useDispatch(); const token = useSelector((state) => state.token); - const loggedInUserId = useSelector((state) => state.user._id); + const loggedInUserId = useSelector((state) => state.user.user._id); const isLiked = Boolean(likes[loggedInUserId]); const likesCount = Object.keys(likes).length; const { palette } = useTheme(); @@ -45,7 +45,7 @@ const ContentWidget = ({ body: JSON.stringify({ userId: loggedInUserId }), }); const updatedPost = await response.json(); - dispatch(setPosts({ post: updatedPost })); + dispatch(setPost({ post: updatedPost })); }; return ( diff --git a/client/src/scenes/widgets/FeedWidget.jsx b/client/src/scenes/widgets/FeedWidget.jsx index 759c0ed..1394e1e 100644 --- a/client/src/scenes/widgets/FeedWidget.jsx +++ b/client/src/scenes/widgets/FeedWidget.jsx @@ -3,7 +3,7 @@ import { useDispatch, useSelector } from "react-redux"; import { setPosts } from "state"; import ContentWidget from "./ContentWidget"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const FeedWidget = ({ userId, isProfile = false }) => { const dispatch = useDispatch(); @@ -35,7 +35,7 @@ const FeedWidget = ({ userId, isProfile = false }) => { useEffect(() => { isProfile ? getUserPosts() : getPosts(); }, []); // eslint-disable-line react-hooks/exhaustive-deps - + console.log(posts); return ( <> {posts.map( diff --git a/client/src/scenes/widgets/FriendList.jsx b/client/src/scenes/widgets/FriendList.jsx index 1e40d93..86cc2ac 100644 --- a/client/src/scenes/widgets/FriendList.jsx +++ b/client/src/scenes/widgets/FriendList.jsx @@ -5,7 +5,7 @@ import { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { setFriends } from "state"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const FriendList = ({ userId }) => { const dispatch = useDispatch(); @@ -36,18 +36,20 @@ const FriendList = ({ userId }) => { fontWeight="500" sx={{ mb: "1.5rem" }} > - Great minds you know + Connected great minds - {friends.map((friend) => ( - - ))} + {friends && + friends.length > 0 && + friends.map((friend) => ( + + ))} ); diff --git a/client/src/scenes/widgets/PostWidget.jsx b/client/src/scenes/widgets/PostWidget.jsx index 4910544..b874053 100644 --- a/client/src/scenes/widgets/PostWidget.jsx +++ b/client/src/scenes/widgets/PostWidget.jsx @@ -25,7 +25,7 @@ import { useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { setPosts } from "state"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const PostWidget = ({ profilePicturePath }) => { const dispatch = useDispatch(); @@ -33,7 +33,7 @@ const PostWidget = ({ profilePicturePath }) => { const [image, setImage] = useState(null); const [post, setPost] = useState(""); const { palette } = useTheme(); - const { _id } = useSelector((state) => state.user); + const userId = useSelector((state) => state.user.user._id); const token = useSelector((state) => state.token); const isNotMobile = useMediaQuery("(min-width: 1000px)"); const mediumMain = palette.neutral.mediumMain; @@ -41,7 +41,7 @@ const PostWidget = ({ profilePicturePath }) => { const handlePost = async () => { const formData = new FormData(); - formData.append("userId", _id); + formData.append("userId", userId); formData.append("content", post); if (image) { formData.append("contentPicture", image); diff --git a/client/src/scenes/widgets/UserWidget.jsx b/client/src/scenes/widgets/UserWidget.jsx index 7052413..d4b82d6 100644 --- a/client/src/scenes/widgets/UserWidget.jsx +++ b/client/src/scenes/widgets/UserWidget.jsx @@ -3,6 +3,9 @@ import { EditOutlined, LocationOnOutlined, FavoriteOutlined, + FacebookOutlined, + Instagram, + LinkedIn, } from "@mui/icons-material"; import { Box, Typography, Divider, useTheme } from "@mui/material"; import ProfilePhoto from "components/ProfilePhoto"; @@ -12,7 +15,7 @@ import { useSelector } from "react-redux"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; -const baseUrl = process.env.REACT_APP_BASE_URL; +const baseUrl = process.env.REACT_APP_BASE_URL || "http://localhost:3001"; const UserWidget = ({ userId, profilePicturePath }) => { const [user, setUser] = useState(null); @@ -64,7 +67,7 @@ const UserWidget = ({ userId, profilePicturePath }) => { {firstName} {lastName} - {friends.length} friend(s) + {friends && friends.length} friend(s) @@ -89,7 +92,7 @@ const UserWidget = ({ userId, profilePicturePath }) => { - facebook + Facebook @@ -101,10 +104,7 @@ const UserWidget = ({ userId, profilePicturePath }) => { - instagram + Instagram @@ -116,7 +116,7 @@ const UserWidget = ({ userId, profilePicturePath }) => { - linkedin + LinkedIn diff --git a/server/controllers/auth.js b/server/controllers/auth.js index e710348..fe1e273 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -71,12 +71,12 @@ export const login = async (req, res) => { const validPassword = await bcrypt.compare(password, user.password); if (!validPassword) return res.status(400).json({ msg: "Wrong password" }); - const accessToken = jwt.sign( + const token = jwt.sign( { id: user._id, admin: user.admin }, process.env.JWT_SECRET ); delete user.password; - res.status(200).json({ accessToken, user }); + res.status(200).json({ token, user }); } catch (error) { res.status(500).json({ error: error.message }); } diff --git a/server/controllers/users.js b/server/controllers/users.js index 05c6a94..633126f 100644 --- a/server/controllers/users.js +++ b/server/controllers/users.js @@ -55,6 +55,9 @@ export const getFriends = async (req, res) => { export const patchFriend = async (req, res) => { try { const { userId, friendId } = req.params; + if (userId === friendId) { + return res.status(400).json({ error: "You cannot add yourself" }); + } const user = await User.findById(userId); const friend = await User.findById(friendId); @@ -63,7 +66,7 @@ export const patchFriend = async (req, res) => { friend.friends = friend.friends.filter((id) => id !== id); } else { user.friends.push(friendId); - friend.friends.push(id); + friend.friends.push(userId); } await user.save(); diff --git a/server/data/mock.js b/server/data/mock.js index 25ef5c9..04ba707 100644 --- a/server/data/mock.js +++ b/server/data/mock.js @@ -36,7 +36,7 @@ export const mockUsers = [ firstName: "Aake", lastName: "Haapala", email: "aake.haapala@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", // 123456## + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", // 123456## profilePicturePath: "p1.jpeg", friends: [], location: "Helsinki, Finland", @@ -51,7 +51,7 @@ export const mockUsers = [ firstName: "Eelis", lastName: "Janhunen", email: "eelis.janhunen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e9", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p2.jpeg", friends: [], location: "Hämeenlinna, Finland", @@ -66,7 +66,7 @@ export const mockUsers = [ firstName: "Eevert", lastName: "Kotilainen", email: "evert.kotilainen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p3.jpeg", friends: [], location: "Kerava, Finland", @@ -81,7 +81,7 @@ export const mockUsers = [ firstName: "Krista", lastName: "Manninen", email: "krista.manninen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p4.jpeg", friends: [], location: "Espoo, Finland", @@ -96,7 +96,7 @@ export const mockUsers = [ firstName: "Taina", lastName: "Ruotsalainen", email: "taina.ruotsalainen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p5.jpeg", friends: [], location: "Turku, Finland", @@ -111,7 +111,7 @@ export const mockUsers = [ firstName: "Lea", lastName: "Varis", email: "lea.varis@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p6.jpeg", friends: [], location: "Tampere, Finland", @@ -126,7 +126,7 @@ export const mockUsers = [ firstName: "Elisa", lastName: "Järvelä", email: "elisa.jarvela@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p7.jpeg", friends: [], location: "Riihimäki, Finland", @@ -141,7 +141,7 @@ export const mockUsers = [ firstName: "Joonas", lastName: "Koivisto", email: "joonas.koivisto@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p8.jpeg", friends: [], location: "Rovaniemi, Finland", @@ -156,7 +156,7 @@ export const mockUsers = [ firstName: "Johanna", lastName: "Mäkinen", email: "johanna.makinen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p9.jpeg", friends: [], location: "Oulu, Finland", @@ -171,7 +171,7 @@ export const mockUsers = [ firstName: "Johannes", lastName: "Korhonen", email: "johannes.korhonen@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p10.jpeg", friends: [], location: "Kuopio, Finland", @@ -186,7 +186,7 @@ export const mockUsers = [ firstName: "Mikko", lastName: "Koivu", email: "mikko.koivu@mail.com", - password: "$2a$04$LzaBXTtLrOglvkHIPcAYKevvIOLoPZCm48DdQr.rf1gg5cIiehV4e", + password: "$2b$04$EYpcRcVBZP35DXZdQbXWSec1Mxh.H3HXqPRBYhjIw1/WXcadzmiti", profilePicturePath: "p11.jpeg", friends: [], location: "Jyväskylä, Finland",