This commit is contained in:
NotII
2025-03-03 21:30:36 +00:00
parent 3282051671
commit c70828ddcf
4 changed files with 53 additions and 26 deletions

View File

@@ -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); setChat(response.data);
setLoading(false); setLoading(false);
} catch (error) { } 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 content: message
}); });

View File

@@ -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 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 // Fetch vendor's stores using storefront endpoint
const storesResponse = await authAxios.get(`/storefront`); const storesResponse = await authAxios.get(`/storefront`);
@@ -99,11 +107,19 @@ export default function ChatList() {
}); });
// Get vendor ID from profile // Get vendor ID from profile
const vendorResponse = await authAxios.get('/api/auth/me'); 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 chats // Fetch chats
const chatsResponse = await authAxios.get(`/api/chats/vendor/${vendorId}`); const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`);
// Filter chats by selected store // Filter chats by selected store
const filteredChats = chatsResponse.data.filter( const filteredChats = chatsResponse.data.filter(
@@ -113,7 +129,7 @@ export default function ChatList() {
setChats(filteredChats); setChats(filteredChats);
// Fetch unread counts // 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); setUnreadCounts(unreadResponse.data);
} catch (error) { } catch (error) {
console.error("Error fetching chats:", error); console.error("Error fetching chats:", error);

View File

@@ -40,11 +40,16 @@ export default function ChatNotifications() {
} }
}); });
// Get vendor info from profile endpoint - removing /api prefix
const vendorResponse = await authAxios.get('/auth/me'); const vendorResponse = await authAxios.get('/auth/me');
console.log(vendorResponse.data.vendor._id); // Access correct property - the vendor ID is in vendor._id
const vendorId = vendorResponse.data.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`); const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
setUnreadCounts(response.data); setUnreadCounts(response.data);
@@ -56,11 +61,11 @@ export default function ChatNotifications() {
// Create a simplified metadata object with just needed info // Create a simplified metadata object with just needed info
const metadata: Record<string, { buyerId: string }> = {}; const metadata: Record<string, { buyerId: string }> = {};
// Fetch each chat to get buyer IDs // Fetch each chat to get buyer IDs - removing /api prefix
await Promise.all( await Promise.all(
chatIds.map(async (chatId) => { chatIds.map(async (chatId) => {
try { try {
const chatResponse = await authAxios.get(`/api/chats/${chatId}`); const chatResponse = await authAxios.get(`/chats/${chatId}`);
metadata[chatId] = { metadata[chatId] = {
buyerId: chatResponse.data.buyerId, buyerId: chatResponse.data.buyerId,
}; };

View File

@@ -24,16 +24,14 @@ export default function NewChatForm() {
useEffect(() => { useEffect(() => {
const fetchVendorStores = async () => { const fetchVendorStores = async () => {
try { try {
// Get auth token from cookies
const authToken = getCookie("Authorization"); const authToken = getCookie("Authorization");
if (!authToken) { 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"); router.push("/auth/login");
return; return;
} }
// Set up axios with the 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: {
@@ -41,16 +39,24 @@ export default function NewChatForm() {
} }
}); });
// Get vendor ID from profile // Get vendor profile first
const vendorResponse = await authAxios.get('/api/auth/me'); const vendorResponse = await authAxios.get('/auth/me');
const vendorId = vendorResponse.data._id;
// Fetch vendor's stores using storefront endpoint // Extract vendor ID properly
const response = await authAxios.get(`/api/storefront`); const vendorId = vendorResponse.data.vendor?._id;
setVendorStores(response.data);
if (response.data.length > 0) { if (!vendorId) {
setSelectedStore(response.data[0]._id); 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) { } catch (error) {
console.error("Error fetching stores:", error); console.error("Error fetching stores:", error);
@@ -59,7 +65,7 @@ export default function NewChatForm() {
}; };
fetchVendorStores(); fetchVendorStores();
}, [router]); }, []);
const handleBackClick = () => { const handleBackClick = () => {
router.push("/dashboard/chats"); router.push("/dashboard/chats");