Update conversations.php
This commit is contained in:
+59
-1
@@ -782,6 +782,53 @@
|
|||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper para llamadas a la API desde esta clase
|
||||||
|
async apiCall(endpoint, options = {}) {
|
||||||
|
const defaultOptions = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Si se pasa body, asumimos POST (y serializamos)
|
||||||
|
const finalOptions = { ...defaultOptions, ...options };
|
||||||
|
if (options.body && typeof options.body === 'object') {
|
||||||
|
finalOptions.method = 'POST';
|
||||||
|
finalOptions.body = JSON.stringify(options.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalizar endpoint: prefijar 'api/' si no es URL completa y no empieza con 'api/'
|
||||||
|
let url = endpoint;
|
||||||
|
if (!/^https?:\/\//i.test(url) && !url.startsWith('api/')) {
|
||||||
|
url = 'api/' + url;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(url, finalOptions);
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
// Sesión expirada: redirigir al login
|
||||||
|
console.warn('apiCall: unauthorized, redirecting to login');
|
||||||
|
window.location.href = 'login.php';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('HTTP error! status: ' + response.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
const text = await response.text();
|
||||||
|
if (!text || text.trim() === '') return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(text);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('apiCall parse error for', url, err);
|
||||||
|
const snippet = text.length > 1000 ? text.substr(0, 1000) : text;
|
||||||
|
throw new Error('Invalid JSON response from ' + url + ': ' + err.message + ' -- response snippet: ' + snippet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async loadConversations(page = 1, append = false) {
|
async loadConversations(page = 1, append = false) {
|
||||||
if (this.loadingConversations) return;
|
if (this.loadingConversations) return;
|
||||||
this.loadingConversations = true;
|
this.loadingConversations = true;
|
||||||
@@ -1210,7 +1257,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let url = `get_user_conversations.php?user_id=${userId}&limit=${this.messageLimit}`;
|
let url = `get_user_messages.php?user_id=${userId}&limit=${this.messageLimit}`;
|
||||||
if (!initial && this.earliestMessage) {
|
if (!initial && this.earliestMessage) {
|
||||||
url += `&before=${encodeURIComponent(this.earliestMessage)}`;
|
url += `&before=${encodeURIComponent(this.earliestMessage)}`;
|
||||||
}
|
}
|
||||||
@@ -1267,6 +1314,17 @@
|
|||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error en loadMessages:', error);
|
console.error('Error en loadMessages:', error);
|
||||||
|
if (container) {
|
||||||
|
container.innerHTML = `
|
||||||
|
<div class="text-center p-4">
|
||||||
|
<i class="fas fa-exclamation-triangle text-warning fa-2x"></i>
|
||||||
|
<p class="mt-3">Error al cargar los mensajes: ${error.message || error}</p>
|
||||||
|
<button class="btn btn-sm btn-primary" id="retry-load-messages">Reintentar</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
const retryBtn = document.getElementById('retry-load-messages');
|
||||||
|
if (retryBtn) retryBtn.addEventListener('click', async () => { await this.loadMessages(userId, true); });
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this.loadingMessages = false;
|
this.loadingMessages = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user