diff --git a/app/mail/page.tsx b/app/mail/page.tsx
index edd3c48..a500e65 100644
--- a/app/mail/page.tsx
+++ b/app/mail/page.tsx
@@ -692,29 +692,81 @@ export default function MailPage() {
};
// Handles bulk actions for selected emails
- const handleBulkAction = (action: 'delete' | 'mark-read' | 'mark-unread' | 'archive') => {
- selectedEmails.forEach(emailId => {
- const email = emails.find(e => e.id.toString() === emailId);
- if (email) {
- switch (action) {
- case 'delete':
- setEmails(emails.filter(e => e.id.toString() !== emailId));
- break;
- case 'mark-read':
- handleMarkAsRead(emailId, true);
- break;
- case 'mark-unread':
- handleMarkAsRead(emailId, false);
- break;
- case 'archive':
- setEmails(emails.map(e =>
- e.id.toString() === emailId ? { ...e, folder: 'Archive' } : e
- ));
- break;
- }
+ const handleBulkAction = async (action: 'delete' | 'mark-read' | 'mark-unread' | 'archive') => {
+ if (action === 'delete') {
+ setDeleteType('emails');
+ setShowDeleteConfirm(true);
+ return;
+ }
+
+ try {
+ const response = await fetch('/api/mail/bulk-actions', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ emailIds: selectedEmails,
+ action: action
+ }),
+ });
+
+ if (!response.ok) {
+ throw new Error('Failed to perform bulk action');
}
- });
- setSelectedEmails([]);
+
+ // Update local state based on the action
+ setEmails(emails.map(email => {
+ if (selectedEmails.includes(email.id.toString())) {
+ switch (action) {
+ case 'mark-read':
+ return { ...email, read: true };
+ case 'mark-unread':
+ return { ...email, read: false };
+ case 'archive':
+ return { ...email, folder: 'Archive' };
+ default:
+ return email;
+ }
+ }
+ return email;
+ }));
+
+ // Clear selection after successful action
+ setSelectedEmails([]);
+ } catch (error) {
+ console.error('Error performing bulk action:', error);
+ alert('Failed to perform bulk action. Please try again.');
+ }
+ };
+
+ // Add handleDeleteConfirm function
+ const handleDeleteConfirm = async () => {
+ try {
+ const response = await fetch('/api/mail/bulk-actions', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ emailIds: selectedEmails,
+ action: 'delete'
+ }),
+ });
+
+ if (!response.ok) {
+ throw new Error('Failed to delete emails');
+ }
+
+ // Remove deleted emails from state
+ setEmails(emails.filter(email => !selectedEmails.includes(email.id.toString())));
+ setSelectedEmails([]);
+ } catch (error) {
+ console.error('Error deleting emails:', error);
+ alert('Failed to delete emails. Please try again.');
+ } finally {
+ setShowDeleteConfirm(false);
+ }
};
// Add infinite scroll handler
@@ -824,7 +876,7 @@ export default function MailPage() {
);
- // Update the bulk actions toolbar to fit within email list panel
+ // Update the bulk actions toolbar to include confirmation dialog
const renderBulkActionsToolbar = () => {
if (selectedEmails.length === 0) return null;
@@ -1463,6 +1515,24 @@ export default function MailPage() {
setAttachments([]);
};
+ // Add the confirmation dialog component
+ const renderDeleteConfirmDialog = () => (
+