-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
41 lines (32 loc) · 1.18 KB
/
main.js
File metadata and controls
41 lines (32 loc) · 1.18 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
import {sanitizeHtml} from './sanitazeHtml.js';
import {deleteTodo, getTodos, postTodo} from "./api.js";
import {renderTasks} from "./renderTasks.js";
const buttonElement = document.getElementById("add-button");
const textInputElement = document.getElementById("text-input");
let tasks = [];
const fetchAndRenderTasks = () => {
getTodos().then((responseData) => {
tasks = responseData.todos;
renderTasks({ tasks, fetchAndRenderTasks });
return true;
});
};
fetchAndRenderTasks();
buttonElement.addEventListener("click", () => {
if (textInputElement.value === "") {
return;
}
buttonElement.disabled = true;
buttonElement.textContent = "Элемент добавлятся...";
postTodo({
text: textInputElement.value
}).then(() => {
return fetchAndRenderTasks();
})
.then(() => {
buttonElement.disabled = false;
buttonElement.textContent = "Добавить";
textInputElement.value = "";
});
renderTasks({ tasks, fetchAndRenderTasks });
});