diff --git a/client/src/containers/User/UserDash/Dash.css b/client/src/containers/User/UserDash/Dash.css index d169758..59957cc 100644 --- a/client/src/containers/User/UserDash/Dash.css +++ b/client/src/containers/User/UserDash/Dash.css @@ -1,40 +1,75 @@ +.dashboard-container { + max-width: 1200px; + margin: auto; + padding: 20px; + font-family: "Arial", sans-serif; +} + .dashboard-header { text-align: center; - margin-bottom: 20px; + margin-bottom: 40px; +} + +.dashboard-header h1 { + font-size: 2.5rem; + margin-bottom: 5px; +} + +.dashboard-header p { + font-size: 1.2rem; + color: #555; +} + +.user-info { + background: #f7f7f7; + padding: 20px; + border-radius: 12px; + margin-bottom: 40px; +} + +.info-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); + gap: 15px; } .chart-container { display: flex; flex-direction: column; align-items: center; - justify-content: center; } .pie-charts { display: flex; justify-content: center; - flex-wrap: wrap; - gap: 20px; /* Space between pie charts */ margin-bottom: 30px; } -.pie-chart { - border: 1px solid #ddd; /* Optional: add border around each chart */ - border-radius: 10px; /* Optional: rounded corners */ +.pie-chart-wrapper { + margin: 0 20px; } -.bar-chart { +.bar-chart-wrapper { + overflow-x: auto; margin-top: 20px; } .home-link { display: block; text-align: center; - margin-top: 20px; - color: #007bff; /* Bootstrap primary color */ + margin-top: 30px; + font-size: 1.1rem; + color: #007bff; text-decoration: none; } .home-link:hover { - text-decoration: underline; /* Add underline on hover */ + text-decoration: underline; +} + +.loading, .error { + text-align: center; + font-size: 1.2rem; + padding: 50px; + color: #ff4d4f; } diff --git a/client/src/containers/User/UserDash/Dash.tsx b/client/src/containers/User/UserDash/Dash.tsx index 94f2076..6bdbb01 100644 --- a/client/src/containers/User/UserDash/Dash.tsx +++ b/client/src/containers/User/UserDash/Dash.tsx @@ -1,143 +1,317 @@ +// import React, { useEffect, useState } from "react"; +// import { Link } from "react-router-dom"; +// import { +// PieChart, +// Pie, +// BarChart, +// Bar, +// Brush, +// ReferenceLine, +// XAxis, +// YAxis, +// CartesianGrid, +// Tooltip, +// Legend, +// } from "recharts"; +// import axios from "axios"; +// import "./Dash.css"; // Make sure to update styles in this file + +// const ServerPort = +// process.env.REACT_APP_SOCKET_IO_CLIENT_PORT || "http://localhost:3001"; + +// const Dash = () => { +// const [userData, setUserData] = useState(null); +// const [loading, setLoading] = useState(true); +// const [error, setError] = useState(""); + +// useEffect(() => { +// const userId = localStorage.getItem("userId"); // Get user ID from local storage +// const fetchUserData = async () => { +// try { +// const res = await axios.get(`${ServerPort}/api/user/view/${userId}`); +// setUserData(res.data); +// } catch (error) { +// console.error("Error fetching user data:", error); +// setError("Failed to load user data."); +// } finally { +// setLoading(false); +// } +// }; + +// if (userId) { +// fetchUserData(); +// } else { +// setError("User ID not found in local storage."); +// setLoading(false); +// } +// }, []); + +// const data = [ +// // Your existing data for bar chart +// { name: "1", uv: 300, pv: 456 }, +// { name: "2", uv: -145, pv: 230 }, +// // ... other data points +// ]; + +// const data2 = [ +// { name: "Group A", value: 400 }, +// { name: "Group B", value: 300 }, +// // ... other data points +// ]; + +// if (loading) { +// return
Loading user data...
; +// } + +// if (error) { +// return
{error}
; +// } + +// return ( +// <> +//
+//

Dashboard

+//
+ +//
+//

User Information

+//

Name: {userData.firstName} {userData.lastName}

+//

Email: {userData.email}

+//

Phone Number: {userData.phoneNumber || "Not provided"}

+//

Address: {userData.address || "Not provided"}

+//

Available Status: {userData.loadStatus}

+//

Customer Since: {new Date(userData.availableFrom).toLocaleDateString()}

+//

Type of Owner: {userData.userType}

+//
+ +//
+//

Charts

+//
+// {Array(4) +// .fill(null) +// .map((_, index) => ( +//
+// +// +// +//
+// ))} +//
+ +//
+// +// +// +// +// +// +// +// +// +// +// +//
+//
+ +// +// Home +// +// +// ); +// }; + +// export default Dash; + import React, { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { PieChart, Pie, + Cell, BarChart, Bar, - Brush, - ReferenceLine, XAxis, YAxis, CartesianGrid, Tooltip, Legend, + ReferenceLine, + Brush, } from "recharts"; import axios from "axios"; -import "./Dash.css"; // Make sure to update styles in this file +import "./Dash.css"; + +const SERVER_PORT = process.env.REACT_APP_SOCKET_IO_CLIENT_PORT || "http://localhost:3001"; -const ServerPort = - process.env.REACT_APP_SOCKET_IO_CLIENT_PORT || "http://localhost:3001"; +// Colors for pie charts +const PIE_COLORS = ["#0088FE", "#00C49F", "#FFBB28", "#FF8042"]; const Dash = () => { const [userData, setUserData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); + // Fetch user data useEffect(() => { - const userId = localStorage.getItem("userId"); // Get user ID from local storage + const userId = localStorage.getItem("userId"); + if (!userId) { + setError("User ID not found in local storage."); + setLoading(false); + return; + } + const fetchUserData = async () => { try { - const res = await axios.get(`${ServerPort}/api/user/view/${userId}`); + const res = await axios.get(`${SERVER_PORT}/api/user/view/${userId}`); setUserData(res.data); - } catch (error) { - console.error("Error fetching user data:", error); + } catch (err) { + console.error("Error fetching user data:", err); setError("Failed to load user data."); } finally { setLoading(false); } }; - if (userId) { - fetchUserData(); - } else { - setError("User ID not found in local storage."); - setLoading(false); - } + fetchUserData(); }, []); - const data = [ - // Your existing data for bar chart - { name: "1", uv: 300, pv: 456 }, - { name: "2", uv: -145, pv: 230 }, - // ... other data points - ]; - - const data2 = [ - { name: "Group A", value: 400 }, - { name: "Group B", value: 300 }, - // ... other data points - ]; + if (loading) return
Loading user data...
; + if (error) return
{error}
; - if (loading) { - return
Loading user data...
; - } + // Prepare chart data + const barData = userData?.loadDetails?.map((load: any, idx: number) => ({ + name: `Load ${idx + 1}`, + weight: load.weight || 0, + value: load.value || 0, + })) || [{ name: "No Data", weight: 0, value: 0 }]; - if (error) { - return
{error}
; - } + const pieData = [ + { name: "Available", value: userData.availableFrom ? 1 : 0 }, + { name: "Unavailable", value: userData.availableFrom ? 0 : 1 }, + ]; return ( - <> -
+
+

Dashboard

-
+

Welcome back, {userData.firstName}!

+ -
+ {/* User Info */} +

User Information

-

Name: {userData.firstName} {userData.lastName}

-

Email: {userData.email}

-

Phone Number: {userData.phoneNumber || "Not provided"}

-

Address: {userData.address || "Not provided"}

-

Available Status: {userData.loadStatus}

-

Customer Since: {new Date(userData.availableFrom).toLocaleDateString()}

-

Type of Owner: {userData.userType}

-
- -
-

Charts

+
+
+ Name: {userData.firstName} {userData.lastName} +
+
+ Email: {userData.email} +
+
+ Phone: {userData.phoneNumber || "Not provided"} +
+
+ Address: {userData.address || "Not provided"} +
+
+ Status: {userData.loadStatus || "N/A"} +
+
+ Customer Since:{" "} + {userData.availableFrom + ? new Date(userData.availableFrom).toLocaleDateString() + : "N/A"} +
+
+ Type of Owner: {userData.userType || "N/A"} +
+
+ Experience Level: {userData.experienceLevel || "N/A"} +
+
+ Preferred Load: {userData.preferredLoadType || "N/A"} +
+
+ Company: {userData.company || "N/A"} +
+
+ Subscribed: {userData.subscribed ? "Yes" : "No"} +
+
+ Rating: {userData.rating || "N/A"} +
+
+ + + {/* Charts */} +
+

Analytics

+ + {/* Pie Chart */}
- {Array(4) - .fill(null) - .map((_, index) => ( -
- - - -
- ))} + + + {pieData.map((entry, index) => ( + + ))} + + +
+ {/* Bar Chart */}
- + - - + +
-
+ - Home + Back to Home - +
); };