-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (71 loc) · 2.23 KB
/
Copy pathscript.js
File metadata and controls
78 lines (71 loc) · 2.23 KB
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
// script.js
const taskList = document.getElementById('task-list');
function refreshTaskList() {
fetch('/api/tasks')
.then((response) => response.json())
.then((tasks) => {
taskList.innerHTML = '';
tasks.forEach((task) => {
const li = document.createElement('li');
li.innerHTML = `
<strong>${task.name}</strong><br>
${task.description}<br>
Due Date: ${task.dueDate ? new Date(task.dueDate).toLocaleDateString() : 'Not specified'}<br>
<button onclick="deleteTask('${task._id}')">Delete</button>
<button onclick="updateTask('${task._id}')">Update</button>
`;
if (task.isCompleted) {
li.style.backgroundColor = '#c0f0c0';
}
taskList.appendChild(li);
});
});
}
function addTask() {
const taskName = document.getElementById('task-name').value;
const taskDescription = document.getElementById('task-description').value;
const taskDueDate = document.getElementById('task-due-date').value;
fetch('/api/tasks', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: taskName, description: taskDescription, dueDate: taskDueDate }),
})
.then(() => {
refreshTaskList();
document.getElementById('task-name').value = '';
document.getElementById('task-description').value = '';
document.getElementById('task-due-date').value = '';
});
}
function deleteTask(taskId) {
fetch(`/api/tasks/${taskId}`, {
method: 'DELETE',
})
.then(() => {
refreshTaskList();
});
}
function updateTask(taskId) {
const newName = prompt('Enter new task name:');
if (newName !== null) {
const newDescription = prompt('Enter new task description:');
const newDueDate = prompt('Enter new due date (YYYY-MM-DD):');
fetch(`/api/tasks/${taskId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: newName,
description: newDescription,
dueDate: newDueDate,
}),
})
.then(() => {
refreshTaskList();
});
}
}
refreshTaskList();