-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
155 lines (132 loc) · 4.38 KB
/
script.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
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
"use strict"
$(".search").on("input", function() {
updateTodoList();
});
let Todo = function(title, description, place, dueDate) {
this.title = title;
this.description = description;
this.place = place;
this.dueDate = dueDate;
}
let todoList = [];
let restoreTodoList = function() {
try {
$.ajax({
url: BASE_URL,
type: 'GET',
headers: {
'X-Master-Key': SECRET_KEY
},
success: (data) => {
for (let r of data.record) {
todoList.push(new Todo(
r.title,
r.description,
r.place,
new Date(r.dueDate)
));
}
updateTodoList();
},
error: (err) => {
console.log(err.responseJSON);
}
});
} catch (error) {
console.error(error);
console.log(
"Make sure there is a `key.js` file in the project's root "
+ "directory with the following content:\n"
+ "const BASE_URL = \"<BIN_URL>\";\n"
+ "const SECRET_KEY = \"<SECRET_KEY>\";\n"
);
}
}
let saveTodoList = function() {
$.ajax({
url: BASE_URL,
type: 'PUT',
headers: {
'X-Master-Key': SECRET_KEY
},
contentType: 'application/json',
data: JSON.stringify(todoList),
success: (data) => {
console.log(data);
},
error: (err) => {
console.log(err.responseJSON);
}
});
}
let addTodo = function() {
todoList.push(
new Todo(
$("#inputTitle").val(),
$("#inputDescription").val(),
$("#inputPlace").val(),
$("#inputDate").val()
)
);
saveTodoList();
updateTodoList();
}
let deleteTodo = function(index) {
todoList.splice(index, 1);
saveTodoList();
updateTodoList();
}
let updateTodoList = function() {
let $todoListDiv = $("#todoListView");
$todoListDiv.children().remove()
let $table = $("<table>", {"class": "container"});
let searchText = $("#inputSearch").val().toLowerCase();
let $startDate = $("#startDate");
let $endDate = $("#endDate");
for (let idx in todoList) {
if (
(
(searchText === "")
|| (todoList[idx].title.toLowerCase().includes(searchText))
|| (todoList[idx].description.toLowerCase().includes(searchText))
|| (todoList[idx].place.toLowerCase().includes(searchText))
) && (
$startDate.val() === $startDate.prop("defaultValue")
|| todoList[idx].dueDate.getTime() >= $startDate.prop("valueAsDate").getTime()
) && (
$endDate.val() === $endDate.prop("defaultValue")
|| todoList[idx].dueDate.getTime() <= $endDate.prop("valueAsDate").getTime()
)
) {
let $titleCell = $("<th>", {"class": "col text-center"});
$titleCell.text(todoList[idx].title);
let $descCell = $("<td>", {"class": "col text-center"});
$descCell.text(todoList[idx].description);
let $placeCell = $("<td>", {"class": "col text-center"});
$placeCell.text(todoList[idx].place);
let $dateCell = $("<td>", {"class": "col text-center"});
let formattedDate = todoList[idx].dueDate.toLocaleString("pl-PL", {
day: "numeric",
month: "numeric",
year: "numeric",
});
$dateCell.text(formattedDate);
let $closeButtonCell = $("<td>", {"class": "col text-center"});
let $closeButton = $("<td>", {"type": "button", "class": "btn btn-danger"});
$closeButton.click("click", () => {
deleteTodo(idx);
});
$closeButton.html('<span class="fa-solid fa-trash"></span>');
$closeButtonCell.append($closeButton);
let $row = $("<tr>", {"class": "row my-1"});
$row.append($titleCell);
$row.append($descCell);
$row.append($placeCell);
$row.append($dateCell);
$row.append($closeButtonCell);
$table.append($row);
}
}
$todoListDiv.append($table);
}
restoreTodoList();