diff --git a/backend/controllers/userController.js b/backend/controllers/userController.js new file mode 100644 index 0000000..ec39789 --- /dev/null +++ b/backend/controllers/userController.js @@ -0,0 +1,18 @@ + +import User from '../models/User.js'; + +export const getUserProfile = async (req, res) => { + try { + const userId = req.params.id; + const user = await User.findById(userId).select('-password'); + + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + res.status(200).json(user); + res.status(500).json({ message: 'Server error', error }); + }catch(err) { + console.log(err); + } +}; \ No newline at end of file diff --git a/backend/index.js b/backend/index.js index 88e9e76..b67aed5 100644 --- a/backend/index.js +++ b/backend/index.js @@ -32,9 +32,11 @@ import stationRoutes from "./routes/stationRoutes.js"; import trainRoutes from "./routes/trainRoutes.js"; import contactUs from "./routes/contactUsRouter.js"; import complaintRoutes from "./routes/complaintRoutes.js"; +import userRoutes from "./routes/userRoutes.js"; app.use("/auth", authRoutes); app.use("/api", authRoutes); +app.use("/api", userRoutes); app.use("/api", complaintRoutes); app.use("/station", stationRoutes); app.use("/train", trainRoutes); diff --git a/backend/routes/userRoutes.js b/backend/routes/userRoutes.js new file mode 100644 index 0000000..8c437f8 --- /dev/null +++ b/backend/routes/userRoutes.js @@ -0,0 +1,9 @@ +// routes/userRoutes.js +import express from 'express'; +import { getUserProfile } from '../controllers/userController.js'; + +const router = express.Router(); + +router.get('/profile/:id', getUserProfile); + +export default router; diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 2b60ddb..6e38b81 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -30,6 +30,7 @@ import ComplainBox from "./Pages/ComplainBox"; import Metadata from "./metadata"; import SettingsPage from "./Pages/Settings"; import Faq from './Pages/Faq'; +import ProfilePage from "./Pages/Profile"; function App() { return ( @@ -49,6 +50,7 @@ function App() { } /> } /> } /> + } /> } /> } /> diff --git a/frontend/src/Pages/Profile.jsx b/frontend/src/Pages/Profile.jsx new file mode 100644 index 0000000..6be80fe --- /dev/null +++ b/frontend/src/Pages/Profile.jsx @@ -0,0 +1,162 @@ +import React, { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import axios from 'axios'; +import placeholderImage from '../assets/person.jpg'; + +const ProfilePage = ({ userId }) => { + const navigate = useNavigate(); + const [profilePicture, setProfilePicture] = useState(placeholderImage); + const [userData, setUserData] = useState({}); + const [isEditing, setIsEditing] = useState(false); + + // Fetch user profile data on component load + useEffect(() => { + const fetchUserProfile = async () => { + + try { + const response = await axios.get(`http://localhost:3000/api/profile/${userId}`); + const data = response.data; + setUserData(data); + setProfilePicture(data.profilePicture || placeholderImage); + } catch (error) { + console.error('Error fetching profile:', error); + } + }; + fetchUserProfile(); + }, [userId]); + + const handleLogout = () => { + navigate('/login'); + }; + + return ( +
+

User Profile

+ {/* Profile Header */} +
+
+ Profile + { + const file = e.target.files[0]; + const newProfilePicture = URL.createObjectURL(file); + setProfilePicture(newProfilePicture); // Temporarily show new picture + // Handle picture upload to server here + }} + /> +
+
+

{userData.name}

+

{userData.email}

+

{userData.phoneNumber}

+
+
+ + {/* Action Buttons */} +
+ + +
+ + {/* Profile Information and Stats */} +
+
+

About

+

+ {userData.bio || "This is a placeholder bio. Update your profile to add more information."} +

+
+ +
+
+ 15 + Posts +
+
+ 24 + Reviews +
+
+ 4.5 + Rating +
+
+
+ + {/* Edit Profile Form */} + {isEditing && ( +
+

Edit Profile

+
e.preventDefault()} className="space-y-4"> +
+ + setUserData({ ...userData, name: e.target.value })} + className="w-full px-3 py-2 border border-gray-300 rounded-lg" + /> +
+
+ + setUserData({ ...userData, email: e.target.value })} + className="w-full px-3 py-2 border border-gray-300 rounded-lg" + /> +
+
+ + setUserData({ ...userData, phoneNumber: e.target.value })} + className="w-full px-3 py-2 border border-gray-300 rounded-lg" + /> +
+
+ +