-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (179 loc) · 6.14 KB
/
script.js
File metadata and controls
199 lines (179 loc) · 6.14 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const apiUrl = "<Paste-your-invoke-url-here>";
//Function to add a new task
function addTask() {
const taskInput = document.getElementById('new-task');
const taskText = taskInput.value.trim();
if (taskText) {
const taskId = prompt("Enter task ID:");
if (taskId === null) {
taskInput.value = '';
return;
}
checkIfTaskIdExists(taskId).then(exists => {
if (exists) {
alert("Task ID already exists. Please enter a different Task ID.");
} else {
const taskDescription = prompt("Enter task description:");
if (taskDescription === null) {
taskInput.value = '';
return;
}
const taskStatus = prompt("Enter task status:");
if (taskStatus === null) {
taskInput.value = '';
return;
}
const taskData = {
TaskID: taskId,
TaskName: taskText,
TaskDescription: taskDescription,
TaskStatus: taskStatus
};
fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(taskData)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Task added successfully:", data);
taskInput.value = '';
if (document.getElementById('task-table').style.display === 'table') {
getAllTasks();
}
displayConfirmationMessage();
})
.catch(error => {
console.error("Error adding task:", error);
alert("Failed to add task. Please try again.");
});
}
}).catch(error => {
console.error("Error checking task ID:", error);
alert("Failed to check task ID. Please try again.");
});
}
}
// Function to check if TaskID exists
function checkIfTaskIdExists(taskID) {
return fetch(`${apiUrl}?TaskID=${taskID}`, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
if (data.body) {
data = JSON.parse(data.body);
}
console.log("Check TaskID Response:", data);
return data && data.TaskID === taskID;
})
.catch(error => {
console.error("Error checking task ID:", error);
return false;
});
}
// Function to display confirmation message
function displayConfirmationMessage() {
const confirmationMessage = document.getElementById('confirmation-message');
confirmationMessage.style.display = 'block';
setTimeout(() => {
confirmationMessage.style.display = 'none';
}, 3000);
}
// Function to delete a task and remove it from the DOM
function deleteTask(taskID) {
fetch(`${apiUrl}?TaskID=${taskID}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
console.log("Task deleted successfully:", data);
removeTaskFromDOM(taskID);
if (document.getElementById('task-table').style.display === 'table') {
getAllTasks();
}
})
.catch(error => {
console.error("Error deleting task:", error);
});
}
// Function to remove a task element from the DOM
function removeTaskFromDOM(taskID) {
const taskElement = document.querySelector(`[data-task-id='${taskID}']`);
if (taskElement) {
taskElement.remove();
}
}
// Function to delete a task by ID
function deleteTaskById() {
const taskIDInput = document.getElementById('delete-task-id');
const taskID = taskIDInput.value.trim();
if (taskID) {
deleteTask(taskID);
taskIDInput.value = '';
} else {
alert("Please enter a Task ID.");
}
}
// Function to get all tasks from the database and display them in a table
function getAllTasks() {
document.getElementById('task-table').style.display = 'table';
const cacheBuster = new Date().getTime(); // Unique timestamp to avoid caching
fetch(`${apiUrl}?TaskID=all&cb=${cacheBuster}`, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
if (data.body) {
data = JSON.parse(data.body);
}
console.log("All Tasks:", data);
displayTasksInTable(data);
})
.catch(error => {
console.error("Error fetching all tasks:", error);
alert("Failed to fetch tasks. Please try again.");
});
}
// Function to display tasks in a table
function displayTasksInTable(tasks) {
const tableBody = document.getElementById('task-table-body');
tableBody.innerHTML = ''; // Clear existing rows
tasks.forEach(task => {
const row = document.createElement('tr');
const cellId = document.createElement('td');
cellId.textContent = task.TaskID;
row.appendChild(cellId);
const cellName = document.createElement('td');
cellName.textContent = task.TaskName;
row.appendChild(cellName);
const cellDescription = document.createElement('td');
cellDescription.textContent = task.TaskDescription;
row.appendChild(cellDescription);
const cellStatus = document.createElement('td');
cellStatus.textContent = task.TaskStatus;
row.appendChild(cellStatus);
tableBody.appendChild(row);
});
}
// Function to clear the TaskID input field
function clearTaskIDInput() {
const taskIDInput = document.getElementById('task-id');
taskIDInput.value = '';
}