-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientDashboard.jsx
More file actions
223 lines (207 loc) · 5.25 KB
/
ClientDashboard.jsx
File metadata and controls
223 lines (207 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import "./styles.css";
import { Line, Bar } from "react-chartjs-2";
import { useState, useEffect } from "react";
import axiosBaseUrl from "../../../Axios/axios";
import { useDispatch, useSelector } from "react-redux";
import { toggleLoad } from "../../../Redux/Slices/loadingSlice";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
BarElement,
Title,
Tooltip,
Legend,
Filler,
} from "chart.js";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
BarElement,
Title,
Tooltip,
Legend,
Filler
);
const ClientDashboard = () => {
const [dashboardData, setDashboardData] = useState(null);
const [error, setError] = useState(null);
const userId = useSelector((state) => state.user.id);
const loading = useSelector((state) => state.loading.loadingState);
const dispatch = useDispatch();
useEffect(() => {
const fetchDashboardData = async () => {
dispatch(toggleLoad(true));
try {
const response = await axiosBaseUrl.get(
`/clients/clientDashboardData/${userId}`
);
setDashboardData(response.data.data);
dispatch(toggleLoad(false));
} catch (error) {
setError(error.message);
dispatch(toggleLoad(false));
}
};
fetchDashboardData();
}, [userId, dispatch]);
if (loading) {
return (
<div className="spinner-container">
<div className="spinner"></div>
<p>Loading dashboard data...</p>
</div>
);
}
if (error) {
return (
<div className="error-message">Error loading dashboard data: {error}</div>
);
}
if (!dashboardData) {
return (
<div className="spinner-container">
<div className="spinner"></div>
<p>Loading dashboard data...</p>
</div>
);
}
const powerUsageData = {
labels: Object.keys(dashboardData.powerUsagePerDay),
datasets: [
{
label: "Power Usage (in Kilowatts)",
data: Object.values(dashboardData.powerUsagePerDay),
fill: true,
backgroundColor: "rgba(75, 192, 192, 0.6)",
borderColor: "#28a745",
pointBackgroundColor: "#28a745",
pointBorderColor: "#fff",
tension: 0.3,
},
],
};
const cumulativePowerUsageData = {
labels: Object.keys(dashboardData.cumulativePowerUsage),
datasets: [
{
label: "Cumulative Power Usage (in Kilowatts)",
data: Object.values(dashboardData.cumulativePowerUsage).map(
(arr) => Math.max(...arr)
),
backgroundColor: "rgba(255, 193, 7, 0.6)",
borderColor: "#ffc107",
borderWidth: 1,
},
],
};
const chartOptions = {
responsive: true,
maintainAspectRatio: false,
layout: {
padding: {
top: 0,
bottom: 0,
},
},
plugins: {
legend: {
position: "bottom",
},
title: {
display: true,
font: {
size: 16,
},
},
},
scales: {
x: {
title: {
display: true,
text: "Date",
color: "#555",
},
grid: {
borderColor: "#eee",
},
},
y: {
beginAtZero: true,
min: 0,
title: {
display: true,
text: "Kilowatts",
color: "#555",
},
grid: {
borderColor: "#eee",
},
},
},
};
return (
<div className="client-dashboard-container">
<h2>Client Dashboard</h2>
<div className="charts-row">
<div className="chart-widget">
<h3>Power Usage per Day</h3>
{Object.keys(powerUsageData.labels).length > 0 ? (
<Line
data={powerUsageData}
options={{
...chartOptions,
plugins: {
...chartOptions.plugins,
},
}}
/>
) : (
<p>No power usage data available for the current month.</p>
)}
</div>
<div className="chart-widget">
<h3>Cumulative Power Usage Per Day</h3>
{Object.keys(cumulativePowerUsageData.labels).length > 0 ? (
<Bar
data={cumulativePowerUsageData}
options={{
...chartOptions,
plugins: {
...chartOptions.plugins,
},
}}
/>
) : (
<p>
No cumulative power usage data available for the current month.
</p>
)}
</div>
</div>
<div className="summary-widgets">
<div className="summary-widget">
<h3>Total Power Usage This Month (In Kilowatts)</h3>
<p className="metric-value">
{dashboardData.totalPowerUsageThisMonth}
</p>
</div>
<div className="summary-widget">
<h3>Average Voltage Reach</h3>
<p className="metric-value">{dashboardData.averageVoltageReach} V</p>
</div>
<div className="summary-widget">
<h3>Expected Power Limit for the month</h3>
<p className="metric-value">
{dashboardData.expectedPowerLimit} Kilowatts
</p>
</div>
</div>
</div>
);
};
export default ClientDashboard;