-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
112 lines (99 loc) · 3.24 KB
/
main.js
File metadata and controls
112 lines (99 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
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(".submit-button")
// Error Message
const errMassge = function (err, inBody){
const message = document.createElement('div')
message.classList.add("filid-message")
message.innerHTML = `${err}`
inBody.prepend(message)
}
const displyData = 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 post Show
const getPostShow = async function(){
try {
const getData = await axios.get(apiEndPoint)
const data = getData.data
displyData(data)
} catch (err) {
errMassge(err, todoListUL)
}
}
getPostShow()
// New Post create
const getPost = async function (newPost){
try {
await axios.post(apiEndPoint, newPost,
{headers: { 'Content-Type': 'application/json;charset=UTF-8', "Access-Control-Allow-Origin": "*", "Accept": "application/json" }}
);
getPostShow()
} catch(err){
errMassge(err, todoListUL)
}
}
// New post action
submitBtn.addEventListener('click', async function(e){
e.preventDefault()
if(input.value === '') return false
const post = {
text: input.value
}
await getPost(post)
input.value = ""
})
// Post delete
const deletePost = async function(id){
await axios.delete(`${apiEndPoint}/${id}`)
await getPostShow()
}
// Delete Post Action
todoListUL.addEventListener('click', function(e){
e.preventDefault()
const targetSel = e.target.classList.contains('delete-btn')
const id = e.target.closest('.list').dataset.id
if(targetSel){
deletePost(id)
}
})
// Post Update target select & input value set
todoListUL.addEventListener('click', async function(e){
e.preventDefault()
const targeUpdate = e.target.classList.contains('get-update')
const id = e.target.closest(".list").dataset.id
const res = await axios.get(`${apiEndPoint}/${id}`)
const post = res.data
if(targeUpdate) {
document.querySelector(".form-control").value = post.text,
document.querySelector(".update-submit-btn").value= post.id
}
})
// Post update / Put
const postUpdate = async function(updatePost, id){
await axios.put(`${apiEndPoint}/${id}`, updatePost)
await getPostShow()
}
// Update button action
document.querySelector(".update-submit-btn").addEventListener('click', (e)=>{
e.preventDefault()
const id = e.target.value
const updatePost = {
text: document.querySelector(".form-control").value
}
postUpdate(updatePost, id)
})