Update ChatList.tsx
This commit is contained in:
@@ -154,92 +154,75 @@ 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 () => {
|
try {
|
||||||
if (!selectedStore) {
|
if (!selectedStore) return;
|
||||||
console.log("No store selected, skipping fetch chats");
|
|
||||||
setLoading(false);
|
// Get vendor ID from auth token
|
||||||
|
const authToken = getCookie("Authorization");
|
||||||
|
|
||||||
|
if (!authToken) {
|
||||||
|
console.error("No auth token found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Fetching chats for store:", selectedStore);
|
// Set up axios with auth token
|
||||||
setLoading(true);
|
const authAxios = axios.create({
|
||||||
try {
|
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
||||||
// Get auth token from cookies
|
headers: {
|
||||||
const authToken = getCookie("Authorization");
|
Authorization: `Bearer ${authToken}`
|
||||||
|
|
||||||
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 the auth token
|
// Get vendor ID from JWT token
|
||||||
const authAxios = axios.create({
|
const tokenParts = authToken.split('.');
|
||||||
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
const payload = JSON.parse(atob(tokenParts[1]));
|
||||||
headers: {
|
const vendorId = payload.id;
|
||||||
Authorization: `Bearer ${authToken}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get vendor ID from profile
|
console.log("Fetching chats for vendor:", vendorId, "store:", selectedStore);
|
||||||
const vendorResponse = await authAxios.get('/auth/me');
|
|
||||||
console.log("Vendor response:", vendorResponse.data);
|
|
||||||
|
|
||||||
// Access correct property - the vendor ID is in vendor._id
|
// Fetch chats for this vendor and filter by selected store
|
||||||
const vendorId = vendorResponse.data.vendor?._id;
|
const response = await authAxios.get(`/chats/vendor/${vendorId}`);
|
||||||
|
console.log("All chats:", response.data);
|
||||||
|
|
||||||
if (!vendorId) {
|
const filteredChats = response.data.filter((chat: Chat) =>
|
||||||
console.error("Vendor ID not found in profile response:", vendorResponse.data);
|
chat.storeId === selectedStore
|
||||||
toast.error("Could not retrieve vendor information");
|
);
|
||||||
setLoading(false);
|
console.log("Filtered chats:", filteredChats);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch chats
|
setChats(filteredChats);
|
||||||
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
|
// Fetch unread counts
|
||||||
const filteredChats = chatsResponse.data.filter(
|
const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
|
||||||
(chat: Chat) => chat.storeId === selectedStore
|
console.log("Unread counts:", unreadResponse.data);
|
||||||
);
|
|
||||||
console.log("Filtered chats:", filteredChats);
|
|
||||||
|
|
||||||
setChats(filteredChats);
|
// Check if there are new unread messages and play sound
|
||||||
|
if (!loading && unreadResponse.data.totalUnread > previousTotalUnread) {
|
||||||
// Fetch unread counts
|
playNotificationSound();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
fetchChats();
|
// Update states
|
||||||
|
setUnreadCounts(unreadResponse.data);
|
||||||
|
setPreviousTotalUnread(unreadResponse.data.totalUnread);
|
||||||
|
setLoading(false);
|
||||||
|
|
||||||
// Set up polling for updates every 30 seconds
|
console.log("Chat loading complete");
|
||||||
const intervalId = setInterval(fetchChats, 10000);
|
} catch (error) {
|
||||||
|
console.error("Error fetching chats:", error);
|
||||||
|
toast.error("Failed to load chats");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return () => clearInterval(intervalId);
|
// Add polling effect
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedStore) {
|
||||||
|
fetchChats();
|
||||||
|
|
||||||
|
// Poll for updates every 10 seconds
|
||||||
|
const intervalId = setInterval(fetchChats, 10000);
|
||||||
|
|
||||||
|
return () => clearInterval(intervalId);
|
||||||
|
}
|
||||||
}, [selectedStore]);
|
}, [selectedStore]);
|
||||||
|
|
||||||
// Handle chat selection
|
// Handle chat selection
|
||||||
|
|||||||
Reference in New Issue
Block a user