const deleteTaskWithUndo = async (taskId) => {
// Store task data before deletion for potential undo
const taskResponse = await fetch(
`https://api.vambe.ai/api/public/tasks?taskId=${taskId}`,
{
headers: {
'x-api-key': 'your_api_key_here',
},
},
);
const taskData = await taskResponse.json();
// Delete the task
const deleteResponse = await fetch(
`https://api.vambe.ai/api/public/tasks/${taskId}`,
{
method: 'DELETE',
headers: {
'x-api-key': 'your_api_key_here',
},
},
);
const result = await deleteResponse.json();
if (result.status === 'success') {
return {
success: true,
undoData: taskData,
undo: async () => {
// Recreate the task if undo is called
const recreateResponse = await fetch(
'https://api.vambe.ai/api/public/tasks',
{
method: 'POST',
headers: {
'x-api-key': 'your_api_key_here',
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: taskData.title,
description: taskData.description,
dueDate: taskData.due_at,
dueTime: taskData.is_all_day ? null : '14:00',
timezone: taskData.due_timezone,
responsibleIds: taskData.task_responsibles.map(
(r) => r.responsible_id,
),
aiContactId: taskData.ai_contact_id,
aiCustomerId: taskData.ai_customer_id,
priority: taskData.priority.toString(), // Convert number to string
}),
},
);
return await recreateResponse.json();
},
};
}
return { success: false };
};
// Example usage
const result = await deleteTaskWithUndo('123e4567-e89b-12d3-a456-426614174000');
if (result.success) {
showNotification('Task deleted. Undo?', 'info', {
action: 'Undo',
onAction: result.undo,
});
}