-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index3.js
89 lines (81 loc) · 2.92 KB
/
Index3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
document.addEventListener("DOMContentLoaded", function () {
loadTasks();
});
function addTask() {
const taskInput = document.getElementById("taskInput");
const prioritySelect = document.getElementById("prioritySelect");
const dueDateInput = document.getElementById("dueDateInput");
const categorySelect = document.getElementById("categorySelect");
const descriptionInput = document.getElementById("descriptionInput");
const taskList = document.getElementById("taskList");
if (taskInput.value.trim() !== "") {
const taskDetails = {
title: taskInput.value,
priority: prioritySelect.value,
dueDate: dueDateInput.value,
category: categorySelect.value,
description: descriptionInput.value,
completed: false
};
fetch('/add_task', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(taskDetails),
})
.then(response => response.json())
.then(data => {
const taskItem = createTaskItem(data);
taskList.appendChild(taskItem);
clearTaskInputFields();
animateTask(taskItem);
})
.catch((error) => {
console.error('Error:', error);
});
}
}
function editTask(button) {
const taskItem = button.parentElement.parentElement;
const taskId = taskItem.getAttribute('data-task-id');
const taskDetails = {
title: taskItem.querySelector('.task-title').textContent,
priority: taskItem.querySelector('.task-info:nth-child(2) select').value,
dueDate: taskItem.querySelector('.task-info:nth-child(3) span').textContent,
category: taskItem.querySelector('.task-info:nth-child(4) select').value,
description: taskItem.querySelector('.task-info:nth-child(5) span').textContent,
completed: taskItem.classList.contains('completed')
};
fetch(`/edit_task/${taskId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(taskDetails),
})
.then(response => response.json())
.then(data => {
deleteTask(button);
const updatedTaskItem = createTaskItem(data);
taskList.appendChild(updatedTaskItem);
animateTask(updatedTaskItem);
})
.catch((error) => {
console.error('Error:', error);
});
}
function deleteTask(button) {
const taskItem = button.parentElement.parentElement;
const taskId = taskItem.getAttribute('data-task-id');
fetch(`/delete_task/${taskId}`, {
method: 'DELETE',
})
.then(response => response.json())
.then(data => {
taskList.removeChild(taskItem);
})
.catch((error) => {
console.error('Error:', error);
});
}