diff --git a/components/dashboard/ChatDetail.tsx b/components/dashboard/ChatDetail.tsx index bf69a43..1eb2637 100644 --- a/components/dashboard/ChatDetail.tsx +++ b/components/dashboard/ChatDetail.tsx @@ -62,7 +62,7 @@ export default function ChatDetail({ chatId }: { chatId: string }) { } }); - const response = await authAxios.get(`/api/chats/${chatId}`); + const response = await authAxios.get(`/chats/${chatId}`); setChat(response.data); setLoading(false); } catch (error) { @@ -111,7 +111,7 @@ export default function ChatDetail({ chatId }: { chatId: string }) { } }); - await authAxios.post(`/api/chats/${chatId}/message`, { + await authAxios.post(`/chats/${chatId}/message`, { content: message }); diff --git a/components/dashboard/ChatList.tsx b/components/dashboard/ChatList.tsx index 112482e..6eff509 100644 --- a/components/dashboard/ChatList.tsx +++ b/components/dashboard/ChatList.tsx @@ -54,9 +54,17 @@ export default function ChatList() { } }); - // First, get vendor info using the /api/auth/me endpoint + // First, get vendor info using the /auth/me endpoint const vendorResponse = await authAxios.get('/auth/me'); - const vendorId = vendorResponse.data._id; + + // Access correct property - the vendor ID is in vendor._id + const vendorId = vendorResponse.data.vendor?._id; + + if (!vendorId) { + console.error("Vendor ID not found in profile response:", vendorResponse.data); + toast.error("Could not retrieve vendor information"); + return; + } // Fetch vendor's stores using storefront endpoint const storesResponse = await authAxios.get(`/storefront`); @@ -99,11 +107,19 @@ export default function ChatList() { }); // Get vendor ID from profile - const vendorResponse = await authAxios.get('/api/auth/me'); - const vendorId = vendorResponse.data._id; + const vendorResponse = await authAxios.get('/auth/me'); + + // Access correct property - the vendor ID is in vendor._id + const vendorId = vendorResponse.data.vendor?._id; + + if (!vendorId) { + console.error("Vendor ID not found in profile response:", vendorResponse.data); + toast.error("Could not retrieve vendor information"); + return; + } // Fetch chats - const chatsResponse = await authAxios.get(`/api/chats/vendor/${vendorId}`); + const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`); // Filter chats by selected store const filteredChats = chatsResponse.data.filter( @@ -113,7 +129,7 @@ export default function ChatList() { setChats(filteredChats); // Fetch unread counts - const unreadResponse = await authAxios.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 c17a640..753b48c 100644 --- a/components/dashboard/ChatNotifications.tsx +++ b/components/dashboard/ChatNotifications.tsx @@ -40,11 +40,16 @@ export default function ChatNotifications() { } }); + // Get vendor info from profile endpoint - removing /api prefix const vendorResponse = await authAxios.get('/auth/me'); - - console.log(vendorResponse.data.vendor._id); - - const vendorId = vendorResponse.data.vendor._id; + + // Access correct property - the vendor ID is in vendor._id + const vendorId = vendorResponse.data.vendor?._id; + + if (!vendorId) { + console.error("Vendor ID not found in profile response:", vendorResponse.data); + return; + } const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`); setUnreadCounts(response.data); @@ -56,11 +61,11 @@ export default function ChatNotifications() { // Create a simplified metadata object with just needed info const metadata: Record = {}; - // Fetch each chat to get buyer IDs + // Fetch each chat to get buyer IDs - removing /api prefix await Promise.all( chatIds.map(async (chatId) => { try { - const chatResponse = await authAxios.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 e536e9b..eda29ec 100644 --- a/components/dashboard/NewChatForm.tsx +++ b/components/dashboard/NewChatForm.tsx @@ -24,16 +24,14 @@ export default function NewChatForm() { useEffect(() => { const fetchVendorStores = async () => { try { - // Get auth token from cookies const authToken = getCookie("Authorization"); if (!authToken) { - toast.error("You need to be logged in"); + toast.error("You must be logged in to start a chat"); router.push("/auth/login"); return; } - // Set up axios with the auth token const authAxios = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_URL, headers: { @@ -41,16 +39,24 @@ export default function NewChatForm() { } }); - // Get vendor ID from profile - const vendorResponse = await authAxios.get('/api/auth/me'); - const vendorId = vendorResponse.data._id; + // Get vendor profile first + const vendorResponse = await authAxios.get('/auth/me'); - // Fetch vendor's stores using storefront endpoint - const response = await authAxios.get(`/api/storefront`); - setVendorStores(response.data); + // Extract vendor ID properly + const vendorId = vendorResponse.data.vendor?._id; - if (response.data.length > 0) { - setSelectedStore(response.data[0]._id); + if (!vendorId) { + console.error("Vendor ID not found in profile response:", vendorResponse.data); + toast.error("Could not retrieve vendor information"); + return; + } + + // Fetch stores + const storesResponse = await authAxios.get(`/storefront`); + setVendorStores(storesResponse.data); + + if (storesResponse.data.length > 0) { + setSelectedStore(storesResponse.data[0]._id); } } catch (error) { console.error("Error fetching stores:", error); @@ -59,7 +65,7 @@ export default function NewChatForm() { }; fetchVendorStores(); - }, [router]); + }, []); const handleBackClick = () => { router.push("/dashboard/chats");