Update ChatList.tsx

This commit is contained in:
NotII
2025-03-04 22:52:55 +00:00
parent 75e0b65e11
commit 880613de13

View File

@@ -154,29 +154,19 @@ export default function ChatList() {
}, [router]); }, [router]);
// Fetch chats and unread counts when store is selected // Fetch chats and unread counts when store is selected
useEffect(() => {
const fetchChats = async () => { const fetchChats = async () => {
if (!selectedStore) {
console.log("No store selected, skipping fetch chats");
setLoading(false);
return;
}
console.log("Fetching chats for store:", selectedStore);
setLoading(true);
try { try {
// Get auth token from cookies if (!selectedStore) return;
// Get vendor ID from auth token
const authToken = getCookie("Authorization"); const authToken = getCookie("Authorization");
if (!authToken) { if (!authToken) {
console.log("No auth token found"); console.error("No auth token found");
toast.error("You need to be logged in");
router.push("/auth/login");
setLoading(false);
return; return;
} }
// Set up axios with the auth token // Set up axios with auth token
const authAxios = axios.create({ const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL, baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: { headers: {
@@ -184,28 +174,19 @@ export default function ChatList() {
} }
}); });
// Get vendor ID from profile // Get vendor ID from JWT token
const vendorResponse = await authAxios.get('/auth/me'); const tokenParts = authToken.split('.');
console.log("Vendor response:", vendorResponse.data); const payload = JSON.parse(atob(tokenParts[1]));
const vendorId = payload.id;
// Access correct property - the vendor ID is in vendor._id console.log("Fetching chats for vendor:", vendorId, "store:", selectedStore);
const vendorId = vendorResponse.data.vendor?._id;
if (!vendorId) { // Fetch chats for this vendor and filter by selected store
console.error("Vendor ID not found in profile response:", vendorResponse.data); const response = await authAxios.get(`/chats/vendor/${vendorId}`);
toast.error("Could not retrieve vendor information"); console.log("All chats:", response.data);
setLoading(false);
return;
}
// Fetch chats const filteredChats = response.data.filter((chat: Chat) =>
console.log("Fetching chats for vendor:", vendorId); chat.storeId === selectedStore
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); console.log("Filtered chats:", filteredChats);
@@ -229,17 +210,19 @@ export default function ChatList() {
} catch (error) { } catch (error) {
console.error("Error fetching chats:", error); console.error("Error fetching chats:", error);
toast.error("Failed to load chats"); toast.error("Failed to load chats");
} finally {
console.log("Loading state set to false");
} }
}; };
// Add polling effect
useEffect(() => {
if (selectedStore) {
fetchChats(); fetchChats();
// Set up polling for updates every 30 seconds // Poll for updates every 10 seconds
const intervalId = setInterval(fetchChats, 10000); const intervalId = setInterval(fetchChats, 10000);
return () => clearInterval(intervalId); return () => clearInterval(intervalId);
}
}, [selectedStore]); }, [selectedStore]);
// Handle chat selection // Handle chat selection