Refactor API URLs and add environment config example

Replaces hardcoded production API URLs with localhost defaults for local development in both server and client code. Updates Dockerfile to require API URLs via deployment environment variables. Improves ChatTable to use a batch endpoint for chats and unread counts, with backward compatibility. Adds an env.example file to document required environment variables. Updates next.config.mjs to use environment variables for backend API rewrites and image domains.
This commit is contained in:
NotII
2025-09-01 15:35:10 +01:00
parent 3528ca45cc
commit 29ec1be68c
8 changed files with 74 additions and 29 deletions

View File

@@ -176,24 +176,39 @@ export default function ChatTable() {
// Get the vendor ID from the auth token
const { vendorId } = getVendorIdFromToken();
// Now fetch chats for this vendor using clientFetch with pagination
const response = await clientFetch(`/chats/vendor/${vendorId}?page=${page}&limit=${limit}`);
// Use the optimized batch endpoint that fetches chats and unread counts together
const batchResponse = await clientFetch(`/chats/vendor/${vendorId}/batch?page=${page}&limit=${limit}`);
// Check if the response is the old format (array) or new paginated format
if (Array.isArray(response)) {
// Handle old API response format (backward compatibility)
setChats(response);
// Handle batch response (contains both chats and unread counts)
if (Array.isArray(batchResponse)) {
// Fallback to old API response format (backward compatibility)
setChats(batchResponse);
setTotalPages(1);
setTotalChats(response.length);
setTotalChats(batchResponse.length);
// Try to fetch unread counts separately if using old endpoint
try {
const unreadResponse = await clientFetch(`/chats/vendor/${vendorId}/unread`);
setUnreadCounts(unreadResponse);
} catch (error) {
console.warn("Failed to fetch unread counts:", error);
}
} else {
// Handle new paginated response format
setChats(response.chats || []);
setTotalPages(response.totalPages || 1);
setCurrentPage(response.page || 1);
setTotalChats(response.totalChats || 0);
// Handle new batch response format
setChats(batchResponse.chats || []);
setTotalPages(batchResponse.totalPages || 1);
setCurrentPage(batchResponse.page || 1);
setTotalChats(batchResponse.totalChats || 0);
// Handle unread counts from batch response
const newUnreadCounts = batchResponse.unreadCounts || { totalUnread: 0, chatCounts: {} };
// Play sound if there are new messages
if (newUnreadCounts.totalUnread > unreadCounts.totalUnread) {
//playNotificationSound();
}
setUnreadCounts(newUnreadCounts);
}
await fetchUnreadCounts();
} catch (error) {
console.error("Failed to fetch chats:", error);
toast.error("Failed to load chat conversations");