-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
118 lines (105 loc) · 3.24 KB
/
Copy pathmain.js
File metadata and controls
118 lines (105 loc) · 3.24 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
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
const apiEndPoint = "http://localhost:3000/user";
const todoListUL = document.querySelector(".todo-list-wrap ul")
const todoListWrap = document.querySelector(".todo-list-wrap")
const input = document.querySelector(".single-box input")
const submitBtn = document.querySelector(".form-wrap")
// Disply Card UI
const displyCard = function (data) {
todoListUL.innerHTML = ""
data.map(item => {
const html = `
<li class="d-flex list align-items-center" data-id="${item?.id}">
<span>${item?.id} - ${item?.text}</span>
<span class="end">
<button class="btn btn-success get-update" id="#update-modal" data-bs-toggle="modal" data-bs-target="#update-modal">Edit</button>
<button class="delete-btn btn btn-danger">Delete</button>
</span>
</li>
`
todoListUL.insertAdjacentHTML("afterbegin", html)
})
}
// Get Display Post
const getDisplyPost = async function(){
const res = await fetch(apiEndPoint)
if(!res.ok){
throw new Error("API is Fails")
}
const posts = await res.json()
displyCard(posts)
}
getDisplyPost()
// Get Post
const getPost = async function(newPost) {
const res = await fetch(apiEndPoint, {
method: "POST",
body: JSON.stringify(newPost),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
await res.json()
getDisplyPost()
}
// Submit post
submitBtn.addEventListener("submit", async function(e){
e.preventDefault()
if(input.value === "") return false
const post = {
text: input.value
}
await getPost(post)
input.value = ""
})
// Get Delete
const getDelete = async function (id){
const response = await fetch(`${apiEndPoint}/${id}`, {
method: "DELETE",
})
await response.json()
}
todoListWrap.addEventListener('click', async function(e){
const targetBtn = e.target.classList.contains('delete-btn')
if(targetBtn){
const id = e.target.closest(".list").dataset.id
await getDelete(id)
// DOM Item Remove
getDisplyPost()
// e.target.closest(".list").remove()
}
})
// Get update
const updateText = document.querySelector(".input-fild .form-control")
const updateButon = document.querySelector(".create-submit-btn")
todoListWrap.addEventListener('click', async function(e){
e.preventDefault()
const targetBtn = e.target.classList.contains('get-update')
const id = e.target.closest(".list").dataset.id
const res = await fetch(`${apiEndPoint}/${id}`)
const post = await res.json()
if(targetBtn){
updateText.value = post.text
updateButon.value = post.id
}
})
// Update json create
const getUpdate = async function(updatePost, id){
const res = await fetch(`${apiEndPoint}/${id}`, {
method: "PUT",
body: JSON.stringify(updatePost),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
await res.json()
}
// post update
updateButon.addEventListener('click', async function(e){
e.preventDefault()
const id = e.target.value
const post = {
text: updateText.value
}
await getUpdate(post, id)
getDisplyPost()
})