-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (76 loc) · 2.4 KB
/
main.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
var todoList = $("#todo-list");
var todoForm = $("#new-todo-form");
var newTodo = $("#new-todo");
newTodo.focus();
todoForm.on('submit', function(event) {
var inputValue = newTodo.val().trim();
if(inputValue !== ""){
console.log("submitted yay");
newTodo.val("");
$.ajax("https://rowan-todo-api.herokuapp.com/items", {
method: "post",
dataType: "json",
contentType: "application/json",
jsonp: false,
data: JSON.stringify({
description: inputValue
}),
error: function () {
console.error("couldn't post todo :(");
},
success: function (items, status) {
loadTodoList();
}
});
}
event.preventDefault();
});
todoList.on('change','.todo-complete', function (event) {
var checkbox = $(event.target);
var checked = checkbox.is(":checked");
var checkboxId = checkbox.attr('id');
var endpoint = "";
console.log(checked);
if(checked) {
endpoint = "https://rowan-todo-api.herokuapp.com/items/" + checkboxId + "/complete"
}
else{
endpoint = "https://rowan-todo-api.herokuapp.com/items/" + checkboxId + "/not-complete"
}
$.ajax(endpoint, {
method: "post",
dataType: "json",
contentType: "application/json",
jsonp: false,
error: function () {
console.error("couldn't update todo :(");
}
});
});
function loadTodoList(){
$.ajax("https://rowan-todo-api.herokuapp.com/items", {
dataType: "json",
jsonp: false,
error: function(){
console.error("couldn't load todos :(");
todoList.html('<p>Todo list could not be loaded</p>');
},
success: function(items){
var html = "";
for (const item of items) {
console.log(item);
html += "<li>";
html += item.description + " ";
if (item.complete === true){
html += '<input type="checkbox" checked class="todo-complete" id="'+item.id+'"/>';
}
else{
html += '<input type="checkbox" class="todo-complete" id="' + item.id +'"/>';
}
html += "</li>";
}
todoList.html(html);
}
});
}
loadTodoList();