Skip to content

Commit 98b3f0a

Browse files
committed
Add api call
1 parent 29a0357 commit 98b3f0a

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

src/App.jsx

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import ChatHeader from "./components/ChatHeader";
55
import ChatBox from "./components/ChatBox";
66
import ChatInput from "./components/ChatInput";
77
import "./index.css";
8+
import { API_URL } from "./config";
89

910
export default function App() {
1011
const savedTheme = localStorage.getItem("theme");
11-
const [theme, setTheme] = useState(savedTheme);
12+
const [theme, setTheme] = useState(savedTheme || "dark");
1213
const [sidebarOpen, setSidebarOpen] = useState(true);
1314
const [messages, setMessages] = useState([
1415
{
@@ -27,17 +28,62 @@ export default function App() {
2728
localStorage.setItem("theme", theme);
2829
}, [theme]);
2930

30-
const handleSend = (inputText) => {
31-
const newMessages = [
32-
...messages,
33-
{ type: "user", text: inputText, icon: "🐻" },
34-
{
35-
type: "bot",
36-
text: `Sorry, I don't understand yet. Please try again later.`,
37-
icon: "🐶",
38-
},
39-
];
40-
setMessages(newMessages);
31+
// const handleSend = (inputText) => {
32+
// const newMessages = [
33+
// ...messages,
34+
// { type: "user", text: inputText, icon: "🐻" },
35+
// {
36+
// type: "bot",
37+
// text: `Sorry, I don't understand yet. Please try again later.`,
38+
// icon: "🐶",
39+
// },
40+
// ];
41+
// setMessages(newMessages);
42+
// };
43+
44+
const handleSend = async (inputText) => {
45+
const userMessage = { type: "user", text: inputText, icon: "🐻" };
46+
setMessages((prev) => [...prev, userMessage]);
47+
48+
// Add loading state
49+
const loadingId = setTimeout(() => {
50+
setMessages((prev) => [
51+
...prev,
52+
{ type: "bot", text: "Thinking...", icon: "🐶", isLoading: true }
53+
]);
54+
}, 1000);
55+
56+
try {
57+
const res = await fetch(`${API_URL}/chat`, { // Change this to your deployed API
58+
method: "POST",
59+
headers: { "Content-Type": "application/json" },
60+
body: JSON.stringify({ question: inputText }),
61+
});
62+
63+
clearTimeout(loadingId);
64+
65+
// Remove loading message if it was added
66+
setMessages((prev) => prev.filter(msg => !msg.isLoading));
67+
68+
if (!res.ok) {
69+
throw new Error(`Server responded with status: ${res.status}`);
70+
}
71+
72+
const data = await res.json();
73+
const botReply = { type: "bot", text: data.response || "Sorry, I couldn't generate a response.", icon: "🐶" };
74+
setMessages((prev) => [...prev, botReply]);
75+
} catch (error) {
76+
clearTimeout(loadingId);
77+
78+
// Remove loading message if it was added
79+
setMessages((prev) => prev.filter(msg => !msg.isLoading));
80+
81+
console.error("Chat error:", error);
82+
setMessages((prev) => [
83+
...prev,
84+
{ type: "bot", text: "An error occurred. Please try again later.", icon: "🐶" },
85+
]);
86+
}
4187
};
4288

4389
return (

src/components/Sidebar.jsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export default function Sidebar({ open, setOpen }) {
1919
localStorage.setItem("sidebar", open ? "open" : "closed");
2020
}, [open]);
2121

22+
23+
const studyOffice = () => {}
24+
2225
return (
2326
<aside className={`sidebar ${open ? "open" : "closed hidden"} ${!open && isMobile ? "hidden" : ""}`}>
2427
<div className="relative group">
@@ -29,30 +32,29 @@ export default function Sidebar({ open, setOpen }) {
2932
</div>
3033

3134
<div className="mt-10">
32-
<button className="btn">Study Office Opening hour</button>
33-
<button className="btn">IRO Opening hour</button>
35+
<button className="btn" onClick={studyOffice}>Study Office Opening hour</button>
36+
<button className="btn">International Office Opening hour</button>
3437
</div>
3538

3639
<div className="links">
3740
<h3>Useful Links</h3>
41+
<div>
42+
<a href="https://www.uniduna.hu/" target="_blank" className="display-inline-block">
43+
<span className="link-element">University</span>
44+
</a>
45+
<a href="https://nappw.dfad.duf.hu/hallgato_ng/login" target="_blank" className="display-inline-block">
46+
<span className="link-element">Neptun</span>
47+
</a>
48+
<a href="https://v39.moodle.uniduna.hu/login/index.php" target="_blank" className="display-inline-block">
49+
<span className="link-element">Moodle</span>
50+
</a>
51+
</div>
3852
<ul>
3953
<li>
40-
<a href="https://www.uniduna.hu/" target="_blank">
41-
<i className="fa-solid fa-award"></i>
42-
<span className="link-element">University</span>
43-
</a>
44-
</li>
45-
<li>
46-
<a href="https://nappw.dfad.duf.hu/hallgato_ng/login" target="_blank">
47-
<i className="fa-solid fa-award"></i>
48-
<span className="link-element">Neptun</span>
49-
</a>
54+
5055
</li>
5156
<li>
52-
<a href="https://v39.moodle.uniduna.hu/login/index.php" target="_blank">
53-
<i className="fa-solid fa-award"></i>
54-
<span className="link-element">Moodle</span>
55-
</a>
57+
5658
</li>
5759
</ul>
5860
</div>

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const API_URL = "https://exhalted-unibot.hf.space";

0 commit comments

Comments
 (0)