forked from Definehack/Define25
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDashboard.jsx
More file actions
175 lines (159 loc) · 7.03 KB
/
Copy pathDashboard.jsx
File metadata and controls
175 lines (159 loc) · 7.03 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
import React, { useState, useEffect, useRef } from 'react';
import { useNavigate, Route, Routes, Link } from 'react-router-dom';
import '../styles/dashboard.css';
import AnalyticsChart from './Analytics';
import TransactionHistory from './TransactionHistory';
import StockTrends from './StockTrends';
import SplitPayApp from './SplitPayApp'; // Import the SplitPayApp component
function Dashboard() {
const navigate = useNavigate();
const [showLogoutConfirm, setShowLogoutConfirm] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [chatMessages, setChatMessages] = useState([]);
const [inputMessage, setInputMessage] = useState('');
const messagesEndRef = useRef(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => {
scrollToBottom();
}, [chatMessages]);
useEffect(() => {
// Check authentication and set up dashboard
const isAuth = localStorage.getItem('isAuthenticated');
if (!isAuth) {
navigate('/');
return;
}
setIsLoading(false);
}, [navigate]);
const handleLogoutClick = () => {
setShowLogoutConfirm(true);
};
const handleLogoutConfirm = () => {
localStorage.removeItem('isAuthenticated');
localStorage.removeItem('currentUser');
navigate('/');
};
if (isLoading) {
return (
<div className="dashboard-container loading">
<div className="loading-spinner">Loading...</div>
</div>
);
}
async function handleSendMessage(e) {
e.preventDefault();
if (!inputMessage.trim()) return;
const message = inputMessage.trim();
setChatMessages(prev => [...prev, { type: 'user', content: message }]);
setInputMessage('');
try {
const response = await fetch('http://localhost:5174/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message })
});
if (!response.ok) {
const errorText = await response.text();
console.error('API Error Response:', errorText);
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('Response data:', data); // Debug logging
if (data && data.response) {
setChatMessages(prev => [...prev, {
type: 'bot',
content: data.response
}]);
} else {
throw new Error('Invalid response format');
}
} catch (error) {
console.error('Chat Error:', error);
setChatMessages(prev => [...prev, {
type: 'bot',
content: 'FinBot: I apologize, but I am temporarily unable to process requests. Please try again shortly.'
}]);
}
}
return (
<div className="dashboard-container">
<header className="dashboard-header">
<div className="logo">Finura</div>
<nav className="dashboard-nav">
<Link to="/" className="active">Overview</Link>
<Link to="/transactions">Transactions</Link>
<Link to="/analytics">Analytics</Link>
<Link to="/settings">Settings</Link>
<Link to="/split-pay">Split Pay</Link> {/* Add Split Pay link */}
</nav>
<div className="logout-container">
<button className="logout-btn" onClick={handleLogoutClick} style={{ width: '30px', height: '15px' }}>Logout</button>
</div>
</header>
<div className="dashboard-main">
<Routes>
<Route path="/" element={
<div className="dashboard-content">
<div className="dashboard-left">
<div className="dashboard-card">
<h2>Financial Overview</h2>
<AnalyticsChart />
</div>
<div className="bottom-row">
<div className="bottom-row-item">
<TransactionHistory />
</div>
<div className="bottom-row-item">
<StockTrends />
</div>
</div>
</div>
</div>
} />
<Route path="/split-pay" element={<SplitPayApp />} /> {/* Add route for Split Pay */}
</Routes>
<div className="dashboard-right">
<div className="chatbot-container">
<div className="chatbot-header">
<h3>AI Financial Assistant</h3>
</div>
<div className="chatbot-messages">
{chatMessages.map((msg, index) => (
<div key={index} className={`message ${msg.type}`}>
{msg.type === 'user' ? `You: ${msg.content}` : msg.content}
</div>
))}
<div ref={messagesEndRef} />
</div>
<form className="chatbot-input" onSubmit={handleSendMessage}>
<input
type="text"
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
placeholder="Ask about your finances..."
/>
<button type="submit">Send</button>
</form>
</div>
</div>
</div>
{showLogoutConfirm && (
<div className="logout-confirm-overlay">
<div className="logout-confirm-modal">
<h3>Confirm Logout</h3>
<p>Are you sure you want to logout?</p>
<div className="logout-confirm-buttons">
<button onClick={handleLogoutConfirm}>Yes, Logout</button>
<button onClick={() => setShowLogoutConfirm(false)}>Cancel</button>
</div>
</div>
</div>
)}
</div>
);
}
export default Dashboard;