-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathmain.js
72 lines (66 loc) · 2.15 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
const deleteBtn = document.querySelectorAll('.del')
const todoItem = document.querySelectorAll('span.not')
const todoComplete = document.querySelectorAll('span.completed')
Array.from(deleteBtn).forEach((el)=>{
el.addEventListener('click', deleteTodo)
})
Array.from(todoItem).forEach((el)=>{
el.addEventListener('click', markComplete)
})
Array.from(todoComplete).forEach((el)=>{
el.addEventListener('click', markIncomplete)
})
async function deleteTodo(){
//data set grabs any data attributes.. if in the ejs the data attribute was set to butt (data-butt='<%=el._id%>') then this would say dataset.butt
const todoId = this.parentNode.dataset.id
try{
const response = await fetch('todos/deleteTodo', {
method: 'delete',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'todoIdFromJSFile': todoId
})
})
const data = await response.json()
console.log(data)
location.reload()
}catch(err){
console.log(err)
}
}
async function markComplete(){
// if you have two identical items in your list this will only update the one you click one because of that unique id
const todoId = this.parentNode.dataset.id
try{
// todos route -> markComplete
const response = await fetch('todos/markComplete', {
method: 'put',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'todoIdFromJSFile': todoId
})
})
const data = await response.json()
console.log(data)
location.reload()
}catch(err){
console.log(err)
}
}
async function markIncomplete(){
const todoId = this.parentNode.dataset.id
try{
const response = await fetch('todos/markIncomplete', {
method: 'put',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'todoIdFromJSFile': todoId
})
})
const data = await response.json()
console.log(data)
location.reload()
}catch(err){
console.log(err)
}
}