@@ -299,13 +343,10 @@ export default function Profile() {
onClick={() => setIsReplyModalOpen(false)}
className="w-10 h-10 md:w-14 md:h-14 flex items-center justify-center bg-stone-100 hover:bg-amber-400 text-stone-900 rounded-full transition-all group active:scale-95 shrink-0"
>
-
- close
-
+ close
- {/* Comments Feed */}
{comments?.length > 0 ? (
comments.map((c) => (
@@ -320,7 +361,6 @@ export default function Profile() {
className="w-8 h-8 md:w-12 md:h-12 rounded-xl md:rounded-2xl object-cover shrink-0 shadow-md border-2 border-white ring-1 ring-stone-100"
alt="User"
/>
-
@@ -331,39 +371,28 @@ export default function Profile() {
{new Date(c.createdAt).toLocaleDateString()}
-
{user?.user?._id === c.userId?._id && (
)}
-
-
+
+ {c.comment}
+
))
) : (
-
- forum
-
-
- Be the first to speak
-
+
forum
+
Be the first to speak
)}
- {/* Textarea wrapper */}
-
- chat_bubble
-
+ chat_bubble
-
-
-
-
diff --git a/server/controllers/features/profile.controller.js b/server/controllers/features/profile.controller.js
index 4b1ebc9..1b3c963 100644
--- a/server/controllers/features/profile.controller.js
+++ b/server/controllers/features/profile.controller.js
@@ -1,4 +1,4 @@
-import { getUserMeData } from "../user.controller.js";
+import { getUserData, getUserMeData } from "../user.controller.js";
import { getFollowersData, getFollowingData } from "../user_follows.controller.js";
@@ -30,41 +30,30 @@ export const getUserMeProfile = async (req, res) => {
}
};
+export const getUserProfile = async (req, res) => {
+ try {
+ const userId = req.params.userId;
+ const [user, followers, following] = await Promise.all([
+ getUserData(userId),
+ getFollowersData(userId),
+ getFollowingData(userId)
+ ]);
+ res.status(200).json({
+ success: true,
+ user,
+ followersCount: followers.length,
+ followers,
+ followingCount: following.length,
+ following
+ });
-
-
-
-
-
-// import { getUserMeData } from "../user.controller.js";
-// import { getFollowersData, getFollowingData } from "../user_follows.controller.js";
-
-// export const getUserMeProfile = async (req, res) => {
-// try {
-// const userId = req.user.userId;
-
-// const [user, followers, following] = await Promise.all([
-// getUserMeData(userId),
-// // getFollowersData(userId),
-// // getFollowingData(userId)
-// ]);
-
-// res.status(200).json({
-// success: true,
-// user,
-// // followersCount: followers.length,
-// // followers,
-// // followingCount: following.length,
-// // following
-// });
-
-// } catch (error) {
-// console.error(error);
-// res.status(500).json({
-// success: false,
-// error: "Server error"
-// });
-// }
-// };
\ No newline at end of file
+ } catch (error) {
+ console.error(error);
+ res.status(500).json({
+ success: false,
+ error: "Server error"
+ });
+ }
+}
\ No newline at end of file
diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js
index 8f1c727..ea0cd99 100644
--- a/server/controllers/user.controller.js
+++ b/server/controllers/user.controller.js
@@ -12,6 +12,16 @@ export const getUserMeData = async (userId) => {
}
};
+export const getUserData = async (userId) => {
+ try {
+ const user = await User.findById(userId).select("-password");
+ return user;
+ } catch (error) {
+ console.error("getUserData error:", error);
+ throw new Error("Database error while fetching user");
+ }
+}
+
export const getUserMe = async (req, res) => {
try {
const userId = req.user.userId;
@@ -35,7 +45,34 @@ export const getUserMe = async (req, res) => {
export const editProfile = async (req, res) => {
try {
const userId = req.user.userId;
- const { firstName, lastName, password, gender, about, pfp, cover } = req.body;
+ const { about, pfp, cover } = req.body;
+
+ const user = await User.findById(userId);
+ if (!user) {
+ return res.status(404).json({ success: false, error: "User not found" });
+ }
+
+ if (about) user.about = about;
+ if (pfp) user.pfp = pfp;
+ if (cover) user.cover = cover;
+
+ await user.save();
+
+ const userObj = user.toObject();
+ delete userObj.password;
+
+ res.status(200).json({ success: true, user: userObj });
+
+ } catch (error) {
+ console.error(error);
+ res.status(500).json({ success: false, error: "Server error" });
+ }
+};
+
+export const updateAccountSettings = async (req, res) => {
+ try {
+ const userId = req.user.userId;
+ const { firstName, lastName, password, gender } = req.body;
const user = await User.findById(userId);
if (!user) {
@@ -48,9 +85,6 @@ export const editProfile = async (req, res) => {
user.password = await bcrypt.hash(password, 10);
}
if (gender) user.gender = gender;
- if (about) user.about = about;
- if (pfp) user.pfp = pfp;
- if (cover) user.cover = cover;
await user.save();
@@ -58,7 +92,7 @@ export const editProfile = async (req, res) => {
delete userObj.password;
res.status(200).json({ success: true, user: userObj });
-
+
} catch (error) {
console.error(error);
res.status(500).json({ success: false, error: "Server error" });
@@ -68,7 +102,7 @@ export const editProfile = async (req, res) => {
export const getUser = async (req, res) => {
try {
const userId = req.params.userId;
- const user = await User.findById(userId).select('-password');
+ const user = await getUserData(userId);
if (!user) {
return res.status(404).json({ success: false, error: "User not found" });
diff --git a/server/routes/features/profile.route.js b/server/routes/features/profile.route.js
index 9ef855b..3c036e5 100644
--- a/server/routes/features/profile.route.js
+++ b/server/routes/features/profile.route.js
@@ -1,10 +1,12 @@
import { Router } from "express";
import authorize from '../../middlewares/auth.middleware.js';
-import { getUserMeProfile } from "../../controllers/features/profile.controller.js";
+import { getUserMeProfile, getUserProfile } from "../../controllers/features/profile.controller.js";
const profileRouter = Router();
profileRouter.get('/profile/me', authorize, getUserMeProfile);
+profileRouter.get('/user/:userId', authorize, getUserProfile);
+
export default profileRouter;
\ No newline at end of file
diff --git a/server/routes/user.route.js b/server/routes/user.route.js
index 65fc621..76d45ab 100644
--- a/server/routes/user.route.js
+++ b/server/routes/user.route.js
@@ -1,16 +1,16 @@
import { Router } from "express";
import authorize from "../middlewares/auth.middleware.js";
-import { getUserMe, editProfile, getUser, searchUsers } from "../controllers/user.controller.js";
+import { getUserMe, editProfile, getUser, searchUsers, updateAccountSettings } from "../controllers/user.controller.js";
const userRouter = Router();
userRouter.get('/me', authorize, getUserMe);
-userRouter.put('/me', authorize, editProfile);
+userRouter.put('/me/edit', authorize, editProfile);
-userRouter.get('/search', authorize, searchUsers);
+userRouter.put('/me/update-account', authorize, updateAccountSettings);
-userRouter.get('/:userId', authorize, getUser);
+userRouter.get('/search', authorize, searchUsers);
export default userRouter;
\ No newline at end of file