diff --git a/components/dashboard/ChatList.tsx b/components/dashboard/ChatList.tsx index 6eff509..caf0885 100644 --- a/components/dashboard/ChatList.tsx +++ b/components/dashboard/ChatList.tsx @@ -56,6 +56,7 @@ export default function ChatList() { // First, get vendor info using the /auth/me endpoint const vendorResponse = await authAxios.get('/auth/me'); + console.log("Vendor auth response:", vendorResponse.data); // Access correct property - the vendor ID is in vendor._id const vendorId = vendorResponse.data.vendor?._id; @@ -66,16 +67,32 @@ export default function ChatList() { return; } - // Fetch vendor's stores using storefront endpoint - const storesResponse = await authAxios.get(`/storefront`); - setVendorStores(storesResponse.data); + // Fetch vendor's store using storefront endpoint + const storeResponse = await authAxios.get(`/storefront`); + console.log("Store response:", storeResponse.data); - if (storesResponse.data.length > 0) { - setSelectedStore(storesResponse.data[0]._id); + // Handle both array and single object responses + if (Array.isArray(storeResponse.data)) { + // If it's an array, use it as is + setVendorStores(storeResponse.data); + + if (storeResponse.data.length > 0) { + setSelectedStore(storeResponse.data[0]._id); + } + } else if (storeResponse.data && typeof storeResponse.data === 'object' && storeResponse.data._id) { + // If it's a single store object, convert it to an array with one element + const singleStore = [storeResponse.data]; + setVendorStores(singleStore); + setSelectedStore(storeResponse.data._id); + } else { + console.error("Expected store data but received:", storeResponse.data); + setVendorStores([]); + toast.error("Failed to load store data in expected format"); } } catch (error) { console.error("Error fetching vendor data:", error); toast.error("Failed to load vendor data"); + setVendorStores([]); } }; @@ -85,16 +102,23 @@ export default function ChatList() { // Fetch chats and unread counts when store is selected useEffect(() => { const fetchChats = async () => { - if (!selectedStore) return; + if (!selectedStore) { + console.log("No store selected, skipping fetch chats"); + setLoading(false); + return; + } + console.log("Fetching chats for store:", selectedStore); setLoading(true); try { // Get auth token from cookies const authToken = getCookie("Authorization"); if (!authToken) { + console.log("No auth token found"); toast.error("You need to be logged in"); router.push("/auth/login"); + setLoading(false); return; } @@ -108,6 +132,7 @@ export default function ChatList() { // Get vendor ID from profile const vendorResponse = await authAxios.get('/auth/me'); + console.log("Vendor response:", vendorResponse.data); // Access correct property - the vendor ID is in vendor._id const vendorId = vendorResponse.data.vendor?._id; @@ -115,27 +140,35 @@ export default function ChatList() { if (!vendorId) { console.error("Vendor ID not found in profile response:", vendorResponse.data); toast.error("Could not retrieve vendor information"); + setLoading(false); return; } // Fetch chats + console.log("Fetching chats for vendor:", vendorId); const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`); + console.log("Chats response:", chatsResponse.data); // Filter chats by selected store const filteredChats = chatsResponse.data.filter( (chat: Chat) => chat.storeId === selectedStore ); + console.log("Filtered chats:", filteredChats); setChats(filteredChats); // Fetch unread counts const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`); + console.log("Unread counts:", unreadResponse.data); setUnreadCounts(unreadResponse.data); + + console.log("Chat loading complete"); } catch (error) { console.error("Error fetching chats:", error); toast.error("Failed to load chats"); } finally { setLoading(false); + console.log("Loading state set to false"); } }; @@ -188,32 +221,50 @@ export default function ChatList() { Customer Chats -
- - -
+ {vendorStores.length === 0 ? ( +
+ No store available. Please create a store first. +
+ ) : vendorStores.length === 1 ? ( +
+ Store: {vendorStores[0].name} +
+ ) : ( +
+ + +
+ )} {chats.length === 0 ? ( -
-

No conversations yet

+
+
+ + + +
+

No chats available

+

+ There are no customer conversations for this store yet. +

diff --git a/components/dashboard/NewChatForm.tsx b/components/dashboard/NewChatForm.tsx index eda29ec..32aa5ed 100644 --- a/components/dashboard/NewChatForm.tsx +++ b/components/dashboard/NewChatForm.tsx @@ -41,6 +41,7 @@ export default function NewChatForm() { // Get vendor profile first const vendorResponse = await authAxios.get('/auth/me'); + console.log("Vendor auth response:", vendorResponse.data); // Extract vendor ID properly const vendorId = vendorResponse.data.vendor?._id; @@ -51,16 +52,32 @@ export default function NewChatForm() { return; } - // Fetch stores - const storesResponse = await authAxios.get(`/storefront`); - setVendorStores(storesResponse.data); + // Fetch store + const storeResponse = await authAxios.get(`/storefront`); + console.log("Store response:", storeResponse.data); - if (storesResponse.data.length > 0) { - setSelectedStore(storesResponse.data[0]._id); + // Handle both array and single object responses + if (Array.isArray(storeResponse.data)) { + // If it's an array, use it as is + setVendorStores(storeResponse.data); + + if (storeResponse.data.length > 0) { + setSelectedStore(storeResponse.data[0]._id); + } + } else if (storeResponse.data && typeof storeResponse.data === 'object' && storeResponse.data._id) { + // If it's a single store object, convert it to an array with one element + const singleStore = [storeResponse.data]; + setVendorStores(singleStore); + setSelectedStore(storeResponse.data._id); + } else { + console.error("Expected store data but received:", storeResponse.data); + setVendorStores([]); + toast.error("Failed to load store data in expected format"); } } catch (error) { - console.error("Error fetching stores:", error); - toast.error("Failed to load stores"); + console.error("Error fetching store:", error); + toast.error("Failed to load store"); + setVendorStores([]); } }; @@ -158,20 +175,39 @@ export default function NewChatForm() {
- + {vendorStores.length === 0 ? ( +
+

+ No store available. Please create a store first. +

+
+ ) : vendorStores.length === 1 ? ( +
+

+ {vendorStores[0].name} +

+ +
+ ) : ( + + )}