fix: Preserve HTTP status code in Response::json and handle 401 session expiration gracefully in loadConversations
This commit is contained in:
@@ -660,10 +660,23 @@ class TeacherPortal
|
||||
}
|
||||
}
|
||||
|
||||
function getAuthToken() {
|
||||
return localStorage.getItem('saqel_teacher_jwt') || getCookie('saqel_teacher_jwt') || '';
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
const v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
|
||||
return v ? v[2] : null;
|
||||
}
|
||||
|
||||
// 2. Load Conversations List via REST API
|
||||
async function loadConversations() {
|
||||
const token = localStorage.getItem('saqel_teacher_jwt');
|
||||
if (!token) return;
|
||||
const token = getAuthToken();
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (!token) {
|
||||
if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">يرجى تسجيل الدخول أولاً</div>';
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await fetch('/api/chat/conversations', {
|
||||
headers: { 'Authorization': 'Bearer ' + token }
|
||||
@@ -674,10 +687,12 @@ class TeacherPortal
|
||||
data = JSON.parse(text);
|
||||
} catch (jsonErr) {
|
||||
console.error('Conversations parse error:', jsonErr, 'Raw:', text);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">خطأ في معالجة البيانات</div>';
|
||||
}
|
||||
if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">خطأ في معالجة البيانات</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.status === 401 || (data && data.error === 'Unauthorized')) {
|
||||
if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">انتهت الجلسة — يرجى تسجيل الدخول مجدداً</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -685,14 +700,12 @@ class TeacherPortal
|
||||
renderConversations(data.data || []);
|
||||
} else {
|
||||
console.warn('Load convs returned error payload:', data);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">' + (data.message || 'لا توجد محادثات نشطة') + '</div>';
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Load convs network error:', e);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">تعذر تحميل المحادثات</div>';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user