diff --git a/components/dashboard/ChatList.tsx b/components/dashboard/ChatList.tsx index 051b27f..5037abc 100644 --- a/components/dashboard/ChatList.tsx +++ b/components/dashboard/ChatList.tsx @@ -154,92 +154,75 @@ export default function ChatList() { }, [router]); // Fetch chats and unread counts when store is selected - useEffect(() => { - const fetchChats = async () => { - if (!selectedStore) { - console.log("No store selected, skipping fetch chats"); - setLoading(false); + const fetchChats = async () => { + try { + if (!selectedStore) return; + + // Get vendor ID from auth token + const authToken = getCookie("Authorization"); + + if (!authToken) { + console.error("No auth token found"); 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; + // Set up axios with auth token + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` } - - // 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 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; - - 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); - - // Check if there are new unread messages and play sound - if (!loading && unreadResponse.data.totalUnread > previousTotalUnread) { - playNotificationSound(); - } - - // Update states - setUnreadCounts(unreadResponse.data); - setPreviousTotalUnread(unreadResponse.data.totalUnread); - setLoading(false); - - console.log("Chat loading complete"); - } catch (error) { - console.error("Error fetching chats:", error); - toast.error("Failed to load chats"); - } finally { - console.log("Loading state set to false"); + }); + + // Get vendor ID from JWT token + const tokenParts = authToken.split('.'); + const payload = JSON.parse(atob(tokenParts[1])); + const vendorId = payload.id; + + console.log("Fetching chats for vendor:", vendorId, "store:", selectedStore); + + // Fetch chats for this vendor and filter by selected store + const response = await authAxios.get(`/chats/vendor/${vendorId}`); + console.log("All chats:", response.data); + + const filteredChats = response.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); + + // Check if there are new unread messages and play sound + if (!loading && unreadResponse.data.totalUnread > previousTotalUnread) { + playNotificationSound(); } - }; - - fetchChats(); - - // Set up polling for updates every 30 seconds - const intervalId = setInterval(fetchChats, 10000); - - return () => clearInterval(intervalId); + + // Update states + setUnreadCounts(unreadResponse.data); + setPreviousTotalUnread(unreadResponse.data.totalUnread); + setLoading(false); + + console.log("Chat loading complete"); + } catch (error) { + console.error("Error fetching chats:", error); + toast.error("Failed to load chats"); + } + }; + + // Add polling effect + useEffect(() => { + if (selectedStore) { + fetchChats(); + + // Poll for updates every 10 seconds + const intervalId = setInterval(fetchChats, 10000); + + return () => clearInterval(intervalId); + } }, [selectedStore]); // Handle chat selection