use axios instead of fetch
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
"use client"
|
"use client";
|
||||||
|
|
||||||
import * as React from "react"
|
import * as React from "react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
import { Button } from "@/components/ui/button"
|
import { Message } from "@/components/panel-ui/patient/app-chat";
|
||||||
import { Message } from "@/components/panel-ui/patient/app-chat"
|
import { Input } from "@/components/ui/input";
|
||||||
import { Input } from "@/components/ui/input"
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
|
|
||||||
import { Card, CardContent } from "@/components/ui/card"
|
|
||||||
|
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useUser } from "@clerk/nextjs";
|
import { useUser } from "@clerk/nextjs";
|
||||||
@@ -17,46 +15,58 @@ export default function Chat() {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
const [userData, setUserData] = useState(null);
|
const [userData, setUserData] = useState(null);
|
||||||
const [userQuery, setUserQuery] = useState('');
|
const [userQuery, setUserQuery] = useState("");
|
||||||
const [chatHistory, setChatHistory] = useState<{ type: 'user' | 'bot', text: string }>([]);
|
const [chatHistory, setChatHistory] = useState<
|
||||||
|
{ type: "user" | "bot", text: string }
|
||||||
|
>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) {
|
if (user) {
|
||||||
axios.get(`/api/user?userId=${user.id}`).then(response => {
|
axios
|
||||||
|
.get(`/api/user?userId=${user.id}`)
|
||||||
|
.then((response) => {
|
||||||
setUserData(response.data);
|
setUserData(response.data);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error("Error fetching user data:", error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
if (userData) {
|
useEffect(() => {
|
||||||
if (userData.role !== "patient") {
|
if (userData && userData.role !== "patient") {
|
||||||
router.push("/suite/doctor/dashboard");
|
router.push("/suite/doctor/dashboard");
|
||||||
}
|
}
|
||||||
}
|
}, [userData, router]);
|
||||||
|
|
||||||
const handleSend = async () => {
|
const handleSend = async () => {
|
||||||
if (!userQuery.trim()) return;
|
if (!userQuery.trim()) return;
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const response = await fetch('/api/chat', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ query: userQuery }),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
try {
|
||||||
|
const response = await axios.post("/api/chat", { query: userQuery });
|
||||||
|
|
||||||
setChatHistory([
|
const botResponse = response.data?.answer || "No response from the bot.";
|
||||||
...chatHistory,
|
|
||||||
{ type: 'user', text: userQuery },
|
setChatHistory((prevHistory) => [
|
||||||
{ type: 'bot', text: data.answer },
|
...prevHistory,
|
||||||
|
{ type: "user", text: userQuery },
|
||||||
|
{ type: "bot", text: botResponse },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
setUserQuery('');
|
setUserQuery("");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error sending message:", error);
|
||||||
|
setChatHistory((prevHistory) => [
|
||||||
|
...prevHistory,
|
||||||
|
{ type: "user", text: userQuery },
|
||||||
|
{ type: "bot", text: "An error occurred while processing your request." },
|
||||||
|
]);
|
||||||
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -70,7 +80,7 @@ export default function Chat() {
|
|||||||
key={idx}
|
key={idx}
|
||||||
avatarUrl="/vercel.svg"
|
avatarUrl="/vercel.svg"
|
||||||
message={msg.text}
|
message={msg.text}
|
||||||
sender={msg.type === 'user' ? "You" : "Bot"}
|
sender={msg.type === "user" ? "You" : "Bot"}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -87,12 +97,12 @@ export default function Chat() {
|
|||||||
onClick={handleSend}
|
onClick={handleSend}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
>
|
>
|
||||||
{loading ? 'Loading...' : 'Send'}
|
{loading ? "Loading..." : "Send"}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user