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);
|
||||
|
||||
try {
|
||||
console.log('Starting fetch request...');
|
||||
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.status === 401) {
|
||||
signIn();
|
||||
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
|
||||
const emailsArray = Array.isArray(rawData) ? rawData : (rawData.emails || []);
|
||||
// Ensure we have an array to work with
|
||||
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
|
||||
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);
|
||||
console.log('Emails to process:', emailsToProcess);
|
||||
|
||||
// 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);
|
||||
setMailUrl(rawData.mailUrl || 'https://espace.slm-lab.net/apps/mail/'); // Fallback URL
|
||||
setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error('Error fetching emails:', err);
|
||||
console.error('Fetch error:', err);
|
||||
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user