This commit is contained in:
NotII
2025-03-03 23:21:09 +00:00
parent c70828ddcf
commit 2d0c74d906
2 changed files with 135 additions and 48 deletions

View File

@@ -56,6 +56,7 @@ export default function ChatList() {
// First, get vendor info using the /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');
console.log("Vendor auth response:", vendorResponse.data);
// 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 = vendorResponse.data.vendor?._id;
@@ -66,16 +67,32 @@ export default function ChatList() {
return; return;
} }
// Fetch vendor's stores using storefront endpoint // Fetch vendor's store using storefront endpoint
const storesResponse = await authAxios.get(`/storefront`); const storeResponse = await authAxios.get(`/storefront`);
setVendorStores(storesResponse.data); console.log("Store response:", storeResponse.data);
if (storesResponse.data.length > 0) { // Handle both array and single object responses
setSelectedStore(storesResponse.data[0]._id); if (Array.isArray(storeResponse.data)) {
// If it's an array, use it as is
setVendorStores(storeResponse.data);
if (storeResponse.data.length > 0) {
setSelectedStore(storeResponse.data[0]._id);
}
} else if (storeResponse.data && typeof storeResponse.data === 'object' && storeResponse.data._id) {
// If it's a single store object, convert it to an array with one element
const singleStore = [storeResponse.data];
setVendorStores(singleStore);
setSelectedStore(storeResponse.data._id);
} else {
console.error("Expected store data but received:", storeResponse.data);
setVendorStores([]);
toast.error("Failed to load store data in expected format");
} }
} catch (error) { } catch (error) {
console.error("Error fetching vendor data:", error); console.error("Error fetching vendor data:", error);
toast.error("Failed to load vendor data"); toast.error("Failed to load vendor data");
setVendorStores([]);
} }
}; };
@@ -85,16 +102,23 @@ export default function ChatList() {
// Fetch chats and unread counts when store is selected // Fetch chats and unread counts when store is selected
useEffect(() => { useEffect(() => {
const fetchChats = async () => { const fetchChats = async () => {
if (!selectedStore) return; if (!selectedStore) {
console.log("No store selected, skipping fetch chats");
setLoading(false);
return;
}
console.log("Fetching chats for store:", selectedStore);
setLoading(true); setLoading(true);
try { try {
// Get auth token from cookies // Get auth token from cookies
const authToken = getCookie("Authorization"); const authToken = getCookie("Authorization");
if (!authToken) { if (!authToken) {
console.log("No auth token found");
toast.error("You need to be logged in"); toast.error("You need to be logged in");
router.push("/auth/login"); router.push("/auth/login");
setLoading(false);
return; return;
} }
@@ -108,6 +132,7 @@ export default function ChatList() {
// Get vendor ID from profile // Get vendor ID from profile
const vendorResponse = await authAxios.get('/auth/me'); const vendorResponse = await authAxios.get('/auth/me');
console.log("Vendor response:", vendorResponse.data);
// 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 = vendorResponse.data.vendor?._id;
@@ -115,27 +140,35 @@ export default function ChatList() {
if (!vendorId) { if (!vendorId) {
console.error("Vendor ID not found in profile response:", vendorResponse.data); console.error("Vendor ID not found in profile response:", vendorResponse.data);
toast.error("Could not retrieve vendor information"); toast.error("Could not retrieve vendor information");
setLoading(false);
return; return;
} }
// Fetch chats // Fetch chats
console.log("Fetching chats for vendor:", vendorId);
const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`); const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`);
console.log("Chats response:", chatsResponse.data);
// Filter chats by selected store // Filter chats by selected store
const filteredChats = chatsResponse.data.filter( const filteredChats = chatsResponse.data.filter(
(chat: Chat) => chat.storeId === selectedStore (chat: Chat) => chat.storeId === selectedStore
); );
console.log("Filtered chats:", filteredChats);
setChats(filteredChats); setChats(filteredChats);
// Fetch unread counts // Fetch unread counts
const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`); const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
console.log("Unread counts:", unreadResponse.data);
setUnreadCounts(unreadResponse.data); setUnreadCounts(unreadResponse.data);
console.log("Chat loading complete");
} 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 { } finally {
setLoading(false); setLoading(false);
console.log("Loading state set to false");
} }
}; };
@@ -188,32 +221,50 @@ export default function ChatList() {
<span>Customer Chats</span> <span>Customer Chats</span>
<Button onClick={handleCreateChat}>New Chat</Button> <Button onClick={handleCreateChat}>New Chat</Button>
</CardTitle> </CardTitle>
<div className="flex items-center space-x-2"> {vendorStores.length === 0 ? (
<label htmlFor="store-select" className="text-sm font-medium"> <div className="text-sm text-muted-foreground">
Store: No store available. Please create a store first.
</label> </div>
<select ) : vendorStores.length === 1 ? (
id="store-select" <div className="text-sm font-medium">
value={selectedStore} Store: {vendorStores[0].name}
onChange={handleStoreChange} </div>
className="rounded-md border border-input bg-background px-3 py-2 text-sm" ) : (
> <div className="flex items-center space-x-2">
{vendorStores.map((store) => ( <label htmlFor="store-select" className="text-sm font-medium">
<option key={store._id} value={store._id}> Store:
{store.name} </label>
</option> <select
))} id="store-select"
</select> value={selectedStore}
</div> onChange={handleStoreChange}
className="rounded-md border border-input bg-background px-3 py-2 text-sm"
>
{vendorStores.map((store) => (
<option key={store._id} value={store._id}>
{store.name}
</option>
))}
</select>
</div>
)}
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{chats.length === 0 ? ( {chats.length === 0 ? (
<div className="text-center py-8 text-muted-foreground"> <div className="text-center py-12 px-4">
<p>No conversations yet</p> <div className="mb-4 mx-auto w-16 h-16 rounded-full bg-muted flex items-center justify-center">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="text-muted-foreground">
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
</svg>
</div>
<h3 className="text-lg font-medium">No chats available</h3>
<p className="text-muted-foreground mt-2 mb-4">
There are no customer conversations for this store yet.
</p>
<Button <Button
variant="outline" variant="default"
className="mt-4"
onClick={handleCreateChat} onClick={handleCreateChat}
className="mt-2"
> >
Start a new conversation Start a new conversation
</Button> </Button>

View File

@@ -41,6 +41,7 @@ export default function NewChatForm() {
// Get vendor profile first // Get vendor profile first
const vendorResponse = await authAxios.get('/auth/me'); const vendorResponse = await authAxios.get('/auth/me');
console.log("Vendor auth response:", vendorResponse.data);
// Extract vendor ID properly // Extract vendor ID properly
const vendorId = vendorResponse.data.vendor?._id; const vendorId = vendorResponse.data.vendor?._id;
@@ -51,16 +52,32 @@ export default function NewChatForm() {
return; return;
} }
// Fetch stores // Fetch store
const storesResponse = await authAxios.get(`/storefront`); const storeResponse = await authAxios.get(`/storefront`);
setVendorStores(storesResponse.data); console.log("Store response:", storeResponse.data);
if (storesResponse.data.length > 0) { // Handle both array and single object responses
setSelectedStore(storesResponse.data[0]._id); if (Array.isArray(storeResponse.data)) {
// If it's an array, use it as is
setVendorStores(storeResponse.data);
if (storeResponse.data.length > 0) {
setSelectedStore(storeResponse.data[0]._id);
}
} else if (storeResponse.data && typeof storeResponse.data === 'object' && storeResponse.data._id) {
// If it's a single store object, convert it to an array with one element
const singleStore = [storeResponse.data];
setVendorStores(singleStore);
setSelectedStore(storeResponse.data._id);
} else {
console.error("Expected store data but received:", storeResponse.data);
setVendorStores([]);
toast.error("Failed to load store data in expected format");
} }
} catch (error) { } catch (error) {
console.error("Error fetching stores:", error); console.error("Error fetching store:", error);
toast.error("Failed to load stores"); toast.error("Failed to load store");
setVendorStores([]);
} }
}; };
@@ -158,20 +175,39 @@ export default function NewChatForm() {
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="store">Store</Label> <Label htmlFor="store">Store</Label>
<select {vendorStores.length === 0 ? (
id="store" <div>
value={selectedStore} <p className="text-sm text-muted-foreground p-2 border rounded-md">
onChange={(e) => setSelectedStore(e.target.value)} No store available. Please create a store first.
className="w-full rounded-md border border-input bg-background px-3 py-2" </p>
required </div>
> ) : vendorStores.length === 1 ? (
<option value="" disabled>Select a store</option> <div className="p-2 border rounded-md bg-muted/20">
{vendorStores.map((store) => ( <p className="text-sm font-medium">
<option key={store._id} value={store._id}> {vendorStores[0].name}
{store.name} </p>
</option> <input
))} type="hidden"
</select> value={vendorStores[0]._id}
name="storeId"
/>
</div>
) : (
<select
id="store"
value={selectedStore}
onChange={(e) => setSelectedStore(e.target.value)}
className="w-full rounded-md border border-input bg-background px-3 py-2"
required
>
<option value="" disabled>Select a store</option>
{vendorStores.map((store) => (
<option key={store._id} value={store._id}>
{store.name}
</option>
))}
</select>
)}
</div> </div>
<div className="space-y-2"> <div className="space-y-2">