forked from chandradeoarya/todolist-flask
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.html
More file actions
206 lines (183 loc) · 6.6 KB
/
index.html
File metadata and controls
206 lines (183 loc) · 6.6 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
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<title>My Todo App</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
#taskList {
padding: 0;
}
#taskList li {
list-style: none;
background: #ddd;
padding: 10px;
margin-bottom: 10px;
border-radius: 3px;
}
input[type=text] {
padding: 10px;
border: none;
border-radius: 3px;
margin-right: 10px;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
border: none;
background: #5cb85c;
color: white;
border-radius: 3px;
cursor: pointer;
}
button:hover {
background: #4cae4c;
}
.task-table {
width: 100%;
border-collapse: collapse;
}
.task-table th,
.task-table td {
border: 1px solid #ddd;
padding: 8px;
}
.task-table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
const API_URL = 'http://localhost'; // replace with your API endpoint
var taskIdToEdit;
function fetchTasks() {
$.getJSON(API_URL + '/todos', function(data) {
var tasks = data.tasks;
var output = '<table class="task-table">';
output += '<tr><th>Id</th><th>Title</th><th>Description</th><th>Is it done?</th><th>Change</th></tr>';
for (var i in tasks) {
output += `<tr><td>${tasks[i].task_id}</td><td>${tasks[i].title}</td><td>${tasks[i].description}</td><td>${tasks[i].is_done}</td><td> <button class="btn btn-primary edit-btn" data-id="${tasks[i].task_id}" data-toggle="modal" data-target="#editTaskModal">Edit</button> </td></tr>`;
}
output += '</table>';
$('#taskList').html(output);
$('.edit-btn').click(function() {
taskIdToEdit = $(this).data('id'); // Get id of task to edit
// fetch the task and populate the form fields
$.getJSON(API_URL + '/todos/' + taskIdToEdit, function(data) {
$('#edit-title').val(data.task.title);
$('#edit-description').val(data.task.description);
$('#edit-is_done').prop('checked', data.task.is_done);
});
});
});
}
$('#editTaskForm').submit(function(e) {
e.preventDefault();
// use taskIdToEdit here
$.ajax({
url: API_URL + '/todos/' + taskIdToEdit,
type: 'PUT',
data: {
title: $('#edit-title').val(),
description: $('#edit-description').val(),
is_done: $('#edit-is_done').is(':checked') ? 1 : 0
},
success: function(result) {
// do something with the result
$('#editTaskModal').modal('hide');
fetchTasks();
}
});
});
function addTask() {
var title = $('#title').val();
var description = $('#description').val();
$.ajax({
url: API_URL + '/todos',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ 'title': title, 'description': description }),
success: function(response) {
console.log(response);
fetchTasks();
}
});
}
function deleteTask() {
var taskId = $('#taskId').val();
$.ajax({
url: API_URL + '/todos/' + taskId,
type: 'DELETE',
success: function(response) {
console.log(response);
fetchTasks();
}
});
}
$(document).ready(function() {
fetchTasks();
$('#addButton').click(addTask);
$('#deleteButton').click(deleteTask);
});
</script>
<script>
</script>
</head>
<body>
<h1>My Todo App</h1>
<ul id="taskList"></ul>
<h2>Add Task</h2>
<input type="text" id="title" placeholder="Title">
<input type="text" id="description" placeholder="Description">
<button id="addButton">Add Task</button>
<h2>Delete Task</h2>
<input type="text" id="taskId" placeholder="Task Id">
<button id="deleteButton">Delete Task</button>
<div class="modal fade" id="editTaskModal" tabindex="-1" role="dialog" aria-labelledby="editTaskModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editTaskModalLabel">Edit Task</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="editTaskForm">
<div class="form-group">
<label for="edit-title">Title</label>
<input type="text" class="form-control" id="edit-title" required>
</div>
<div class="form-group">
<label for="edit-description">Description</label>
<textarea class="form-control" id="edit-description"></textarea>
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="edit-is_done">
<label class="form-check-label" for="edit-is_done">Is Done</label>
</div>
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>