hmm
This commit is contained in:
@@ -11,8 +11,7 @@ import {
|
|||||||
DropdownMenuItem,
|
DropdownMenuItem,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from "@/components/ui/dropdown-menu";
|
} from "@/components/ui/dropdown-menu";
|
||||||
import axios from "axios";
|
import { clientFetch } from "@/lib/client-utils";
|
||||||
import { getCookie } from "@/lib/client-utils";
|
|
||||||
|
|
||||||
interface UnreadCounts {
|
interface UnreadCounts {
|
||||||
totalUnread: number;
|
totalUnread: number;
|
||||||
@@ -81,54 +80,45 @@ export default function ChatNotifications() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchUnreadCounts = async () => {
|
const fetchUnreadCounts = async () => {
|
||||||
try {
|
try {
|
||||||
const authToken = getCookie("Authorization");
|
// Use clientFetch instead of direct API calls to leverage the Next.js API rewrite rules
|
||||||
|
|
||||||
if (!authToken) return;
|
// Get vendor info from profile endpoint
|
||||||
|
const vendorData = await clientFetch('/auth/me');
|
||||||
const authAxios = axios.create({
|
|
||||||
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${authToken}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get vendor info from profile endpoint - removing /api prefix
|
|
||||||
const vendorResponse = await authAxios.get('/auth/me');
|
|
||||||
|
|
||||||
// Access correct property - the vendor ID is in vendor._id
|
// Access correct property - the vendor ID is in vendor._id
|
||||||
const vendorId = vendorResponse.data.vendor?._id;
|
const vendorId = vendorData.vendor?._id;
|
||||||
|
|
||||||
if (!vendorId) {
|
if (!vendorId) {
|
||||||
console.error("Vendor ID not found in profile response:", vendorResponse.data);
|
console.error("Vendor ID not found in profile response:", vendorData);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
|
const response = await clientFetch(`/chats/vendor/${vendorId}/unread`);
|
||||||
|
|
||||||
// Check if there are new notifications and play sound if needed
|
// Check if there are new notifications and play sound if needed
|
||||||
if (!loading && response.data.totalUnread > previousUnreadTotal) {
|
if (!loading && response.totalUnread > previousUnreadTotal) {
|
||||||
playNotificationSound();
|
playNotificationSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update state
|
// Update state - clientFetch already parses the JSON
|
||||||
setUnreadCounts(response.data);
|
setUnreadCounts(response);
|
||||||
setPreviousUnreadTotal(response.data.totalUnread);
|
setPreviousUnreadTotal(response.totalUnread);
|
||||||
|
|
||||||
if (response.data.totalUnread > 0) {
|
if (response.totalUnread > 0) {
|
||||||
const chatIds = Object.keys(response.data.chatCounts);
|
const chatIds = Object.keys(response.chatCounts);
|
||||||
|
|
||||||
if (chatIds.length > 0) {
|
if (chatIds.length > 0) {
|
||||||
// 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 - removing /api prefix
|
// Fetch each chat to get buyer IDs
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
chatIds.map(async (chatId) => {
|
chatIds.map(async (chatId) => {
|
||||||
try {
|
try {
|
||||||
// Use markAsRead=false to ensure we don't mark messages as read
|
// Use markAsRead=false to ensure we don't mark messages as read
|
||||||
const chatResponse = await authAxios.get(`/chats/${chatId}?markAsRead=false`);
|
const chatResponse = await clientFetch(`/chats/${chatId}?markAsRead=false`);
|
||||||
metadata[chatId] = {
|
metadata[chatId] = {
|
||||||
buyerId: chatResponse.data.buyerId,
|
buyerId: chatResponse.buyerId,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching chat ${chatId}:`, error);
|
console.error(`Error fetching chat ${chatId}:`, error);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import {
|
|||||||
Eye,
|
Eye,
|
||||||
User
|
User
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import axios from "axios";
|
import { clientFetch } from "@/lib/client-utils";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { getCookie } from "@/lib/client-utils";
|
import { getCookie } from "@/lib/client-utils";
|
||||||
|
|
||||||
@@ -106,17 +106,12 @@ export default function ChatTable() {
|
|||||||
const fetchUnreadCounts = async () => {
|
const fetchUnreadCounts = async () => {
|
||||||
try {
|
try {
|
||||||
// Get the vendor ID from the auth token
|
// Get the vendor ID from the auth token
|
||||||
const { vendorId, authToken } = getVendorIdFromToken();
|
const { vendorId } = getVendorIdFromToken();
|
||||||
|
|
||||||
// Fetch unread counts for this vendor
|
// Fetch unread counts for this vendor using clientFetch
|
||||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/chats/vendor/${vendorId}/unread`, {
|
const response = await clientFetch(`/chats/vendor/${vendorId}/unread`);
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${authToken}`,
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const newUnreadCounts = response.data;
|
const newUnreadCounts = response;
|
||||||
|
|
||||||
// Play sound if there are new messages
|
// Play sound if there are new messages
|
||||||
if (newUnreadCounts.totalUnread > unreadCounts.totalUnread) {
|
if (newUnreadCounts.totalUnread > unreadCounts.totalUnread) {
|
||||||
@@ -135,17 +130,12 @@ export default function ChatTable() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get the vendor ID from the auth token
|
// Get the vendor ID from the auth token
|
||||||
const { vendorId, authToken } = getVendorIdFromToken();
|
const { vendorId } = getVendorIdFromToken();
|
||||||
|
|
||||||
// Now fetch chats for this vendor
|
// Now fetch chats for this vendor using clientFetch
|
||||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/chats/vendor/${vendorId}`, {
|
const response = await clientFetch(`/chats/vendor/${vendorId}`);
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${authToken}`,
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
setChats(Array.isArray(response.data) ? response.data : []);
|
setChats(Array.isArray(response) ? response : []);
|
||||||
await fetchUnreadCounts();
|
await fetchUnreadCounts();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch chats:", error);
|
console.error("Failed to fetch chats:", error);
|
||||||
|
|||||||
@@ -80,8 +80,9 @@ export default function OrderNotifications() {
|
|||||||
yesterday.setDate(yesterday.getDate() - 1);
|
yesterday.setDate(yesterday.getDate() - 1);
|
||||||
const timestamp = yesterday.toISOString();
|
const timestamp = yesterday.toISOString();
|
||||||
|
|
||||||
// Include timestamp filter to reduce load
|
// Use orderDate parameter instead of 'after' to avoid backend casting errors
|
||||||
const orderData = await clientFetch(`/orders?status=paid&limit=10&after=${timestamp}`);
|
// The error logs show that the 'after' parameter is being interpreted as 'orderId' incorrectly
|
||||||
|
const orderData = await clientFetch(`/orders?status=paid&limit=10&orderDate[gte]=${timestamp}`);
|
||||||
const orders: Order[] = orderData.orders || [];
|
const orders: Order[] = orderData.orders || [];
|
||||||
|
|
||||||
// If this is the first fetch, just store the orders without notifications
|
// If this is the first fetch, just store the orders without notifications
|
||||||
|
|||||||
@@ -107,8 +107,9 @@ export default function UnifiedNotifications() {
|
|||||||
yesterday.setDate(yesterday.getDate() - 1);
|
yesterday.setDate(yesterday.getDate() - 1);
|
||||||
const timestamp = yesterday.toISOString();
|
const timestamp = yesterday.toISOString();
|
||||||
|
|
||||||
// Include timestamp filter to reduce load
|
// Use orderDate parameter instead of 'after' to avoid backend casting errors
|
||||||
const orderData = await clientFetch(`/orders?status=paid&limit=10&after=${timestamp}`);
|
// The error logs show that the 'after' parameter is being interpreted as 'orderId' incorrectly
|
||||||
|
const orderData = await clientFetch(`/orders?status=paid&limit=10&orderDate[gte]=${timestamp}`);
|
||||||
const orders: Order[] = orderData.orders || [];
|
const orders: Order[] = orderData.orders || [];
|
||||||
|
|
||||||
// If this is the first fetch, just store the orders without notifications
|
// If this is the first fetch, just store the orders without notifications
|
||||||
@@ -174,41 +175,34 @@ export default function UnifiedNotifications() {
|
|||||||
|
|
||||||
const fetchUnreadCounts = async () => {
|
const fetchUnreadCounts = async () => {
|
||||||
try {
|
try {
|
||||||
const authToken = getCookie("Authorization");
|
// Use clientFetch instead of direct API calls to leverage the API rewrite rules
|
||||||
|
// This will route through Next.js rewrites instead of calling the API directly
|
||||||
if (!authToken) return;
|
|
||||||
|
|
||||||
const authAxios = axios.create({
|
|
||||||
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${authToken}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get vendor info from profile endpoint
|
// Get vendor info from profile endpoint
|
||||||
const vendorResponse = await authAxios.get('/auth/me');
|
const vendorData = await clientFetch('/auth/me');
|
||||||
|
|
||||||
// Access correct property - the vendor ID is in vendor._id
|
// Access correct property - the vendor ID is in vendor._id
|
||||||
const vendorId = vendorResponse.data.vendor?._id;
|
const vendorId = vendorData.vendor?._id;
|
||||||
|
|
||||||
if (!vendorId) {
|
if (!vendorId) {
|
||||||
console.error("Vendor ID not found in profile response:", vendorResponse.data);
|
console.error("Vendor ID not found in profile response:", vendorData);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
|
// Use clientFetch which will properly route through Next.js API rewrites
|
||||||
|
const response = await clientFetch(`/chats/vendor/${vendorId}/unread`);
|
||||||
|
|
||||||
// Check if there are new notifications and play sound if needed
|
// Check if there are new notifications and play sound if needed
|
||||||
if (!loading && response.data.totalUnread > previousUnreadTotal) {
|
if (!loading && response.totalUnread > previousUnreadTotal) {
|
||||||
playNotificationSound();
|
playNotificationSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update chat state
|
// Update chat state - note that clientFetch already parses the JSON response
|
||||||
setUnreadCounts(response.data);
|
setUnreadCounts(response);
|
||||||
setPreviousUnreadTotal(response.data.totalUnread);
|
setPreviousUnreadTotal(response.totalUnread);
|
||||||
|
|
||||||
if (response.data.totalUnread > 0) {
|
if (response.totalUnread > 0) {
|
||||||
const chatIds = Object.keys(response.data.chatCounts);
|
const chatIds = Object.keys(response.chatCounts);
|
||||||
|
|
||||||
if (chatIds.length > 0) {
|
if (chatIds.length > 0) {
|
||||||
// Create a simplified metadata object with just needed info
|
// Create a simplified metadata object with just needed info
|
||||||
@@ -219,9 +213,9 @@ export default function UnifiedNotifications() {
|
|||||||
chatIds.map(async (chatId) => {
|
chatIds.map(async (chatId) => {
|
||||||
try {
|
try {
|
||||||
// Use markAsRead=false to ensure we don't mark messages as read
|
// Use markAsRead=false to ensure we don't mark messages as read
|
||||||
const chatResponse = await authAxios.get(`/chats/${chatId}?markAsRead=false`);
|
const chatResponse = await clientFetch(`/chats/${chatId}?markAsRead=false`);
|
||||||
metadata[chatId] = {
|
metadata[chatId] = {
|
||||||
buyerId: chatResponse.data.buyerId,
|
buyerId: chatResponse.buyerId,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching chat ${chatId}:`, error);
|
console.error(`Error fetching chat ${chatId}:`, error);
|
||||||
|
|||||||
Reference in New Issue
Block a user