Fix: Ensure instance cards appear after creation
The instance cards were not appearing after creation. This commit fixes the issue, ensuring that the cards are displayed whether the instance is connected or disconnected.
This commit is contained in:
parent
54310bd204
commit
11628031a3
@ -56,6 +56,9 @@ const WhatsApp = () => {
|
|||||||
(instance: WhatsAppInstance) => instance.userId === currentUserId
|
(instance: WhatsAppInstance) => instance.userId === currentUserId
|
||||||
);
|
);
|
||||||
setInstances(userInstances);
|
setInstances(userInstances);
|
||||||
|
|
||||||
|
// Log for debugging
|
||||||
|
console.log('Loaded user instances:', userInstances);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error parsing saved instances:", error);
|
console.error("Error parsing saved instances:", error);
|
||||||
}
|
}
|
||||||
@ -84,6 +87,9 @@ const WhatsApp = () => {
|
|||||||
// Add current user's instances to the array
|
// Add current user's instances to the array
|
||||||
const updatedInstances = [...allInstances, ...instances];
|
const updatedInstances = [...allInstances, ...instances];
|
||||||
localStorage.setItem('whatsappInstances', JSON.stringify(updatedInstances));
|
localStorage.setItem('whatsappInstances', JSON.stringify(updatedInstances));
|
||||||
|
|
||||||
|
// Log for debugging
|
||||||
|
console.log('Saved all instances to localStorage:', updatedInstances);
|
||||||
}
|
}
|
||||||
}, [instances, currentUserId]);
|
}, [instances, currentUserId]);
|
||||||
|
|
||||||
@ -235,17 +241,21 @@ const WhatsApp = () => {
|
|||||||
throw new Error(data.message || 'Erro ao criar instância');
|
throw new Error(data.message || 'Erro ao criar instância');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('API response for create instance:', data);
|
||||||
|
|
||||||
// Create new instance object with user ID
|
// Create new instance object with user ID
|
||||||
const newInstance: WhatsAppInstance = {
|
const newInstance: WhatsAppInstance = {
|
||||||
instanceName,
|
instanceName,
|
||||||
instanceId: data.instance.instanceId,
|
instanceId: data.instance?.instanceId || Date.now().toString(), // Fallback if instanceId not provided
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
userId: currentUserId, // Associate with current user
|
userId: currentUserId, // Associate with current user
|
||||||
status: data.instance.status,
|
status: data.instance?.status || 'created',
|
||||||
qrcode: data.qrcode,
|
qrcode: data.qrcode?.base64 || null,
|
||||||
connectionState: 'connecting'
|
connectionState: 'connecting'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('New instance created:', newInstance);
|
||||||
|
|
||||||
// Add new instance to the list
|
// Add new instance to the list
|
||||||
setInstances(prevInstances => [...prevInstances, newInstance]);
|
setInstances(prevInstances => [...prevInstances, newInstance]);
|
||||||
|
|
||||||
@ -259,7 +269,7 @@ const WhatsApp = () => {
|
|||||||
setPhoneNumber('');
|
setPhoneNumber('');
|
||||||
|
|
||||||
// If there's a QR code in the response, show it
|
// If there's a QR code in the response, show it
|
||||||
if (data.qrcode) {
|
if (data.qrcode && data.qrcode.base64) {
|
||||||
setActiveInstance(newInstance);
|
setActiveInstance(newInstance);
|
||||||
setQrCodeData(data.qrcode.base64);
|
setQrCodeData(data.qrcode.base64);
|
||||||
setQrDialogOpen(true);
|
setQrDialogOpen(true);
|
||||||
@ -299,12 +309,13 @@ const WhatsApp = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log('QR Code API response:', data);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(data.message || 'Erro ao obter QR Code');
|
throw new Error(data.message || 'Erro ao obter QR Code');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using the "code" field from the response as the QR code data
|
// Using the "base64" field from the response as the QR code data
|
||||||
if (data && data.base64) {
|
if (data && data.base64) {
|
||||||
// Save the base64 image data directly - it already contains the data:image prefix
|
// Save the base64 image data directly - it already contains the data:image prefix
|
||||||
setQrCodeData(data.base64);
|
setQrCodeData(data.base64);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user