mail page imap connection mime 5 bis rest 16 login page 29
This commit is contained in:
parent
c4355749ce
commit
358bedaeec
@ -38,41 +38,69 @@ export function Email() {
|
|||||||
if (!isRefresh) setLoading(true);
|
if (!isRefresh) setLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Starting fetch request...');
|
||||||
const response = await fetch('/api/mail');
|
const response = await fetch('/api/mail');
|
||||||
const rawData = await response.json();
|
console.log('Response status:', response.status);
|
||||||
|
|
||||||
console.log('Raw API response:', rawData);
|
// First, get the raw text of the response
|
||||||
|
const rawText = await response.text();
|
||||||
|
console.log('Raw response text:', rawText);
|
||||||
|
|
||||||
|
// Try to parse it as JSON
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = JSON.parse(rawText);
|
||||||
|
console.log('Parsed data type:', typeof data);
|
||||||
|
console.log('Is Array?', Array.isArray(data));
|
||||||
|
console.log('Data:', data);
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error('JSON parse error:', parseError);
|
||||||
|
throw new Error('Invalid JSON response from server');
|
||||||
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
signIn();
|
signIn();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error(rawData.error || 'Failed to fetch emails');
|
throw new Error(data.error || 'Failed to fetch emails');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle both array and object responses
|
// Ensure we have an array to work with
|
||||||
const emailsArray = Array.isArray(rawData) ? rawData : (rawData.emails || []);
|
let emailsToProcess = [];
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
emailsToProcess = data;
|
||||||
|
} else if (data && Array.isArray(data.emails)) {
|
||||||
|
emailsToProcess = data.emails;
|
||||||
|
} else {
|
||||||
|
console.error('Unexpected data structure:', data);
|
||||||
|
throw new Error('Invalid response format: no emails array found');
|
||||||
|
}
|
||||||
|
|
||||||
// Transform the emails array into the expected format
|
console.log('Emails to process:', emailsToProcess);
|
||||||
const transformedEmails = emailsArray.map((email: any) => ({
|
|
||||||
id: String(email.id),
|
|
||||||
subject: email.subject || '(No subject)',
|
|
||||||
sender: {
|
|
||||||
name: extractSenderName(email.from),
|
|
||||||
email: extractEmailAddress(email.from)
|
|
||||||
},
|
|
||||||
date: email.date,
|
|
||||||
isUnread: !email.read
|
|
||||||
}));
|
|
||||||
|
|
||||||
console.log('Transformed emails:', transformedEmails);
|
// Transform the emails
|
||||||
|
const transformedEmails = emailsToProcess.map((email) => {
|
||||||
|
console.log('Processing email:', email);
|
||||||
|
return {
|
||||||
|
id: String(email.id || ''),
|
||||||
|
subject: email.subject || '(No subject)',
|
||||||
|
sender: {
|
||||||
|
name: extractSenderName(email.from || ''),
|
||||||
|
email: extractEmailAddress(email.from || '')
|
||||||
|
},
|
||||||
|
date: email.date || new Date().toISOString(),
|
||||||
|
isUnread: !email.read
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Final transformed emails:', transformedEmails);
|
||||||
|
|
||||||
setEmails(transformedEmails);
|
setEmails(transformedEmails);
|
||||||
setMailUrl(rawData.mailUrl || 'https://espace.slm-lab.net/apps/mail/'); // Fallback URL
|
setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');
|
||||||
setError(null);
|
setError(null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching emails:', err);
|
console.error('Fetch error:', err);
|
||||||
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user