-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (110 loc) · 4.26 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
let notesForm = document.querySelector("#notes-list")
let notesList = document.querySelector(".notes")
notesForm.addEventListener("submit", function(event) {
let notesTextInput = document.querySelector("#notes-text")
let notesText = notesTextInput.value
event.preventDefault ()
//create a new note item on the list by sending a POST
//request so that it is added to the database
notesTextInput.value = ""
createNewNote (notesText)
})
function renderNotes () {
notesList.innerHTML = ""
fetch('http://localhost:3000/notes', {
method: "GET"
})
.then(response => response.json())
.then(function (data) {
let list = document.createElement("ul")
//this is the parent of the new list of notes:
list.id = "item-list"
for (let item of data) {
let listItem = document.createElement("li")
listItem.dataset.id = item.id
listItem.innerText = item.item
let deleteIcon = document.createElement("span")
deleteIcon.id = "delete"
deleteIcon.classList.add("fa", "fa-trash", "mar-l-xs")
listItem.appendChild(deleteIcon)
let editIcon = document.createElement("span")
editIcon.id = "edit"
editIcon.classList.add("fa", "fa-edit")
let saveIcon = document.createElement("span")
saveIcon.id = "save"
saveIcon.classList.add("fa", "fa-save")
listItem.appendChild(editIcon)
listItem.appendChild(saveIcon)
list.appendChild(listItem)
notesList.appendChild(list)
}
}) .then(() => editNotesItem)
}
function editNotesItem (itemId) {
let noteToEdit = document.querySelector(`li[data-id="${itemId}"]`)
let notesTextInput = document.querySelector("#notes-text")
let notesText = notesTextInput.value
fetch(`http://localhost:3000/notes/${itemId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ item:notesText, done: false, created: moment().format() })
})
.then(response => response.json())
.then(function () {
let newChildEl = document.createElement("input")
let list = document.querySelector("#item-list")
newChildEl.id = "edited-child"
newChildEl.innerText = ("")
list.appendChild(newChildEl)
// oldInputParent.appendChild(newChildEl)
// newChildEl.appendChild(newChildEl)
list.replaceChild(newChildEl, noteToEdit)
})
}
//write the fetch request to post data in its own function
function createNewNote (notesText) {
fetch("http://localhost:3000/notes", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ item:notesText, done: false, created: moment().format() })
})
.then(() => renderNotes())
}
function noteFeed () {
let notesAppear = document.querySelector(".notes")
let notesDiv = document.createElement("div")
notesAppear.appendChild(notesDiv)
}
notesList.addEventListener("click", function(event) {
let targetEl = event.target
if (targetEl.matches("#edit")) {
console.log("EDIT")
editNotesItem(targetEl.parentElement.dataset.id)
} else if (targetEl.matches("#save")) {
console.log("SAVE")
} else if (targetEl.matches("#delete")) {
console.log("DELETE")
deleteNotesItem(targetEl.parentElement.dataset.id)
}
})
function deleteNotesItem (itemId) {
let noteToDelete = document.querySelector(`li[data-id="${itemId}"]`)
fetch(`http://localhost:3000/notes/${itemId}`, {
method: "DELETE"
})
.then(function () {
document.querySelector("#item-list").removeChild(noteToDelete)
})
}
// function newChild (itemId) {
// let oldInputParent = document.querySelector("#item-list")
// let noteToEdit = document.getElementById(`li[data-id="${itemId}"]`)
// let listItem = document.createElement("li")
// listItem.dataset.id = item.id
// listItem.innerText = item.item
// fetch(`http://localhost:3000/notes/${itemId}`, {
// method: "PATCH",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify({ item:noteToEdit, done: false, created: moment().format() })
// })
// }