From f5382d82f6cd3de33cfd1c04747d67dcb00a608e Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Mon, 3 Mar 2025 21:13:07 +0000 Subject: [PATCH] test --- components/dashboard/ChatDetail.tsx | 39 ++++++++++++++++++- components/dashboard/ChatList.tsx | 45 ++++++++++++++++++---- components/dashboard/ChatNotifications.tsx | 20 ++++++++-- components/dashboard/NewChatForm.tsx | 39 ++++++++++++++++--- 4 files changed, 125 insertions(+), 18 deletions(-) diff --git a/components/dashboard/ChatDetail.tsx b/components/dashboard/ChatDetail.tsx index 6c4f9d7..1eb2637 100644 --- a/components/dashboard/ChatDetail.tsx +++ b/components/dashboard/ChatDetail.tsx @@ -11,6 +11,7 @@ import { formatDistanceToNow } from "date-fns"; import axios from "axios"; import { toast } from "sonner"; import { ArrowLeft, Send, RefreshCw } from "lucide-react"; +import { getCookie } from "@/lib/client-utils"; interface Message { _id: string; @@ -44,7 +45,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) { // Fetch chat data const fetchChat = async () => { try { - const response = await axios.get(`/api/chats/${chatId}`); + // Get auth token from cookies + const authToken = getCookie("Authorization"); + + if (!authToken) { + toast.error("You need to be logged in"); + router.push("/auth/login"); + return; + } + + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + const response = await authAxios.get(`/chats/${chatId}`); setChat(response.data); setLoading(false); } catch (error) { @@ -76,7 +94,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) { setSending(true); try { - await axios.post(`/api/chats/${chatId}/message`, { + // Get auth token from cookies + const authToken = getCookie("Authorization"); + + if (!authToken) { + toast.error("You need to be logged in"); + router.push("/auth/login"); + return; + } + + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + await authAxios.post(`/chats/${chatId}/message`, { content: message }); diff --git a/components/dashboard/ChatList.tsx b/components/dashboard/ChatList.tsx index 290d1be..420473b 100644 --- a/components/dashboard/ChatList.tsx +++ b/components/dashboard/ChatList.tsx @@ -37,17 +37,30 @@ export default function ChatList() { useEffect(() => { const fetchVendorData = async () => { try { - // Get vendor info from cookies - const vendorId = getCookie("vendorId"); + // Get auth token from cookies + const authToken = getCookie("Authorization"); - if (!vendorId) { + if (!authToken) { toast.error("You need to be logged in to view chats"); router.push("/auth/login"); return; } + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + // Get vendor ID from token (assuming JWT with vendorId in the payload) + // If you can't extract from token, you might need a profile endpoint + // For now, we'll use a placeholder ID until proper JWT decoding is implemented + const vendorId = "current"; // Replace with actual ID extraction + // Fetch vendor's stores - const storesResponse = await axios.get(`/api/stores/vendor/${vendorId}`); + const storesResponse = await authAxios.get(`/stores/vendor/${vendorId}`); setVendorStores(storesResponse.data); if (storesResponse.data.length > 0) { @@ -69,10 +82,28 @@ export default function ChatList() { setLoading(true); try { - const vendorId = getCookie("vendorId"); + // Get auth token from cookies + const authToken = getCookie("Authorization"); + + if (!authToken) { + toast.error("You need to be logged in"); + router.push("/auth/login"); + return; + } + + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + // Get vendor ID (as above, assuming we have a way to get it) + const vendorId = "current"; // Replace with actual ID extraction // Fetch chats - const chatsResponse = await axios.get(`/api/chats/vendor/${vendorId}`); + const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`); // Filter chats by selected store const filteredChats = chatsResponse.data.filter( @@ -82,7 +113,7 @@ export default function ChatList() { setChats(filteredChats); // Fetch unread counts - const unreadResponse = await axios.get(`/api/chats/vendor/${vendorId}/unread`); + const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`); setUnreadCounts(unreadResponse.data); } catch (error) { console.error("Error fetching chats:", error); diff --git a/components/dashboard/ChatNotifications.tsx b/components/dashboard/ChatNotifications.tsx index 1e303d9..074647e 100644 --- a/components/dashboard/ChatNotifications.tsx +++ b/components/dashboard/ChatNotifications.tsx @@ -29,11 +29,23 @@ export default function ChatNotifications() { useEffect(() => { const fetchUnreadCounts = async () => { try { - const vendorId = getCookie("vendorId"); + // Get auth token from cookies + const authToken = getCookie("Authorization"); - if (!vendorId) return; + if (!authToken) return; - const response = await axios.get(`/api/chats/vendor/${vendorId}/unread`); + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + // Get vendor ID (placeholder until proper JWT decoding) + const vendorId = "current"; // Replace with actual ID extraction + + const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`); setUnreadCounts(response.data); // If there are unread messages, fetch chat metadata @@ -48,7 +60,7 @@ export default function ChatNotifications() { await Promise.all( chatIds.map(async (chatId) => { try { - const chatResponse = await axios.get(`/api/chats/${chatId}`); + const chatResponse = await authAxios.get(`/chats/${chatId}`); metadata[chatId] = { buyerId: chatResponse.data.buyerId, }; diff --git a/components/dashboard/NewChatForm.tsx b/components/dashboard/NewChatForm.tsx index 0704a35..0d06398 100644 --- a/components/dashboard/NewChatForm.tsx +++ b/components/dashboard/NewChatForm.tsx @@ -24,15 +24,27 @@ export default function NewChatForm() { useEffect(() => { const fetchVendorStores = async () => { try { - const vendorId = getCookie("vendorId"); + // Get auth token from cookies + const authToken = getCookie("Authorization"); - if (!vendorId) { + if (!authToken) { toast.error("You need to be logged in"); - router.push("/login"); + router.push("/auth/login"); return; } - const response = await axios.get(`/api/stores/vendor/${vendorId}`); + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + // Get vendor ID (placeholder until proper JWT decoding) + const vendorId = "current"; // Replace with actual ID extraction + + const response = await authAxios.get(`/stores/vendor/${vendorId}`); setVendorStores(response.data); if (response.data.length > 0) { @@ -61,7 +73,24 @@ export default function NewChatForm() { setLoading(true); try { - const response = await axios.post("/api/chats/create", { + // Get auth token from cookies + const authToken = getCookie("Authorization"); + + if (!authToken) { + toast.error("You need to be logged in"); + router.push("/auth/login"); + return; + } + + // Set up axios with the auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + const response = await authAxios.post("/chats/create", { buyerId, storeId: selectedStore, initialMessage